我有以下数据
Input Rtime Rcost Rsolutions Btime Bcost 1 12 proc. 1 36 614425 40 36 2 15 proc. 1 51 534037 50 51 3 18-proc 5 62 1843820 66 66 4 20-proc 4 68 1645581 104400 73 5 20-proc(l) 4 64 1658509 14400 65 6 21-proc 10 78 3923623 453600 82我想根据这些数据创建一个分组条形图,这样 x 轴包含 Input 字段(作为组),y 轴代表 Rtime 和 Btime 字段(两个条形).
I want to create a grouped bar chart from this data such that x-axis contains Input field (as groups) and y axis represent the log scale for the Rtime and Btime fields (the two bars).
我在网上查看的所有解决方案/示例都将类似的数据放入三列布局中.我不知道如何使用我必须生成分组条形图的数据.或者,如果有办法将这些数据(手动转换不是一种选择,因为它是一个包含很多行的巨大文件)转换为 R 和 ggplot 兼容的数据格式.
All solutions/examples I checked online had similar data put into a three column layout. I do not know how to use the data I have to generate the grouped bar-chart. Or if there is a way to convert this data (manually converting is not an options because it is a huge file with a lot of rows) into a R and ggplot compatible data format.
使用 gncs 解决方案生成的图形
Graph generated using gncs solution
推荐答案根据要求,一个同样使用 reshape2 的 ggplot2 解决方案:
As requested, a ggplot2 solution that also uses reshape2:
library(reshape2) df <- read.table(text = " Input Rtime Rcost Rsolutions Btime Bcost 1 12-proc. 1 36 614425 40 36 2 15-proc. 1 51 534037 50 51 3 18-proc 5 62 1843820 66 66 4 20-proc 4 68 1645581 104400 73 5 20-proc(l) 4 64 1658509 14400 65 6 21-proc 10 78 3923623 453600 82",header = TRUE,sep = "") dfm <- melt(df[,c('Input','Rtime','Btime')],id.vars = 1) ggplot(dfm,aes(x = Input,y = value)) + geom_bar(aes(fill = variable),stat = "identity",position = "dodge") + scale_y_log10()注意这里的样式差异,因为 log(1) = 0,ggplot2 将其视为零高度的条并且不绘制任何内容,而 barplot 绘制了一个小存根(在我看来这有点误导).
Note a style difference here, where since log(1) = 0, ggplot2 treats that as a bar of zero height and doesn't plot anything, whereas barplot plots a little stub (which in my opinion is a little misleading).
更多推荐
在 R 中创建多列数据的分组条形图
发布评论