2017-12-19 14 views
3

Ich lese in diesem Stapel Überlauf Frage eine kluge Möglichkeit zu simulieren Einstellung eines ästhetischen Panel-Hintergrund mit geom_rect.Wie kann man in ggplot2 einen ästhetischen Hintergrund an einen Panel-Hintergrund übergeben?

Conditionally change panel background with facet_grid?

Leider funktioniert es nicht, wenn Sie andere Farben in der Handlung setzen wollen. Die Farben mischen sich und die Legende wird verschmutzt. Stattdessen würde ich bevorzugen, dass die Farbe nur für den Hintergrund gilt und nicht gemischt wird. Meine andere Frage ist: Gibt es einen Ansatz, der in Polarkoordinaten funktionieren würde?

Für ein reproduzierbares Beispiel finden Sie den Code unten:

pies <- data_frame(pie = c(rep("hawaiian", 3), rep("pepperoni", 2)), 
        fraction = c(c(0.3, 0.2, 0.5), c(0.4, 0.6)), 
        ingredient = c("cheese", "pineapple", "ham", 
            "peperroni", "cheese"), 
        deepdish = c(rep(TRUE, 3), rep(FALSE, 2))) 

p <- pies %>% 
    ggplot() + 
    geom_bar(aes(x = factor(1), 
       y = fraction, 
       fill = ingredient), 
      width = 0.6, 
      stat = "identity", 
      position = "fill") + 
    facet_wrap(~ pie) + 
    geom_rect(mapping = aes(fill = deepdish), 
      alpha = 0.1, 
      xmin = -Inf, xmax = Inf, 
      ymin=-Inf, ymax=Inf, 
      show.legend = FALSE) 

p 
p + coord_polar(theta = "y") 

Antwort

3
pies <- data_frame(pie = c(rep("hawaiian", 3), rep("pepperoni", 2)), 
        fraction = c(c(0.3, 0.2, 0.5), c(0.4, 0.6)), 
        ingredient = c("cheese", "pineapple", "ham", 
            "peperroni", "cheese"), 
        deepdish = c(rep(TRUE, 3), rep(FALSE, 2))) 
library(ggplot2) 
library(dplyr) 
p <- pies %>% 
    ggplot() + 
    geom_bar(aes(x = factor(1), y = fraction, fill = ingredient), 
      width = 0.6, stat = "identity", position = "fill") + 
    facet_wrap(~ pie) + coord_polar(theta = "y") 

g <- ggplotGrob(p) 
# Set manually the background color for each panel 
g$grobs[[2]]$children[[1]]$children[[1]]$gp$fill <- "#88334466" 
g$grobs[[3]]$children[[1]]$children[[1]]$gp$fill <- "#44338866" 

library(grid) 
grid.draw(g) 

enter image description here

3
library(egg) 
library(grid) 

pies <- data.frame(pie = c(rep("hawaiian", 3), rep("pepperoni", 2)), 
        fraction = c(c(0.3, 0.2, 0.5), c(0.4, 0.6)), 
        ingredient = c("cheese", "pineapple", "ham", 
            "peperroni", "cheese")) 

dummy <- data.frame(x = 0, y = 0, 
        pie = c("hawaiian","pepperoni"), 
        deepdish = c("green","yellow"), stringsAsFactors = FALSE) 

p <- ggplot(pies) + 
    facet_wrap(~ pie) + 
    geom_custom(data= dummy, mapping = aes(x = factor(0), 
          y = y, 
          data = deepdish), 
       grob_fun = function(x) rectGrob(gp=gpar(fill=x,col=NA)), inherit.aes = TRUE) + 
    geom_bar(aes(x = factor(1), 
       y = fraction, 
       fill = ingredient), 
      width = 0.6, 
      stat = "identity", 
      position = "fill") 

p + coord_polar(theta = "y") 

enter image description here

Verwandte Themen