r - ggplot2 geom_plot 值在同一栏中

标签 r ggplot2 data-visualization

我正在尝试在 上绘制特定图形.我想这样绘制:

enter image description here

我的代码是:

library("ggplot2")

exec <- data.frame(
threads = c(2,4,8,16,32,64,2,4,8,16,32,64,2,4,8,16,32,64,2,4,8,16,32,64), 
msgs=c(100,100,100,100,100,100,400,400,400,400,400,400,1600,1600,1600,
       1600,1600,1600,6400,6400,6400,6400,6400,6400),
qtds=c(3778.2,6000,6000,6000,6000,6000,3756.6,7462.8,14666.2,24000,24000,24000,
       3762.4,7445.4,14284.4,27869.8,55877.4,93407.4,2934,5427.4,10717.6,17214.2,
       26222.2,37333.6))

ggplot(data=exec, aes(x=threads, y=qtds, fill=msgs)) + geom_bar(stat="identity", 
       position="dodge")

然而,所有的msgs都在同一个栏中,如图所示。

enter image description here

我如何解决它?

最佳答案

您需要将整数转换为因子。使用 as.factor() 进行转换。

library("ggplot2")

exec <- data.frame(threads = c(2,4,8,16,32,64,2,4,8,16,32,64,2,4,8,16,32,64,2,4,8,16,32,64),  msgs=c("100 msg/min","100 msg/min","100 msg/min","100 msg/min","100 msg/min","100 msg/min","400 msg/min","400 msg/min","400 msg/min","400 msg/min","400 msg/min","400 msg/min","1600 msg/min","1600 msg/min","1600 msg/min","1600 msg/min","1600 msg/min","1600 msg/min","6400 msg/min","6400 msg/min","6400 msg/min","6400 msg/min","6400 msg/min","6400 msg/min"),
        qtds=c(3778.2,6000,6000,6000,6000,6000,3756.6,7462.8,14666.2,24000,24000,24000,3762.4,7445.4,14284.4,27869.8,55877.4,93407.4,2934,5427.4,10717.6,17214.2,26222.2,37333.6))

ggplot(data=exec, aes(x=as.factor(threads), y=qtds, fill=msgs)) +
geom_bar(stat="identity", position="dodge") +
scale_fill_discrete(name = "Msgs") +
  xlab("Threads") +
  ylab("Qtds") +
  theme_bw() +
  theme(legend.position = c(0.22,0.85))

enter image description here

关于r - ggplot2 geom_plot 值在同一栏中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54268057/

相关文章:

r - 用户定义的 S3 组通用函数如何在 R 中工作?

两个矩阵之间的行相关性

r - ggplot2 条形图和线图的叠加

javascript - 如何在 JavaScript 中构建交互式 3D 图表

python - 如何制作象形图/图标图表?

r - 在具有 3 个 y 轴的单个图中绘制 4 条曲线

r - 预测 AUC 1 的集成模型

r - ggplot2:为每组的平均值添加行

r - 在 ggplot2 中的图表标题周围放置一个灰色框

r - 如何对 R 中不同 map 中的点使用相同的色标?