在ggplot2中,位置不会躲避条形图

时间:2016-04-15 15:22:38

标签: r plot ggplot2

这类问题的帖子很多,我尝试了很多,但位置="闪避"不会躲避情节中的酒吧。我的数据如下:

+----------------------------------------------------+
|              Sps_wk0_wk7 Prot_Delta Seq_Delta      |
+----------------------------------------------------+
| 1  Prevotella_copri_DSM_18205         25   -47.214 |
| 2 Dorea_longicatena_DSM_13814         -2    12.925 |
| 3      Acidaminococcus_sp_D21          6     8.328 |
+----------------------------------------------------+

每次我得到这个:barplot using ggplot2并且你可以看到位置不会躲闪。 我想要这样的事情:barplot using Excel 这是我尝试过的代码:

    ggplot(proteogenomic_diff, aes(x=Sps_wk0_wk7))+
    geom_bar(aes(y=Prot_Delta, group=Prot_Delta, fill="blue"),  stat="identity", width=0.10)+
    geom_bar(aes(y=Seq_Delta, group=Seq_Delta, position="dodge", fill="Orange"), stat="identity", width=0.10)

代码的其他变体不断给我错误,甚至没有产生情节。任何人都可以指出我正确的方向。 如果代码或表格格式不正确,我向SO提出的第一个问题是如此道歉。我可以很容易地在Excel中做到这一点,但我正在学习R而我只是想知道为什么R不会产生类似的情节? 谢谢!

1 个答案:

答案 0 :(得分:1)

您需要先将数据融合成长格式:

library(reshape2)
proteogenomic_diff.m <- melt(proteogenomic_diff)

ggplot(proteogenomic_diff.m, aes(x = Sps_wk0_wk7, y = value, group = variable, fill = variable)) +
  geom_bar(stat="identity", position = position_dodge(width = .1), width = .1)

enter image description here