在 R 中创建多列数据的分组条形图 [英] Creating grouped bar-plot of multi-column data in R

查看:35
本文介绍了在 R 中创建多列数据的分组条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据

       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).

我在网上查看的所有解决方案/示例都将类似的数据放入三列布局中.我不知道如何使用我必须生成分组条形图的数据.或者,如果有办法将这些数据(手动转换不是一种选择,因为它是一个包含很多行的巨大文件)转换为 Rggplot 兼容的数据格式.

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

推荐答案

根据要求,一个同样使用 reshape2ggplot2 解决方案:

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) = 0ggplot2 将其视为零高度的条并且不绘制任何内容,而 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 中创建多列数据的分组条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆