ggplot geom_pointrange doesn't show data that have a single value

Viewed 159

I know from naming that geom_pointrange is to show the range of a group of data, but I think it should still plot all the data even if there is only one value(point).

Is there a way to configure it to show single-value points or it has to include geom_point?

1 Answers

There is no problem to visualize a treatement if you have only one response (one point in a group of data) with geom_pointrange. It just won't show the interval but only point. Please see the sample below (Treatment 3 has only 1 response).

library(tidyverse)
# Create a simple example dataset
df <- data.frame(
  trt = factor(c(1, 1, 2, 2, 3)),
  resp = c(1, 5, 3, 4, 2),
  group = factor(c(1, 2, 1, 2, 1)),
  upper = c(1.1, 5.3, 3.3, 4.2, 2),
  lower = c(0.8, 4.6, 2.4, 3.6, 2)
)

p <- ggplot(df, aes(trt, resp, colour = group))
p + geom_pointrange(aes(ymin = lower, ymax = upper))

Output: geom_pointrange plot

Related