2017-03-10 76 views
1

我有我從做了如下箱線圖:我想填充只有特定的顏色在箱線GGPLOT2

#Data 
set.seed(395) 
Env<- data.frame(CO2= c(cumsum(rnorm(1*36)), cumsum(rnorm(1*36))), 
        Group= rep(c("A","B"), each=36), 
        Segment=rep(seq(1,12),each=36)) 

ggplot(data=subset(Env, !is.na(Segment)),aes(factor(Segment),CO2))+ 
    geom_boxplot()+ 
    facet_wrap(~ Group)+ 
    theme_bw()+ 
    labs(x="Time segment", y="CO2") 

enter image description here

上色3日,9日和12箱線圖的特定顏色:綠色,紅色和紫色。這可能嗎?

我嘗試沒有成功如下:

scale_fill_manual(values = c("", "","green","","","","","","red","","","purple")) 

例如像這樣:

enter image description here

+0

這是更難幫助繪製的問題,如果你不提供[重複的例子(http://stackoverflow.com/questions/5963269/how-to-make具有樣本輸入數據的可重複示例)。 – MrFlick

回答

3

要做到這一點,最簡單的方法是:

  1. 將數據幀添加一列與要使用
  2. 地圖的列中的顏色012gggplot審美。
  3. 使用+ scale_fill_identity()將列的值映射到它們表示的顏色。
set.seed(395) 
Env<- data.frame(CO2= c(cumsum(rnorm(1*36)), cumsum(rnorm(1*36))), 
        Group= rep(c("A","B"), each=36), 
        Segment=rep(seq(1,12),each=36)) 

Env$fillcol <- NA 
Env$fillcol[Env$Segment %in% c(3,9,12) & Env$Group == "A"] <- "red" 
Env$fillcol[Env$Segment %in% c(2,10,12) & Env$Group == "B"] <- "blue" 

ggplot(data=subset(Env, !is.na(Segment)),aes(x = factor(Segment), y = CO2, fill = fillcol)) + 
    geom_boxplot()+ 
    facet_wrap(~ Group)+ 
    theme_bw()+ 
    labs(x="Time segment", y="CO2") + 
    scale_fill_identity() 

Result