rggplot2

Show partial segments removed after limit scales for `geom_pointrange`


Let's see an example:

# Create a simple example dataset
df <- data.frame(
  trt = factor(c(1, 1, 2, 2)),
  resp = c(1, 5, 3, 4),
  group = factor(c(1, 2, 1, 2)),
  upper = c(1.1, 5.3, 3.3, 4.2),
  lower = c(0.8, 4.6, 2.4, 3.6)
)
p <- ggplot(df, aes(trt, resp, colour = group))
p + geom_pointrange(aes(ymin = lower, ymax = upper))
p + geom_pointrange(aes(ymin = lower, ymax = upper)) + scale_y_continuous(limits = c(NA, 5.2))

First image enter image description here Second image enter image description here

In the second one, the segment around 5 for trt==1 and group==2 is removed, while I would like it was shown from ymin to the graph limit. How should I do?

Thanks


Solution

  • coord_cartesian() will plot values even if they are close or outside of your defined limits.

    p + geom_pointrange(aes(ymin = lower, ymax = upper)) + coord_cartesian(ylim=c(NA, 5.2))
    

    enter image description here