2013-10-18 161 views
38

有沒有一種方法可以根據數據框提供的變量填充facet_wrap創建的構麪條?ggplot2:facet_wrap基於數據集中變量的條紋顏色

實施例的數據:

MYdata <- data.frame(fruit = rep(c("apple", "orange", "plum", "banana", "pear", "grape")), farm = rep(c(0,1,3,6,9,12), each=6), weight = rnorm(36, 10000, 2500), size=rep(c("small", "large")))

例情節:

p1 = ggplot(data = MYdata, aes(x = farm, y = weight)) + geom_jitter(position = position_jitter(width = 0.3), aes(color = factor(farm)), size = 2.5, alpha = 1) + facet_wrap(~fruit)

我知道如何將條帶的背景色彩改變(例如橙色):

p1 + theme(strip.background = element_rect(fill="orange"))

facet_wrap and orange strip color

有沒有辦法在MYdata轉嫁值的變量size到參數fillelement_rect

基本上,而不是所有條的顏色1,我想小條水果(蘋果,李子,梨)的條形背景顏色爲綠色,大水果(橙色,香蕉,葡萄)的背景顏色爲紅色。

+0

有ISN儘管如此,在另一個網站上,這是一種簡單的方法,[之前已經詢問過](https://groups.google.com/forum/#!topic/ggplot2/fNBQrBPPbPM)。 – nograpes

+0

謝謝,nograpes。我看了看,卻找不到之前詢問過的地方。 – Dalmuti71

+0

如果你得到這個代碼的工作,你應該把它作爲一個答案。 – nograpes

回答

1

我很想知道該怎麼做,這是一個好主意。一個想法是用不同的顏色獨立生成每個圖表,然後使用諸如多槽或視口之類的東西並排顯示 - 這將需要更多的工作。

,如果你想提取的傳說,你將需要爲這種方法 - 這裏是哈德利一些代碼,我發現了一段時間後

g_legend<-function(a.gplot){ 
    tmp <- ggplot_gtable(ggplot_build(a.gplot)) 
    leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box") 
    legend <- tmp$grobs[[leg]] 
    return(legend)} 

看看它是如何從圖表p提取它,然後我將它從圖中取出 傳說< - g_legend(p) lwidth < - sum(legend $ width)#if你想根據這個定義視口 p < - p + theme(legend.position =「無「)

然後你最終繪製

grid.newpage() 
vp <- viewport(width = 1, height = 1) 
#print(p, vp = vp) 

submain <- viewport(width = 0.9, height = 0.9, x = 0.5, y = 1,just=c("center","top")) 
print(p, vp = submain) 
sublegend <- viewport(width = 0.5, height = 0.2, x = 0.5, y = 0.0,just=c("center","bottom")) 
print(arrangeGrob(legend), vp = sublegend) 

好運

46

隨着工作的一點點,你可以與有權grobs虛擬gtable結合您的情節,

enter image description here

d <- data.frame(fruit = rep(c("apple", "orange", "plum", "banana", "pear", "grape")), 
       farm = rep(c(0,1,3,6,9,12), each=6), 
       weight = rnorm(36, 10000, 2500), 
       size=rep(c("small", "large"))) 

p1 = ggplot(data = d, aes(x = farm, y = weight)) + 
    geom_jitter(position = position_jitter(width = 0.3), 
       aes(color = factor(farm)), size = 2.5, alpha = 1) + 
    facet_wrap(~fruit) 

dummy <- ggplot(data = d, aes(x = farm, y = weight))+ facet_wrap(~fruit) + 
    geom_rect(aes(fill=size), xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) + 
    theme_minimal() 

library(gtable) 

g1 <- ggplotGrob(p1) 
g2 <- ggplotGrob(dummy) 

gtable_select <- function (x, ...) 
{ 
    matches <- c(...) 
    x$layout <- x$layout[matches, , drop = FALSE] 
    x$grobs <- x$grobs[matches] 
    x 
} 

panels <- grepl(pattern="panel", g2$layout$name) 
strips <- grepl(pattern="strip_t", g2$layout$name) 
g2$layout$t[panels] <- g2$layout$t[panels] - 1 
g2$layout$b[panels] <- g2$layout$b[panels] - 1 

new_strips <- gtable_select(g2, panels | strips) 
grid.newpage() 
grid.draw(new_strips) 

gtable_stack <- function(g1, g2){ 
    g1$grobs <- c(g1$grobs, g2$grobs) 
    g1$layout <- transform(g1$layout, z= z-max(z), name="g2") 
    g1$layout <- rbind(g1$layout, g2$layout) 
    g1 
} 
## ideally you'd remove the old strips, for now they're just covered 
new_plot <- gtable_stack(g1, new_strips) 
grid.newpage() 
grid.draw(new_plot) 
+1

這只是嘲笑我的世界!一直試圖做這個年齡! –

+0

感謝!任何想法如何包含帶狀顏色的圖例? – jayelm

+0

非常有用,謝謝!這是我正在撰寫的一篇論文(並且我在最終將歸檔的代碼中引用了這個答案)。 – lukeholman