0

我用以下代码创建了一个森林图

虽然货币和空气质量方面/面板按我想要的顺序显示引文(根据因子水平),但元效应方面/面板没有。

我尝试移动两个 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
4

1 回答 1

0

在这段代码中,我有 1)创建数据框的方式略有不同以避免冗余列 2)删除了所有 shapegroup 逻辑 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
于 2017-05-06T23:53:54.817 回答