Как построить комбинированный линейный и линейный график в ggplot2 - PullRequest
3 голосов
/ 03 апреля 2020

У меня есть следующие данные, которые я пытаюсь построить как комбинированный линейчатый и линейный график (с CI)

Фрейм данных значений Feature, Count, Odds Ratio и Confidence Interval для OR A data frame of Feature, Count, Odds Ratio and Confidence Interval values for OR

Я пытаюсь получить график в виде столбчатого графика для подсчета, перекрытого линейным графиком для коэффициента шансов с блоками CI

A bar plot for count over lapped with a line plot for Odds Ratio with CI bars

Я попытался построить график в ggplot2, используя следующий код:

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

Однако я не получаю столбцы CI для линейного графика, как это видно в pi c: Скорее, я получаю их за барплот Rather, I am getting them for barplot

Может кто-нибудь может помочь мне разобраться в этом вопросе.

Спасибо

Редактировать - Вывод:

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 Ответ

5 голосов
/ 03 апреля 2020

Это то, что вы имели в виду?

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

Редактировать: Просто для развлечения с легендой тоже.

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

Поскольку вы спрашивали о добавлении значений p для сравнений в комментариях, вот способ, которым вы можете это сделать. К сожалению, поскольку вы действительно не хотите добавлять ** все * сравнения, есть немного сложного кодирования.

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

...