ggplot中一个面的多种颜色STRIP背景

hlswsv35  于 2023-06-19  发布在  其他
关注(0)|答案(4)|浏览(169)

2023年更新:

现在有一个很棒的包,ggh4x by @teunbrand
相关问题见this answer

Q:在ggplot中,如何根据组修改面背景的颜色?

面背景可以有一种以上的颜色吗?

示例:

我使用facet_grid(不是facet_wrap)和多层。

## Sample data
dat <- mtcars
## Add in some colors based on the data
dat$facet_fill_color <- c("red", "green", "blue", "yellow", "orange")[dat$gear]

## Create main plot
library(ggplot2)
P <- 
  ggplot(dat, aes(x = cyl, y = wt)) + 
  geom_point(aes(fill = hp)) + 
  facet_grid(gear + carb ~ .)

所有条带的背景可使用以下命令更改:

P + theme(strip.background = element_rect(fill="red"))

主要挑战:

但是,我想为不同的群体改变不同的颜色。
类似以下的东西(当然这是行不通的)

P + theme(strip.background = element_rect(fill=dat$facet_fill_color))
P + theme(strip.background = element_rect(aes(fill=facet_fill_color)))
ve7v8dk2

ve7v8dk21#

无论如何,适应以前的gtable hack是非常直接的。

## Sample data
require(ggplot2)
dat <- mtcars
## Add in some colors based on the data
dat$facet_fill_color <- c("red", "green", "blue", "yellow", "orange")[dat$gear]

## Create main plot
p <- ggplot(dat, aes(x=cyl, y=wt)) + 
  geom_point(aes(fill=hp)) + facet_grid(gear+carb ~ .) +
  theme(strip.background=element_blank())

dummy <- p
dummy$layers <- NULL
dummy <- dummy + geom_rect(data=dat, xmin=-Inf, ymin=-Inf, xmax=Inf, ymax=Inf,
                           aes(fill = facet_fill_color))

library(gtable)

g1 <- ggplotGrob(p)
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-right", g2$layout$name)
g2$grobs[strips] <- replicate(sum(strips), nullGrob(), simplify = FALSE)
g2$layout$l[panels] <- g2$layout$l[panels] + 1
g2$layout$r[panels] <- g2$layout$r[panels] + 2

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

gywdnpxw2#

这感觉就像一个可怕的黑客的事情做(到某种程度上,我几乎不好意思张贴这作为一个 * 答案 *),但它是可能的...

require(ggplot2);require(grid)

# Facet strip colours
cols <- rep( c("red", "green", "blue", "yellow", "orange")[rep( c(4,3,5),times=c(4,3,4))] , 2 )

# Make a grob object
Pg <- ggplotGrob(P)

# To keep track of strip.background grobs
idx <- 0 

# Find each strip.background and alter its backround colour...
for( g in 1:length(Pg$grobs) ){
    if( grepl( "strip.absoluteGrob" , Pg$grobs[[g]]$name ) ){
        idx <- idx + 1
        sb <- which( grepl( "strip\\.background" , names( Pg$grobs[[g]]$children ) ) )
        Pg$grobs[[g]]$children[[sb]][]$gp$fill <- cols[idx]

    }
}

# Plot
grid.newpage()
grid.draw(Pg)

slmsl1lt

slmsl1lt3#

受@SimonO'Hanlon的解决方案启发的替代搜索和替换策略,

strips <- grep(pattern="strip-right", Pg$layout$name)

refill <- function(strip, colour){
  strip[["children"]][[1]][["gp"]][["fill"]] <- colour
  strip
}
cols <- rep_len(rep.int(c("blue", "green", "red"), c(4,3,4)), length(strips))
Pg$grobs[strips] <- mapply(refill, 
                           strip = Pg$grobs[strips], 
                           colour = cols,
                           SIMPLIFY = FALSE)

grid.newpage()
grid.draw(Pg)
jvlzgdj9

jvlzgdj94#

有一个非常好和简单的答案隐藏在类似的问题ggplot2: facet_wrap strip color based on variable in data set中,避免了黑客攻击,尽管你仍然需要在strip_themed()中手动指定你想要的颜色。

library(ggh4x)
#> Loading required package: ggplot2

# Only colour strips in x-direction
strip <- strip_themed(background_x = elem_list_rect(fill = rainbow(7)))

# Wrap variant
ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  facet_wrap2(~ class, strip = strip)

相关问题