Here is some example data for a hypothetical meta-analysis on the effectiveness of sports-promotion interventions for which I would like to create a forest plot:
example.df = data.frame(Author = c("McAuliffe et al.", "Palen et al.", "Manning et al.", "Richters et al.", "Grello et al.","Mpofu et al.", "Kuo & St Lawrence", "Langstrom & Hanson", "Ompad et al.", "Abdullah et al.","Yan", "Peltzer & Pengpid", "Lo & Wei", "Haggstrom-Nordin et al.", "Mwaba & Naidoo", "Hughes et al.","Lydie et al.", "Zimmer-Gembeck et al.", "Babalola", "Garos et al.", "Pinkerton et al."),
Sport = c("Basketball", "Basketball", "Baseball", "Dance", "Baseball", "Dance", "Wrestling","Wrestling", "Dance", "Baseball", "Wrestling", "Dance", "Swimming", "Swimming","Basketball", "Basketball", "Basketball", "Basketball", "Basketball", "Swimming", "Wrestling"),
Gender = c("Male", "Female", "Male", "Male", "Female", "Male", "Male", "Male", "Male", "Female","Female", "Male", "Female", "Female", "Female", "Male", "Female", "Female", "Female", "Male", "Female"),
d = c(-0.12, 0.53, 0.11, 0.02, 0.32, 0.04, 0.03,0.04,0.26, 0.76, 1.11, 0.34, 0.77, 1.19, 0.59, 0.15, 0.30, 0.81, 0.12, 0.11, 1.01),
d_SE = c(.10, .04, .06, .01, .11, .08, .08, .04, .05, .05, .14, .07, .05, .08, .19, .16, .07, .16, .06, .18, .15))
The data frame contains author names, the sport, whether the sample was male or female, the effect size for the intervention, and the standard error of the effect size. I am hoping to create a dot plot mapping shape to gender, and faceting by the particular sport. After following examples in Chang's "cookbook" and this related query, I've come up with the following code that meets most of my formatting needs:
p<-ggplot(example.df, aes(x=Author, y=d, ymin=d-1.96*d_SE, ymax=d+1.96*d_SE,shape=Gender))+
geom_pointrange() +
coord_flip()+
scale_y_continuous(limits=c(-2,2),breaks=c(-2,-1.5,-1,-0.5,0,.5,1,1.5,2))+
geom_hline(yintercept=0, color="grey60",linetype="dashed")+
theme_bw()+
theme(panel.grid.major.x=element_blank(),panel.grid.minor.x=element_blank(),panel.grid.major.y=element_line(color="grey60",linetype="dashed"))+
facet_grid(Sport ~ ., scales="free_y")
p
My problem, however, is that the resulting plots for each facet (below) have every author in the entire data frame plotted on the y-axis (technically x-axis, but the axes are flipped). Instead, I only want the authors with data relevant to a given facet to be plotted on the author-associated axis of that facet, so each facet should have a different list of authors on the axis.
I had thought the scales="free_y"
component of the facet_grid
layer would ensure a unique author axis for each facet (I've also tried scales="free_x"
, given the inverted axes), but this is not having the intended effect.
Does anyone know of a way that I could ensure that the only author names that appear on each facet's axis are the ones with associated data for that facet?
See Question&Answers more detail:os