rggplot2

ggplot: adding regression line and CI when y axis is logged


here is my data:x is input values, y is predicted values using lm() model, upr and lwr are 95% CIs calculated from the lm() model

library(tidyverse)
library(scales)
library(cowplot)
library(ggpmisc)

    fit_line <-dput(fit_line)
structure(list(x = c(0.048, 0.55, 0.44, 0.052, 0.029, 0.09, 0.2, 
0.48, 0.51, 0.11, 1.44, 0.15, 0.4, 1.12, 0.54), y = c(535.247309812533, 
1542.55243882316, 1321.82820736665, 543.273645501861, 497.122215288227, 
619.523834550474, 840.248066006986, 1402.09156425993, 1462.28908192988, 
659.655512997113, 3328.41212969858, 739.91886989039, 1241.56485047337, 
2686.30527455236, 1522.48659959984), lwr = c(-474.582987840204, 
754.520653106387, 576.903888431076, -461.460391144338, -537.324262756609, 
-338.409577605306, -2.1782812716142, 647.785886082738, 696.130234140302, 
-274.914182245542, 1249.20869628597, -150.884187631952, 498.439060263022, 
1155.6674836111, 740.548147246478), upr = c(1545.07760746527, 
2330.58422453993, 2066.75252630222, 1548.00768214806, 1531.56869333306, 
1577.45724670625, 1682.67441328559, 2156.39724243711, 2228.44792971946, 
1594.22520823977, 5407.61556311119, 1630.72192741273, 1984.69064068372, 
4216.94306549362, 2304.4250519532)), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 
"14", "15"))

so what i try to plot is line with CIs, byt log y scale.But smth happening: so first when i do not log scale:

ggplot() +
 # scale_y_log10(breaks = c(10, 100, 1000, 10000),labels = c("10", "100", "1000", "10000")  ) +
  coord_cartesian(xlim = c(0, 1.5),ylim = c(10, 10000)) +
  theme_cowplot() +
  annotation_logticks(sides = "l") +
  geom_ribbon(data = fit_line, aes(x,y,ymin = lwr, ymax = upr), fill = "grey80", alpha = 0.4) 

enter image description here

I don't understand where that black rectangle is coming from? then when i log the y-scale i loose some CIs:

ggplot() +
  scale_y_log10(breaks = c(10, 100, 1000, 10000),labels = c("10", "100", "1000", "10000")  ) +
  coord_cartesian(xlim = c(0, 1.5),ylim = c(10, 10000)) +
  theme_cowplot() +
  annotation_logticks(sides = "l") +
  geom_ribbon(data = fit_line, aes(x,y,ymin = lwr, ymax = upr), fill = "grey80", alpha = 0.4) 

enter image description here

Can you please help me to figure out what is the issue in my code? or what is happening?


Solution

  • annotation_logticks function takes log10 from the original y axis values, which ggplot holds after log10 transformation of the axis with scale_y_log10. If you don't actually do the transformation, the values for logtickes will be very small (1 to 5 in your case). This is why you see that black rectangular, which made of compressed logticks. So, apply annotation_logticks only together with axis log transformation.

    As I mentioned in the comments, for the second plot, you lose some intervals because you have negative values in lwr. You need to decide what to do with them. You may want to convert negative values to some small positive number.

    geom_ribbon(data = fit_line %>% mutate(lwr = ifelse(lwr < 0, 1, lwr)), 
                aes(x,y,ymin = lwr, ymax = upr), fill = "grey80", alpha = 0.4)