2013-10-18 14 views
38

C'è un modo per riempire le strisce di facet create con facet_wrap in base a una variabile fornita con il frame di dati?ggplot2: colore striscia facet_wrap in base alla variabile nel set di dati

dati Esempio:

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")))

Esempio di scena:

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)

so come cambiare il colore delle strisce sfondo (ad esempio, ad arancione):

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

facet_wrap and orange strip color

C'è un modo per trasmettere i valori della variabile size in MYdata al parametro fill in element_rect?

Fondamentalmente, invece di 1 colore per tutte le strisce, vorrei che il colore di fondo della striscia di piccoli frutti (mela, prugna, pera) fosse verde e il colore di fondo di frutti grandi (arancia, banana, uva) fosse rosso .

+0

Ci isn 'un modo semplice per farlo, [è stato chiesto prima] (https://groups.google.com/forum/#!topic/ggplot2/fNBQrBPPbPM) su un altro sito però. – nograpes

+0

Grazie, nograpes. Ho guardato ma non ho potuto trovare dove era stato chiesto prima. – Dalmuti71

+0

Se si ottiene che il codice funzioni, è necessario pubblicarlo come risposta. – nograpes

risposta

1

Mi piacerebbe sapere come farlo, è una grande idea. Un'idea è quella di generare ogni grafico in modo indipendente con un colore diverso come si fa e quindi utilizzare qualcosa come il multiplo o le finestre per mostrare poi una accanto all'altra - richiederà un po 'più di lavoro.

se si desidera estrarre la leggenda, che sarà necessario per questo approccio - qui è un codice da Hadley che ho trovato un po 'indietro

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)} 

vedere come viene estratto dalla tabella di p, e poi ho preso fuori della trama leggenda < - g_legend (p) lwidth < - sum (leggenda larghezza $) #if si desidera definire la finestra sulla base di questa p < - p + tema (legend.position =" none ")

quindi alla fine disegnarlo

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) 

fortuna

46

buono con un po 'di lavoro, è possibile combinare la trama con un gtable manichino che ha le grobs giuste,

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

Questo solo [R] ha bloccato il mio mondo! Ho provato a farlo per secoli! –

+0

Grazie per questo! Qualche idea su come includere la legenda per i colori delle strisce? – jayelm

+0

Estremamente utile, grazie!Questo sta andando in un articolo che sto scrivendo in questo momento (e sto citando questa risposta nel codice che alla fine archivierò). – lukeholman

Problemi correlati