在 ggplot2 中,coord_flip 和自由比例尺不能一起工作 [英] In ggplot2, coord_flip and free scales don't work together

查看:23
本文介绍了在 ggplot2 中,coord_flip 和自由比例尺不能一起工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是一些示例数据,用于对体育促进干预措施的有效性进行假设性荟萃分析,我想为其创建森林图:

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 等人"、Mwaba & Naidoo"、Hughes 等人"、Lydie 等人"、Zimmer-Gembeck 等人"、Babalola"、"加罗斯等人"、"平克顿等人"),Sport = c(篮球"、篮球"、棒球"、舞蹈"、棒球"、舞蹈"、摔跤"、摔跤"、舞蹈"、棒球"、摔跤"、舞蹈"", "游泳", "游泳", "篮球", "篮球", "篮球", "篮球", "篮球", "游泳", "摔跤"),性别 = c("男"、"女"、"男"、"男"、"女"、"男"、"男"、"男"、"男"、"女"、"女"、"男"", "女", "女", "女", "男", "女", "女", "女", "男", "女"),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.15, 0.10.15)d_SE = c(.10, .04, .06, .01, .11, .08, .08, .04, .05, .05, .14, .07, .05, .08, .19, .16, .07, .16, .06, .18, .15))

数据框包含作者姓名、运动、样本是男性还是女性、干预的效果大小以及效果大小的标准误差.我希望创建一个点图映射形状到性别,以及特定运动的方面.在按照 Chang 的cookbook"和中接受的答案Gregor 的评论实际上解决了我的问题;唯一需要的改变是我必须计算置信区间的上限/下限值的列.

现在有了更新的数据框:

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 等人"、Mwaba & Naidoo"、Hughes 等人"、Lydie 等人"、Zimmer-Gembeck 等人"、Babalola"、"加罗斯等人"、"平克顿等人"),Sport = c(篮球"、篮球"、棒球"、舞蹈"、棒球"、舞蹈"、摔跤"、摔跤"、舞蹈"、棒球"、摔跤"、舞蹈"", "游泳", "游泳", "篮球", "篮球", "篮球", "篮球", "篮球", "游泳", "摔跤"),性别 = c("男"、"女"、"男"、"男"、"女"、"男"、"男"、"男"、"男"、"女"、"女"、"男"", "女", "女", "女", "男", "女", "女", "女", "男", "女"),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.15, 0.10.15)d_SE = c(.10, .04, .06, .01, .11, .08, .08, .04, .05, .05, .14, .07, .05, .08, .19, .16, .07, .16, .06, .18, .15),ci.low = c(-.30, .45, .00, -.01, .11, -.12, -.14, -.04, .16, .66, .84, .19, .68,1.03, .22, -.17, .17, .50, .00, -.23, .72),ci.high = c(.07, .62, .22, .05, .53, .20, .19, .11, .36, .87, 1.38, .47, .86, 1.35, .97,.47, .43, 1.11, .24, .46, 1.30))#reorder Author 基于 d 的值,因此可以按降序绘制效果大小example.df$Author<-reorder(example.df$Author,example.df$d,FUN=mean)

...然后是情节(没有任何 coord_flip() 用法):

p <- ggplot(example.df, aes(y = Author, x = d, xmin = ci.low, xmax = ci.high, shape=Gender)) +geom_point() +geom_errorbarh(height = .1) +scale_x_continuous(limits=c(-2,2),breaks=c(-2,-1.5,-1,-0.5,0,.5,1,1.5,2))+geom_vline(xintercept=0, color="grey60",linetype="dashed")+facet_grid(Sport ~ ., scales = "free", space = "free") +theme_bw() +主题(strip.text.y = element_text(角度= 0))p

非常好——感谢所有的建议和帮助解决这个情节!

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?

解决方案

Andrie's right, in that coord_flip() seems to be the root of the issue. However, the convention for forest plot formatting is to have the author names on y-axis, so I wanted to find a way that still would meet this formatting requirement.

The accepted answer in the post that Gregor commented on actually solves my issue; the only required change was that I had to calculate columns for upper-bound/lower-bound values of the confidence intervals.

So now with the updated data frame:

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),
                    ci.low = c(-.30, .45, .00, -.01, .11, -.12, -.14, -.04, .16, .66, .84, .19, .68, 1.03, .22, -.17, .17, .50, .00, -.23, .72),
                    ci.high = c(.07, .62, .22, .05, .53, .20, .19, .11, .36, .87, 1.38, .47, .86, 1.35, .97,.47, .43, 1.11, .24, .46, 1.30))

#reorder Author based on value of d, so effect sizes can be plotted in descending order
example.df$Author<-reorder(example.df$Author, example.df$d, FUN=mean)

...and then for the plot (without any coord_flip() usage):

p <- ggplot(example.df, aes(y = Author, x = d, xmin = ci.low, xmax = ci.high, shape=Gender)) +
geom_point() +
geom_errorbarh(height = .1) +
scale_x_continuous(limits=c(-2,2),breaks=c(-2,-1.5,-1,-0.5,0,.5,1,1.5,2))+
geom_vline(xintercept=0, color="grey60",linetype="dashed")+
facet_grid(Sport ~ ., scales = "free", space = "free") +
theme_bw() +
theme(strip.text.y = element_text(angle = 0))
p

Very nice--thanks for all the suggestions and help troubleshooting this plot!

这篇关于在 ggplot2 中,coord_flip 和自由比例尺不能一起工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆