2016-05-08 81 views
0

Ia完全是新的R和刚刚免除edX的介绍。不幸的是,它没有教任何关于ggplot2的东西,我想在我的论文中使用我的图表。我想获得不同颜色的100%条形图,并且如果可能的话,可以获得相应条形图中的值。我的数据是这样的:彩色100%barplot与ggplot2

Concentration,Percentage,Phenotype 
Control,0.933333333,0 
Control,0.014814815,1 
Control,0.022222222,2 
Control,0.02962963,3 
0.002,0.918181818,0 
0.002,0.018181818,1 
0.002,0.018181818,2 
0.002,0.045454545,3 
0.02,0.930434783,0 
0.02,0.017391304,1 
0.02,0.017391304,2 
0.02,0.034782609,3 
0.2,0.928571429,0 
0.2,0.032467532,1 
0.2,0.012987013,2 
0.2,0.025974026,3 
2,0.859813084,0 
2,0.028037383,1 
2,0.046728972,2 
2,0.065420561,3 

和我使用的代码是这样的:

ggplot(Inj, aes(x=Concentration, y=Percentage, fill=Phenotype))+geom_bar(stat='identity',color='black') 

生成的图形看起来就像是:

enter image description here

我如何可以改变颜色不同的酒吧,并获得酒吧%的价值?

感谢您的帮助

回答

0

您可以使用scale_x_discrete手动的因素重新排序是这样的:

ggplot(Inj, aes(x=Concentration, y=Percentage, fill=Phenotype)) + 
    geom_bar(stat='identity',color='black') + 
    scale_fill_grey(start = .4, end = .9) + 
    theme_bw()+ylab("Distribution") + 
    xlab("Contentration [mg/ml]") + 
    ggtitle("Dose-respond analysis") + 
    theme(legend.title = element_text(colour="black", size=10, face="bold")) + 
    theme(legend.background = element_rect(fill="white", 
            size=0.5, linetype="solid", 
            colour ="black")) + 
    scale_x_discrete(limits=c('Control','0.002', '0.02', '0.2', '2'), 
        labels=c('Control\n(N=000)', 
          '0.002\n(N=000)', 
          '0.02\n(N=000)', 
          '0.2\n(N=000)', 
          '2\n(N=000)')) 

我响应在下面的评论你的问题添加了labels部分scale_x_discrete。你只需要更新N个值。请注意,我在每个标签中放置了\n,以便将标签文本强制为两行,因此不会混乱。如果您希望将它放在一条线上,请删除\n

+0

好的,谢谢!最后一件事就是用每个酒吧的人数来标注每个酒吧。比如n = 158或类似的控件,n = 145,等等。那可能吗?只有我在互联网上找到的东西是添加一个酒吧的实际价值,但我想单独标记每一个酒吧。 –

+0

刚刚编辑答案以包含此问题的解决方案。 – user6275647

1

您可以通过使您的填充变量的因素来控制颜色。然后,你可以手动调整这样

ggplot(Inj, aes(x=Concentration, y=Percentage, fill=factor(Phenotype)))+geom_bar(stat='identity',color='black') + 
    scale_fill_manual(values=c("red", "blue", "yellow", "pink")) 

enter image description here

我不建议把值在颜色他们不会为非常小的值可见的颜色。我会用另一种方式来显示数据。