【问题标题】:Colors strips settings in faced-wrap ggplot面包装ggplot中的颜色条设置
【发布时间】:2023-03-19 01:22:01
【问题描述】:

致 3 年前的帖子 ggplot2: facet_wrap strip color based on variable in data set

巴蒂斯特给出了以下解决方案:

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)

  library(grid)
  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)

(我刚刚更新了“strip-t”模式并按照旧帖子中的建议打开了网格库)

我重新发布这个是因为它是一个古老的绝妙的东西,我想自己用它来做演示。 我是 ggplot 的初学者,这也可以帮助我编写各种脚本。

这是我的问题: - 请问如何选择颜色而不是相同的蓝色和红色?在我的脚本中,我设置了 3 种颜色,我希望它可以不那么激进。有可能做到吗? - 另一个问题,是否可以将其整合到图例中,即知道这些颜色指的是什么?

非常感谢

【问题讨论】:

    标签: ggplot2 facet-wrap


    【解决方案1】:

    您可以使用虚拟图中的填充比例更改条带颜色。结合图例有点棘手,但这是一个起点。

    library(ggplot2)
    library(gtable)
    library(gridExtra)
    library(grid)
    
    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
    }
    
    gtable_select <- function (x, ...) 
    {
      matches <- c(...)
      x$layout <- x$layout[matches, , drop = FALSE]
      x$grobs <- x$grobs[matches]
      x
    }
    
    
    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() + scale_fill_brewer(palette = "Pastel2") 
    
    
    g1 <- ggplotGrob(p1)
    g2 <- ggplotGrob(dummy)
    
    # extract legends
    leg <- g1$grobs[[grep("guide-box", g1$layout$name)]]
    dummy_leg <- g2$grobs[[grep("guide-box", g2$layout$name)]]
    combined_leg <- rbind.gtable(leg, dummy_leg)
    g1$grobs[[grep("guide-box", g1$layout$name)]] <- combined_leg
    
    # move dummy panels one cell up
    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)
    
    # stack new strips on top of gtable
    # 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)
    

    【讨论】:

    • 非常感谢,这个情节太棒了,是的,它有点棘手.. 也谢谢你的教学法!
    猜你喜欢
    • 1970-01-01
    • 2022-08-03
    • 2020-07-14
    • 2021-10-02
    • 1970-01-01
    • 2018-08-25
    • 2021-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多