2017-04-09 177 views
2

受多重因素影響小面我有一個data.frame,我試圖在facetted方式與Rggplotgeom_boxplot繪製:顏色和ggplot

set.seed(1) 

vals <- rnorm(12) 
min.vals <- vals-0.5 
low.vals <- vals-0.25 
max.vals <- vals+0.5 
high.vals <- vals+0.25 


df <- data.frame(sample=c("c0.A_1","c0.A_2","c1.A_1","c1.A_2","c2.A_1","c2.A_2","c0.B_1","c0.B_2","c1.B_1","c1.B_2","c2.B_1","c2.B_2"), 
       replicate=rep(c(1,2),6),val=vals,min.val=min.vals,low.val=low.vals,max.val=max.vals,high.val=high.vals, 
       group=c(rep("A",6),rep("B",6)),cycle=rep(c("c0","c0","c1","c1","c2","c2"),2), 
       stringsAsFactors = F) 

在這個例子中,有兩個因素我想facet

facet.factors <- c("group","cycle") 
for(f in 1:length(facet.factors)) df[,facet.factors[f]] <- factor(df[,facet.factors[f]],levels=unique(df[,facet.factors[f]])) 
levels.vec <- sapply(facet.factors,function(f) length(levels(df[,f]))) 

但在其他情況下,我可能只有一個或兩個以上的因素。

有沒有辦法傳遞給facet_wrap要素的面向量和列數?

這裏是我試過了,這裏除了我創造了我自己的顏色對於每個因子水平:

library(RColorBrewer,quietly=T) 
library(scales,quietly=T) 
level.colors <- brewer.pal(sum(levels.vec),"Set2") 

require(ggplot2) 
ggplot(df,aes_string(x="replicate",ymin="min.val",lower="low.val",middle="val",upper="high.val",ymax="max.val",col=facet.factors,fill=facet.factors))+ 
    geom_boxplot(position=position_dodge(width=0),alpha=0.5,stat="identity")+ 
    facet_wrap(~facet.factors,ncol=max(levels.vec))+ 
    labs(x="Replicate",y="Val")+ 
    scale_x_continuous(breaks=unique(df$replicate))+ 
    scale_color_manual(values=level.colors,labels=unname(unlist(sapply(facet.factors,function(f) levels(df[,f])))),name="factor level")+scale_fill_manual(values=level.colors,labels=unname(unlist(sapply(facet.factors,function(f) levels(df[,f])))),name="factor level")+ 
    theme_bw()+theme(legend.position="none",panel.border=element_blank(),strip.background=element_blank(),axis.title=element_text(size=8)) 

這顯然引發此錯誤:

Error in combine_vars(data, params$plot_env, vars, drop = params$drop) : 
    At least one layer must contain all variables used for facetting 

顯然,這種工作原理:

ggplot(df,aes_string(x="replicate",ymin="min.val",lower="low.val",middle="val",upper="high.val",ymax="max.val",col=facet.factors,fill=facet.factors))+ 
    geom_boxplot(position=position_dodge(width=0),alpha=0.5,stat="identity")+ 
    facet_wrap(group~cycle,ncol=max(levels.vec))+ 
    labs(x="Replicate",y="Val")+ 
    scale_x_continuous(breaks=unique(df$replicate))+ 
    scale_color_manual(values=level.colors,labels=unname(unlist(sapply(facet.factors,function(f) levels(df[,f])))),name="factor level")+scale_fill_manual(values=level.colors,labels=unname(unlist(sapply(facet.factors,function(f) levels(df[,f])))),name="factor level")+ 
    theme_bw()+theme(legend.position="none",panel.border=element_blank(),strip.background=element_blank(),axis.title=element_text(size=8)) 

enter image description here

但它忽略了我傳遞的顏色,並沒有添加圖例,我想因爲我無法通過vectorcolfillaesthetics,顯然我必須對這個方面進行硬編碼。

這並不無論是對磨製問題工作:

ggplot(df,aes_string(x="replicate",ymin="min.val",lower="low.val",middle="val",upper="high.val",ymax="max.val",col=facet.factors,fill=facet.factors))+ 
     geom_boxplot(position=position_dodge(width=0),alpha=0.5,stat="identity")+ 
     facet_wrap(facet.factors[1]~facet.factors[2],ncol=max(levels.vec))+ 
     labs(x="Replicate",y="Val")+ 
     scale_x_continuous(breaks=unique(df$replicate))+ 
     scale_color_manual(values=level.colors,labels=unname(unlist(sapply(facet.factors,function(f) levels(df[,f])))),name="factor level")+scale_fill_manual(values=level.colors,labels=unname(unlist(sapply(facet.factors,function(f) levels(df[,f])))),name="factor level")+ 
     theme_bw()+theme(legend.position="none",panel.border=element_blank(),strip.background=element_blank(),axis.title=element_text(size=8)) 

所以我的問題是:1。 有沒有辦法通過一個載體來facet_wrap? 2.是否有一種方法可以通過一個因子矢量而不是單一的因子進行着色和填充?

回答

3

我們不能指定兩種顏色的着色/填充到單個框,我建議刻面變量粘貼在一起作爲着色/填充比例:

df$col.fill <- Reduce(paste, df[facet.factors]) 

facet_wrapfacets同時接受字符向量或一個雙邊公式:

facet.formula <- as.formula(paste('~', paste(facet.factors, collapse = '+'))) 

因此,代碼最後看起來是這樣的:

ggplot(df, 
     aes_string(
      x = "replicate", ymin = "min.val", ymax = "max.val", 
      lower = "low.val", middle = "val", upper = "high.val", 
      col = "col.fill", fill = "col.fill" 
     )) + 
    geom_boxplot(position = position_dodge(width = 0), 
       alpha = 0.5, 
       stat = "identity") + 
    facet_wrap(facet.factors, ncol = max(levels.vec)) + 
    # alternatively: facet_wrap(facet.formula, ncol = max(levels.vec)) + 
    labs(x = "Replicate", y = "Val") + 
    scale_x_continuous(breaks = unique(df$replicate)) + 
    theme_bw() + 
    theme(
     #legend.position = "none", 
     panel.border = element_blank(), 
     strip.background = element_blank(), 
     axis.title = element_text(size = 8) 
    ) 

不顯示圖例是因爲您添加了legend.position = "none",enter image description here

順便說一句,如果你在你的代碼添加一些空間和換行符它肯定會提高可讀性。