2017-12-19 11 views
3

このスタックオーバーフローの質問を読むには、を使用してパネルの背景に美的な設定をシミュレートする賢い方法があります。あなたはプロットに他の色を入れたい場合はggplot2でパネルの背景に美的感覚を渡す方法をシミュレートするには?

Conditionally change panel background with facet_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"), 
        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") 

答えて

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

:再現例えば

は、以下のコードを参照してください

関連する問題