Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
516 views
in Technique[技术] by (71.8m points)

r - How to plot a combined bar and line plot in ggplot2

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

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))
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"))

enter image description here

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")

enter image description here

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

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...