2016-11-07 92 views
1

我有一個樣本數據幀:彩色文本由R中說明的顏色GGPLOT2

dput(數據)

structure(list(DAF = c(0.00704225, 0.00352113, 0.00352113, 0.028169, 
0.00352113, 0.00704225, 0.0105634, 0.00352113, 0.0105634, 0.00352113, 
0.00352113, 0.00352113, 0.0176056, 0.0140845, 0.00352113, 0.0140845, 
0.00352113, 0.0105634, 0.00352113, 0.00352113, 0.0140845, 0.00352113, 
0.084507, 0.00352113, 0.0669014, 0.00704225, 0.00352113, 0.00352113, 
0.00704225, 0.00352113, 0.00704225, 0.00352113, 0.00352113, 0.028169, 
0.00352113, 0.00704225, 0.0105634, 0.00352113, 0.0105634, 0.00352113, 
0.00352113, 0.00352113, 0.0176056, 0.0140845, 0.00352113, 0.0140845, 
0.00352113, 0.0105634, 0.00352113, 0.00352113, 0.0140845, 0.00352113, 
0.084507, 0.00352113, 0.0669014, 0.00704225, 0.00352113, 0.00352113, 
0.00704225, 0.00352113), TYPE = structure(c(2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("All SVs", "bDEL"), class = "factor"), 
Function = structure(c(2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 
2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 
2L, 2L, 2L, 2L, 2L, 1L), .Label = c("Genic", "Intergenic" 
), class = "factor")), .Names = c("DAF", "TYPE", "Function" 
), class = "data.frame", row.names = c(NA, -60L)) 

我已使用」

p1<-ggplot(sv,aes(x=DAF,y=..density..,fill=Function))+geom_histogram(position="dodge",binwidth=0.02)+facet_wrap(~ TYPE, scales = "free",ncol=2) 

從註釋文本繪製數據幀:

> dput(dat) 
structure(list(x = c(0.05, 0.05, 0.05, 0.05, 0.05, 0.05), y = c(20L, 
17L, 14L, 35L, 30L, 25L), labs = structure(c(3L, 4L, 6L, 1L, 
2L, 5L), .Label = c("mean=0.0173", "mean=0.0190", "mean=0.0415", 
"mean=0.0440", "p=0.0393", "p=1.47e-08"), class = "factor"), 
    TYPE = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("All SVs", 
    "bDEL"), class = "factor")), .Names = c("x", "y", "labs", 
"TYPE"), class = "data.frame", row.names = c(NA, -6L)) 


p2<-p1+geom_text(aes(x, y, label=labs),size=1,data=dat,inherit.aes = F) 

它給出下面的情節:

enter image description here

但是,我需要通過傳說的相應的顏色上色文本「的意思= ...」每個面內。即所有的SV,'平均= 0.0415'應該是在「基因」顏色和「平均= 0.0440」在'間隔'顏色

+1

你怎麼知道哪個函數的顏色不在'dat'中是哪個函數的顏色? –

回答

2

你需要添加一列到「dat」來表示什麼組每一行/標籤應該是。我用NA的p值。

dat$Function = c("Genic", "Intergenic", NA, "Genic", "Intergenic", NA) 

然後你就可以在geom_text這個變量來color地圖。你能避免使用show.legend = FALSE加入NA傳說。

p1 + geom_text(aes(x, y, label = labs, color = Function), 
       size = 1, data = dat, inherit.aes = FALSE, show.legend = FALSE) 

NA顏色默認爲grey50,如果需要,您可以將其更改爲scale_color_discrete

p1 + geom_text(aes(x, y, label = labs, color = Function), 
       size = 1, data = dat, inherit.aes = FALSE, show.legend = FALSE) + 
    scale_color_discrete(na.value = "black") 
+0

它的工作原理! 1.如何更改帶有標題「All SVs」和「bDEL」的條寬度,我試過'strip.background = element_rect(size = 0.5)',這沒有任何效果!! 2.可以爲每個方面添加標題,如a,b,c,d ...這是正常使用ggtitle() – chas

+0

@chas我認爲帶的寬度將始終與圖一樣寬,但您可以更改這些標題和/或通過一些'theme'參數一起刪除strip背景,您可以通過'facet_wrap'中的'labeller'添加/更改標題,或者通過改變用作facet變量的因子的級別。 – aosmith