[英]position does not dodge for barplot in ggplot2
There have been a lot of posts for this type of question and I have tried many of them, but position="dodge" does not dodge the bars in the plot. 关于这种类型的问题有很多帖子,我已经尝试了很多,但是position =“ dodge”不会使剧情中的条变得避开。 My data is as follows:
我的数据如下:
+----------------------------------------------------+
| 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 |
+----------------------------------------------------+
and each time I get this: barplot using ggplot2 and as you can see position does not dodge. 每次我得到这个: 使用ggplot2的barplot ,并且您可以看到位置不会闪避。 I wanted something like this: barplot using Excel This is the code I have tried:
我想要这样的东西: 使用Excel的barplot这是我尝试过的代码:
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)
The other variations of the code keeps giving me errors and does not even produce a plot. 代码的其他变体不断给我带来错误,甚至没有绘制出曲线图。 Can anyone please point me in the right direction.
谁能给我指出正确的方向。 My first question to SO so apologies if the code or table is not formatted correctly.
我对SO的第一个问题是,如果代码或表的格式不正确,请您道歉。 I can easily do this in Excel but am learning R and I just want to know why R is not producing a similar kind of plot?
我可以在Excel中轻松地做到这一点,但是正在学习R,我只想知道R为什么不产生类似的图? Thanks!
谢谢!
You need to melt your data into long format first: 您需要首先将数据融为长格式:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.