【问题标题】:How to add horizontal intercept on secondary axis in ggplot2如何在ggplot2的辅助轴上添加水平截距
【发布时间】:2020-08-23 19:01:19
【问题描述】:

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

这是一个示例链接: OR is plotted on secondary y-axis. I want to add a hline at OR=1

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

请帮忙。

【问题讨论】:

    标签: r ggplot2 rgraph


    【解决方案1】:

    看看 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 package (v0.3.0) 于 2020 年 8 月 23 日创建

    【讨论】:

      猜你喜欢
      • 2020-09-02
      • 1970-01-01
      • 2020-08-04
      • 2020-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多