订货

2017-05-06 78 views
0

我已经创建用下面的代码订货

一个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

在该代码中,我有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 
+0

感谢您的回答!这当然解决了我的订购问题。但是,该图现在不显示元效果的点。这是因为size = weight是在现在的单个geom_point()中设置的,并且对于元效果没有权重。我有shapegroup逻辑和两个geom_point()出于这个原因。任何想法我怎么既可以保持排序,并使所有的点显示? –

+0

后续行动:我决定只给元效应一个任意的权重,代码给了我想要的东西。 –