0

I have a graph with two results per exposure on the y axis but I want these to appear 'underneath' each other like this plot I can make with ggforestplot https://nightingalehealth.github.io/ggforestplot/index.html

But I want to do this using ggplot2. Here's an example df.

df <- data.frame(
  ID = 1:6,
  exposure = c('A', 'B', 'C', 'A', 'B', 'C'),
  data= c('d', 'd', 'd', 'r', 'r', 'r'),
  type= c('b', 'b', 'b', 'c', 'c', 'd'),
  cidown = c("0.5", "0.5", "0.6", "0.7", "0.1", "0"),
  ciup= c("1", "1.1", "1.2", "2", "2.4", "1"),
  p= c("0.9", "0.8", "0", "0.1", "0.2", "0"),
  se= c(50000, 60000, 80000, 70000, 45000, 50000),
  b= c("0.7", "0.8", "1", "1.5", "1", "0.5")
)

I want to make a forest plot where there are two results for each exposure [as you can see my code below plots my exposures - A,B,C - twice on the same line but I'd like one above the other]

      library(ggplot2)
      ggplot(df, aes(x = b, y = exposure, xmin = ciup, xmax = cidown))+
      geom_point(size=4,aes(shape=data))+
      geom_pointrange(shape=1, show.legend = TRUE) +
      theme(text=element_text(family="short"))  

I do not want them as facets, I already have facet elements.

Thanks

ayeepi
  • 135
  • 8
  • Does this address your question? https://stackoverflow.com/questions/52338137/vertical-equivalent-of-position-dodge-for-geom-point-on-categorical-scale – Jon Spring Apr 05 '23 at 16:15

1 Answers1

2

Sometimes one cannot see the forest for the trees...

What you are looking for is a vertical dodge and that makes this question a very close relative to Vertical equivalent of position_dodge for geom_point on categorical scale. You can achieve your plot, therefore, by either using coord_flip, or, more elegantly, by using the package {ggstance}.

library(ggstance)

ggplot(df, aes(x = b, y = exposure, xmin = ciup, xmax = cidown, group = data)) +
  geom_point(size = 4, aes(shape = data), position = position_dodgev(height = .5)) +
  geom_pointrange(shape = 1, position = position_dodgev(height = .5))

tjebo
  • 21,977
  • 7
  • 58
  • 94
  • 2
    add `aes(group = data)` to the pointrange (or add it to the global `aes()`) to make the dodging order align with the other layer. currently exposure C is reversed. – Jon Spring Apr 05 '23 at 16:16
  • @JonSpring great shout, and good catch. Correcting as we speak – tjebo Apr 05 '23 at 16:18