How to plot a combined bar and line plot in ggplot2

放肆的年华 提交于 2020-07-03 07:25:11

问题


I have the following data which I am trying to plot as combined bar and line plot (with CI)

A data frame of Feature, Count, Odds Ratio and Confidence Interval values for OR A data frame of Feature, Count, Odds Ratio and Confidence Interval values for OR

I am trying to get a plot as A bar plot for count over lapped with a line plot for Odds Ratio with CI bars

I tried to plot in ggplot2 using following code:

ggplot(feat)+
  geom_bar(aes(x=Feat, y=Count),stat="identity", fill = "steelblue") +
  geom_line(aes(x=Feat, y=OR*max(feat$Count)),stat="identity", group = 1) +
  geom_point(aes(x=Feat, y=OR*max(feat$Count))) +
  geom_errorbar(aes(x=Feat, ymin=CI1, ymax=CI2), width=.1, colour="orange", 
                position = position_dodge(0.05))

However, I am not getting the CI bars for the line graph, as can be seen in pic: Rather, I am getting them for barplot Rather, I am getting them for barplot

Can someone can please help me out to sort this issue.

Thanks

Edit - Dput:

df <- structure(list(Feat = structure(1:8, .Label = c("A", "B", "C", 
"D", "E", "F", "G", "H"), class = "factor"), Count = structure(c(2L, 
8L, 7L, 5L, 4L, 1L, 6L, 3L), .Label = c("13", "145", "2", "25", 
"26", "3", "37", "43"), class = "factor"), OR = structure(c(4L, 
2L, 1L, 5L, 3L, 7L, 6L, 8L), .Label = c("0.38", "1.24", "1.33", 
"1.51", "1.91", "2.08", "2.27", "3.58"), class = "factor"), CI1 = structure(c(7L, 
4L, 1L, 6L, 3L, 5L, 2L, 2L), .Label = c("0.26", "0.43", "0.85", 
"0.89", "1.2", "1.24", "1.25"), class = "factor"), CI2 = structure(c(3L, 
2L, 1L, 6L, 4L, 7L, 8L, 5L), .Label = c("0.53", "1.7", "1.82", 
"1.98", "13.07", "2.83", "3.92", "6.13"), class = "factor")), class = "data.frame", row.names = c(NA, 
-8L))

回答1:


Is this what you had in mind?

ratio <- max(feat$Count)/max(feat$CI2)
ggplot(feat) +
  geom_bar(aes(x=Feat, y=Count),stat="identity", fill = "steelblue") +
  geom_line(aes(x=Feat, y=OR*ratio),stat="identity", group = 1) +
  geom_point(aes(x=Feat, y=OR*ratio)) +
  geom_errorbar(aes(x=Feat, ymin=CI1*ratio, ymax=CI2*ratio), width=.1, colour="orange", 
                position = position_dodge(0.05)) +
    scale_y_continuous("Count", sec.axis = sec_axis(~ . / ratio, name = "Odds Ratio"))

Edit: Just for fun with the legend too.

ggplot(feat) +
  geom_bar(aes(x=Feat, y=Count, fill = "Count"),stat="identity") + scale_fill_manual(values="steelblue") +
  geom_line(aes(x=Feat, y=OR*ratio, color = "Odds Ratio"),stat="identity", group = 1) + scale_color_manual(values="orange") +
  geom_point(aes(x=Feat, y=OR*ratio)) +
  geom_errorbar(aes(x=Feat, ymin=CI1*ratio, ymax=CI2*ratio), width=.1, colour="orange", 
                position = position_dodge(0.05)) +
  scale_y_continuous("Count", sec.axis = sec_axis(~ . / ratio, name = "Odds Ratio")) +  
  theme(legend.key=element_blank(), legend.title=element_blank(), legend.box="horizontal",legend.position = "bottom")

Since you asked about adding p values for comparisons in the comments, here is a way you can do that. Unfortunately, because you don't really want to add **all* the comparisons, there's a little bit of hard coding to do.

library(ggplot2)
library(ggsignif)
ggplot(feat,aes(x=Feat, y=Count)) +
  geom_bar(aes(fill = "Count"),stat="identity") + scale_fill_manual(values="steelblue") +
  geom_line(aes(x=Feat, y=OR*ratio, color = "Odds Ratio"),stat="identity", group = 1) + scale_color_manual(values="orange") +
  geom_point(aes(x=Feat, y=OR*ratio)) +
  geom_errorbar(aes(x=Feat, ymin=CI1*ratio, ymax=CI2*ratio), width=.1, colour="orange", 
                position = position_dodge(0.05)) +
  scale_y_continuous("Count", sec.axis = sec_axis(~ . / ratio, name = "Odds Ratio")) +  
  theme(legend.key=element_blank(), legend.title=element_blank(), legend.box="horizontal",legend.position = "bottom") + 
  geom_signif(comparisons = list(c("A","H"),c("B","F"),c("D","E")),
              y_position = c(150,60,40),
              annotation = c("***","***","n.s."))



来源:https://stackoverflow.com/questions/61002397/how-to-plot-a-combined-bar-and-line-plot-in-ggplot2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!