R中ggplot2中的定制条形图

时间:2013-04-30 15:52:33

标签: r ggplot2

我有以下数据来生成ggplot2(R)

中的条形图
Bin Plr groupname   data    ci 
3   1%  solution    1002.97 6.98322
10  1%  solution    1001.7  1.25921
30  1%  solution    1176.47 12.1888
3   1%  w/o solution    1206.76 6.97126
10  1%  w/o solution    1211.65 1.81174
30  1%  w/o solution    1395.84 15.8956
3   4%  solution    1003.25 6.98989
10  4%  solution    1002.14 0.870957
30  4%  solution    1217.76 45.8752
3   4%  w/o solution    1207.18 6.99091
10  4%  w/o solution    1210.35 1.97528
30  4%  w/o solution    1470.81 48.6673

所以,我正在寻找两个级别的分组。谁能建议我如何从Rg中的ggplot获得以下条形图 http://oi42.tinypic.com/2s8rb7m.jpg

2 个答案:

答案 0 :(得分:0)

试试这个:

ggplot(my_data, aes(x=bin, y=data, fill=groupname)) 
+ geom_bar(stat="identity", position="dodge")
+facet_wrap(~ plr )
+labs(title = "Gains from using the solution", x= "size", y= "time[ms]" )

edit2:sry,需要将x作为因子投射,因此它不会进行线性比例。还使用宽度将其缩小了一点以匹配您的样本图。我的新想法:

(ggplot(my_data, aes(x=factor(Bin), y=data, fill=groupname, width=.65)) 
+ geom_bar( stat="identity", position="dodge")
+facet_wrap(~Plr )
+labs(title = "Gains from using the solution", x= "size", y= "time[ms]" )
 + geom_errorbar(aes(ymin=data-ci, ymax=data+ci),position="dodge") 
 )

答案 1 :(得分:0)

使用Green Demon的帮助,我找到了制作我需要的图表的正确方法

ggplot(my_data, aes(x=factor(Bin), y=data, fill=groupname, width=.65)) + 
      geom_bar( stat="identity", position="dodge")+facet_wrap(~Plr ) +
      labs(title = "Gains from using the solution", x= "size", y= "time[ms]",
                                    linetype='custom title',fill="") + 
       geom_errorbar(aes(ymin=data-ci, ymax=data+ci),
                                    width=0.25,position=position_dodge(0.65))

enter image description here