我使用以下代码
创建了forest plot虽然货币和空气质量方面/面板按我想要的顺序显示引用(根据因子级别),但元效果方面/面板却没有。
我试过移动两个geom_point,发现只有第一个按因子顺序显示引用,第二个按字母顺序显示。我不确定为什么会这样。以及如何在此图表中获取两个图表以按我想要的顺序显示引文。
## Data for the forest plot ##
forestdat= data.frame(matrix(NA,nrow=9))
# citations
forestdat$cite = c('Joshi & Fast (2013, Study 1)','Current Study 1', 'Tost et al. (2015, Study 2)',
'Joshi & Fast (2013, Study 3)','Current Study 2', 'Heller & Ullrich (2017)','Monetary','Air Quality','Overall' )
citeorder = c('Monetary','Heller & Ullrich (2017)','Air Quality','Overall','Joshi & Fast (2013, Study 1)','Current Study 1', 'Tost et al. (2015, Study 2)',
'Joshi & Fast (2013, Study 3)','Current Study 2')
citeorderforplot = citeorder[9:1]
forestdat$cite = ordered(forestdat$cite, levels = citeorderforplot )
# However I change the order of the three meta effect in cite, they always come out alphabetically
# the same for the connection graphs
# effect size
forestdat$effectsize = c(-0.3,-0.002,0.12,-0.17,-0.016,-0.04,-0.037,-0.066,-0.048)
# lowerci
forestdat$lowerci = c(-0.479,-0.067,-0.001,-0.321,-0.081,-0.128,-0.168,-0.192,-0.126)
# upperci
forestdat$upperci = c(-0.121,0.063,0.241,-0.019,0.049,0.048,0.095,0.06,0.031)
forestdat$weight = c(0.253,0.414,0.333,0.268,0.381,0.351,NA,NA,NA)
# subgroups settings
forestdat$subgroup = c('Monetary','Monetary','Monetary','Air Quality','Air Quality','Air Quality','Meta Effect','Meta Effect','Meta Effect')
forestdat$subgroup = ordered(forestdat$subgroup,levels=c('Monetary','Air Quality','Meta Effect'))
# Shape of point
forestdat$shapegroup = c('Individual','Individual','Individual','Individual','Individual','Individual','Summary','Summary','Summary')
## ggplot ## ggplot theme
apatheme=theme_bw()+
theme(panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.border=element_blank(),
axis.line=element_line(),
legend.position='none')
library(ggplot2)
# forest plot ---------------------------------------------------------
p=ggplot(forestdat, aes(y=cite, x=effectsize, xmin=lowerci, xmax=upperci))+
#Add data points and color them black
geom_point(data=subset(forestdat,shapegroup != 'Summary'),color = 'black', shape = 15,
aes(size = weight))+
#Add 'special' points for the summary estimates, by making them diamond shaped
geom_point(data=subset(forestdat,shapegroup == 'Summary'), color='black', shape=18, size=4)+
#add the CI error bars
geom_errorbarh(height=.25)+
#Specify the limits of the x-axis and relabel it to something more meaningful
scale_x_continuous(limits=c(-0.6,0.3), name='Discount Rate Difference')+
#Give y-axis a meaningful label
#Add a vertical dashed line indicating an effect size of zero, for reference
geom_vline(xintercept=0, color='black', linetype='dashed')+
ylab("")+
#Create sub-plots (i.e., facets) based on levels of setting
#And allow them to have their own unique axes (so authors don't redundantly repeat)
#Apply my APA theme
facet_grid(subgroup~., scales= 'free', space='free')+
scale_size_area() +
apatheme
p
答案 0 :(得分:0)
在这段代码中我有1)创建数据帧有点不同以避免冗余列2)删除所有形状组逻辑3)将颜色和形状放在aes()中然后ggplot将改变颜色和形状由子组4 )添加了scale_color_manual和scale_shape_manual来指定颜色和形状
# Data for the forest plot ##
forestdat= data.frame(
# citations
cite = c('Joshi & Fast (2013, Study 1)','Current Study 1', 'Tost et al. (2015, Study 2)',
'Joshi & Fast (2013, Study 3)','Current Study 2', 'Heller & Ullrich (2017)','Monetary','Air Quality','Overall' )
)
citeorder = c('Monetary', 'Air Quality','Overall', 'Heller & Ullrich (2017)','Joshi & Fast (2013, Study 1)','Current Study 1', 'Tost et al. (2015, Study 2)',
'Joshi & Fast (2013, Study 3)','Current Study 2')
citeorderforplot = citeorder[9:1]
forestdat$cite = ordered(forestdat$cite, levels = citeorderforplot )
# However I change the order of the three meta effect in cite, they always come out alphabetically
# the same for the connection graphs
# effect size
forestdat$effectsize = c(-0.3,-0.002,0.12,-0.17,-0.016,-0.04,-0.037,-0.066,-0.048)
# lowerci
forestdat$lowerci = c(-0.479,-0.067,-0.001,-0.321,-0.081,-0.128,-0.168,-0.192,-0.126)
# upperci
forestdat$upperci = c(-0.121,0.063,0.241,-0.019,0.049,0.048,0.095,0.06,0.031)
forestdat$weight = c(0.253,0.414,0.333,0.268,0.381,0.351,NA,NA,NA)
# subgroups settings
forestdat$subgroup = c('Monetary','Monetary','Monetary','Air Quality','Air Quality','Air Quality','Meta Effect','Meta Effect','Meta Effect')
forestdat$subgroup = ordered(forestdat$subgroup,levels=c('Monetary','Air Quality','Meta Effect'))
## ggplot ## ggplot theme
apatheme=theme_bw()+
theme(panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.border=element_blank(),
axis.line=element_line(),
legend.position='none')
library(ggplot2)
# forest plot ---------------------------------------------------------
p=ggplot(forestdat, aes(y=cite, x=effectsize, xmin=lowerci, xmax=upperci))+
#Add data points with color and shape varying by sub group
geom_point(aes(color = subgroup, shape = subgroup, size = weight)) +
# add the error bae
geom_errorbarh(height=.25)+
#Specify the limits of the x-axis and relabel it to something more meaningful
scale_x_continuous(limits=c(-0.6,0.3), name='Discount Rate Difference')+
#Give y-axis a meaningful label
#Add a vertical dashed line indicating an effect size of zero, for reference
geom_vline(xintercept=0, color='black', linetype='dashed')+
ylab("")+
#Create sub-plots (i.e., facets) based on levels of setting
#And allow them to have their own unique axes (so authors don't redundantly repeat)
#Apply my APA theme
facet_grid(subgroup~., scales= 'free', space='free')+
scale_size_area() +
scale_color_manual(values = c("red", "blue", "green")) +
scale_shape_manual(values = c(15, 18, 19)) +
apatheme
p