GGPLOT2 - Sub Groups

#1
Hi,

I'm trying to create a stratified forest plot in ggplot2. In our analyses, we prepared a logistic regression model for each strata (Immigrants and Residents). I have included what I have (have.png) and what I need (need.png) in two images attached. Is there a simple way to do this in GGPLOT2?

(I've foresee a way, through creating a duplicate "Group variable" entry and suppressing the text manually on the graph through theme options, however I'm wondering if there is an easier, more efficient way)

Thanks in advance!

Also the need.png was a hack and slash job in mspaint, don't judge me lol!

Here are the data (from DPUT) and the GGPLOT2 Code:

Code:
 dats <- structure(list( group = structure(c(10L, 10L, 11L, 11L, 12L, 
12L), .Label = c("t", "y", "u", 
"a", "b", "c", "w", "e", "s", 
"Males vs Females", "High vs Low", "New vs Old"), class = "factor"), 
     strata = structure(c(3L, 4L, 3L, 4L, 3L, 4L), .Label = c("a", 
    "b", "Immigrant", "Resident"), class = "factor"),  est = c(1, 
    1.45, 1.56, 1.03, 1.46, 1.55),  low = c(1, 1.4, 1.5, 
    1, 1.31, 1.49),  high = c(1, 1.5, 1.63, 1.07, 1.51, 1.61
    )), .Names = c("group", "strata", "est", "low", 
"high"), row.names = c(NA, -6L), class = "data.frame")
Code:
forest1 <- ggplot(dats, aes(x = group  , y = est, ymin=low, ymax=high)) +
                          geom_hline(aes(yintercept = 1),linetype = 2, size=0.5) +
			  geom_errorbar(aes(ymin=low,ymax=high,color=strata), width=0.1 ) +
                          geom_pointrange(shape = 16,  aes(color=strata),  size = 0.75, stroke = 0.5) +
                          coord_flip() +
			  theme_bw()
 
Last edited:
#2
Took a bit of time for me to remember this but it's quite easy, you just need the position argument in geom_errorbar and geom_pointrange like
Code:
ggplot(dats, aes(x = group, y = est, ymin=low, ymax=high, colour=strata, group=interaction(group, strata))) +
  geom_hline(aes(yintercept = 1), linetype = 2, size=0.5) +
  geom_errorbar(aes(ymin=low, ymax=high), width=0.1, position = position_dodge(width = 0.90)) +
  geom_pointrange(shape = 16,  size = 0.75, stroke = 0.5, position = position_dodge(width = 0.90)) +
  coord_flip() +
  theme_bw()
Play around with the 'width' argument until you get what you're looking for.