简体   繁体   中英

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. 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. I wanted something like this: barplot using Excel This is the code I have tried:

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM