2017-05-08 92 views
2

我產生的柱狀圖中描述我的數據的情況如下,並與下面的代碼段:顯示重疊的數據與GGPLOT2

enter image description here

mydf 
     Groups Gene_content Cases Intersection 
     25   903   Case1  512 
     25   817   Case2  512 
     20   722   Case1  400 
     20   644   Case2  400 
     15   543   Case1  332 
     15   469   Case2  332 
     10   357   Case1  172 
     10   287   Case2  172 
     5   184   Case1  65 
     5   125   Case2  65 
 ggplot(mydf, aes(fill=Cases, y=Gene_content, x=Groups)) +   
     geom_bar(position="dodge", stat="identity", color="black") +   
     geom_text(aes(label=Intersection), vjust=1.6, color="white",  
     position = position_dodge(0.9), size=3.5)+  
     geom_errorbar(aes(y = Intersection, ymin = Intersection,  
     ymax = Intersection), color="Orange",lty=2) +   
     scale_fill_brewer(palette="Set1")+  
     theme_minimal() 

我想要的是:首先用橙色線將所有酒吧的顏色用相同的顏色着色,然後區分酒吧到橙色的水平線,因爲他們已經ar即常見的顏色(黃色)意味着「常見」元素。此外,我會下移文字,指出有多少基因是共同的。所需的輸出(如修改與力點)如下: enter image description here

回答

2

看起來你可以爲Groups每個級別繪製黃條和標籤文本一次,而不是兩次。也許是這樣的:

ggplot(mydf, aes(fill=Cases, y=Gene_content, x=Groups)) +   
    geom_bar(position="dodge", stat="identity", color="black", width=3) + 
    geom_bar(data=mydf[!duplicated(mydf$Groups), ], stat="identity", 
      aes(Groups, Intersection), fill="yellow", width=3) + 
    geom_text(data=mydf[!duplicated(mydf$Groups), ], 
      aes(label=Intersection, y=Intersection - 20), color="black", size=3.5) +  
    # geom_errorbar(aes(y = Intersection, ymin = Intersection,  
    #     ymax = Intersection), color="Orange",lty=2) +   
    scale_fill_brewer(palette="Set1") +  
    theme_minimal() 

enter image description here

3

簡單的事情做的是頂配的高度y=Intersection添加geom_bar是另一個層。您可以通過在geom調用中更改y美學,輕鬆移動文本。

ggplot(aes(fill=Cases, y=Gene_content, x=factor(Groups))) +   
    geom_bar(position="dodge", stat="identity", color="black") + 
    geom_bar(position="dodge", stat="identity", aes(y = Intersection, group = Cases), 
      fill = "yellow", colour = "black") + 
    geom_text(aes(label=Intersection, y = Intersection), vjust=1.6, color="black",  
      position = position_dodge(0.9), size=3.5)+  
    geom_errorbar(aes(y = Intersection, ymin = Intersection,  
        ymax = Intersection), color="Orange",lty=2) +   
    scale_fill_brewer(palette="Set1")+ 
    theme_minimal() 
+1

或使用'nudge_y'。 – Axeman