首页 > 解决方案 > 如何在ggplot2的辅助轴上添加水平截距

问题描述

我有一个带有两个 y 轴的图,我需要使用辅助 y 轴上的值添加一条线。

这是一个示例链接: OR 绘制在辅助 y 轴上。我想在 OR=1 处添加一条线

OR 绘制在辅助 y 轴上。我想在 OR=1 处添加一条线。

请帮忙。

标签: rggplot2rgraph

解决方案


看看 OR 轴是如何计算的primary axis / ratio = OR:有了OR = 1你就可以解决主轴primary axis / ratio = 1 <=> primary axis = ratio::

feat <- 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))
feat$Count <- as.numeric(as.character(feat$Count))
feat$OR <- as.numeric(as.character(feat$OR))
feat$CI1 <- as.numeric(as.character(feat$CI1))
feat$CI2 <- as.numeric(as.character(feat$CI2))

ratio <- max(feat$Count)/max(feat$CI2)
library(ggplot2)
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") +
  geom_hline(yintercept = ratio)

reprex 包于 2020-08-23 创建(v0.3.0)


推荐阅读