gpt4 book ai didi

r - 在 ggplot2 中,coord_flip 和自由比例尺不能一起工作

转载 作者:行者123 更新时间:2023-12-04 03:52:41 25 4
gpt4 key购买 nike

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

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))

数据框包含作者姓名、运动、样本是男性还是女性、干预的效果大小以及效果大小的标准误差。我希望创建一个点图映射形状到性别,并根据特定运动进行分面。在遵循 Chang 的“食谱”和 this related query 中的示例之后,我想出了以下代码,可以满足我的大部分格式化需求:
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

然而,我的问题是,每个方面(下面)的结果图将整个数据框中的每个作者都绘制在 y 轴(技术上是 x 轴,但轴被翻转)上。相反,我只希望将具有与给定方面相关数据的作者绘制在该方面的作者关联轴上,因此每个方面都应在轴上具有不同的作者列表。

enter image description here

我还以为 scales="free_y" facet_grid 的组件layer 将确保每个方面都有一个唯一的作者轴(我也尝试过 scales="free_x" ,考虑到倒置的轴),但这并没有达到预期的效果。

有谁知道我可以确保出现在每个构面轴上的唯一作者姓名是具有该构面相关数据的作者姓名的方法吗?

最佳答案

安德里是对的,在那 coord_flip()似乎是问题的根源。但是,森林图格式的约定是在 y 轴上显示作者姓名,因此我想找到一种仍然可以满足此格式要求的方法。

post 中接受的答案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 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)

...然后是情节(没有任何 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() +
theme(strip.text.y = element_text(angle = 0))
p

enter image description here

非常好-感谢所有建议并帮助解决此情节!

关于r - 在 ggplot2 中,coord_flip 和自由比例尺不能一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25052000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com