2017-10-20 94 views
1

,我有以下數據:添加一個附加項目,傳說

trait,beta,se,p,analysis,signif 
trait1,0.078,0.01,9.00E-13,group1,1 
trait2,0.076,0.01,1.70E-11,group1,1 
trait3,-0.032,0.01,0.004,group1,0 
trait4,0.026,0.01,0.024,group1,0 
trait5,0.023,0.01,0.037,group1,0 
trait1,0.042,0.01,4.50E-04,group2,1 
trait2,0.04,0.01,0.002,group2,1 
trait3,0.03,0.01,0.025,group2,0 
trait4,0.025,0.01,0.078,group2,0 
trait5,0.015,0.01,0.294,group2,0 
trait1,0.02,0.01,0.078,group3,0 
trait2,0.03,0.01,0.078,group3,0 
trait3,0.043,0.01,1.90E-04,group3,0 
trait4,0.043,0.01,2.40E-04,group3,1 
trait5,0.029,0.01,0.013,group3,0 

,並與下面的代碼一個情節:

library(ggplot2) 
ggplot(GEE, aes(y=beta, x=reorder(trait, beta), group=analysis)) + 
    geom_point(data = GEE[GEE$signif == 1, ], 
      color="red", 
      shape = "*", 
      size=12, 
      show.legend = F) + 
    geom_point(aes(color=analysis)) + 
    geom_errorbar(aes(ymin=beta-2*se, ymax=beta+2*se,color=analysis), width=.2, 
       position=position_dodge(.2)) + 
    geom_hline(yintercept = 0) + 
    theme_light() + 
    theme(axis.title.y=element_blank(), 
     legend.title=element_blank()) + 
    coord_flip() 

這給了我下面的情節:

enter image description here

我想一個額外的元素添加到傳說,即紅色ASTE風險,我希望它說「重要」。我該如何去做呢?

PS。如果你喜歡這一段代碼,我與它的另一個問題,指定here :)

回答

7

添加虛擬aes()geom_point - 例如填充名爲顯著aes(fill = "Significant")

# Using OPs data 
library(ggplot2) 
ggplot(GEE, aes(y=beta, x=reorder(trait, beta), group=analysis)) + 
    geom_point(data = GEE[GEE$signif == 1, ], 
      color="red", 
      shape = "*", 
      size=12, 
      aes(fill = "Significant")) + 
    geom_point(aes(color=analysis)) + 
    geom_errorbar(aes(ymin=beta-2*se, ymax=beta+2*se,color=analysis), width=.2, 
       position=position_dodge(.2)) + 
    geom_hline(yintercept = 0) + 
    theme_light() + 
    theme(axis.title.y=element_blank(), 
     legend.title=element_blank()) + 
    coord_flip() + 
    guides(colour = guide_legend(order = 1), 
     fill = guide_legend(override.aes = list(size = 5))) + 
    theme(legend.margin = margin(-0.5,0,0,0, unit="cm")) 

PS:我也刪除show.legend = F從asterik geom_point

enter image description here

+0

太好了!謝謝!在現有的圖例下是否可以將星號和「顯着」兩個字對齊? – Abdel

+2

@Abdel完成:-)我加了'order'; 'override'對齊; 'legend.margin'減少空間 – PoGibas

+0

真棒,謝謝! :) – Abdel