0

I have a plot with two y-axes, I need to add a hline using values on secondary y-axis.

Here is an example link: OR is plotted on secondary y-axis. I want to add a hline at OR=1

OR is plotted on secondary y-axis. I want to add a hline at OR=1.

Please help.

AST
  • 57
  • 1
  • 6

1 Answers1

1

Have a look how the OR axis is calculated: primary axis / ratio = OR. With OR = 1 you can just solve for the primary axis: 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)

Created on 2020-08-23 by the reprex package (v0.3.0)

starja
  • 9,887
  • 1
  • 13
  • 28