訂貨

2017-05-06 79 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

後續行動:我決定只給元效應一個任意的權重,代碼給了我想要的東西。 –