r - 如何在 ggplot2 中绘制组合条形图和折线图

标签 r ggplot2 bar-chart linechart

我有以下数据,我试图将其绘制为组合条形图和折线图(带 CI)

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 条,如图所示:相反,我得到了条形图的 CI 条
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))

最佳答案

这是你想到的吗?

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 值以进行比较,因此您可以通过以下方式做到这一点。不幸的是,因为您真的不想添加 **all* 比较,因此需要进行一些硬编码。
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

关于r - 如何在 ggplot2 中绘制组合条形图和折线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61002397/

相关文章:

r - dplyr中的k折交叉验证?

r - 如何估计 R 中扩展卡尔曼滤波器的参数

r - 如何在散点图上的 stat_smooth 曲线下创建一个区域?

R ggplot箱线图改变颜色和填充

d3.js - D3.js转换

r - 在 ggplot 条形图和箱线图上放置星星 - 以指示显着性水平(p 值)

r - dplyr:样本量大于总体规模

r - 保存到文件后,情节被切断

r - 将离散标签添加到具有连续比例的 ggplot2 图

python - 如何使用输入 *.txt 文件绘制一个非常简单的条形图(Python、Matplotlib)?