如何更改单个facet_wrap面板的格式?

发布于 2024-11-25 05:35:57 字数 211 浏览 1 评论 0原文

是否可以改变单个方面图的格式?例如,使用下面的示例代码,可以更改 cyl=8 图的标题或背景颜色吗?

library(ggplot2)
ggplot(mtcars, aes(x=gear)) + 
  geom_bar(aes(y=gear), stat="identity", position="dodge") +
  facet_wrap(~cyl)

Is it possible to alter the format of an individual facet plot? For example, using the sample code below, can one change the color of the title or background for the cyl=8 plot?

library(ggplot2)
ggplot(mtcars, aes(x=gear)) + 
  geom_bar(aes(y=gear), stat="identity", position="dodge") +
  facet_wrap(~cyl)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦幻的味道 2024-12-02 05:35:57

您可以修改 ggplot2 grobs,例如:

library("ggplot2")
d <- ggplot(mtcars, aes(x=gear)) + 
       geom_bar(aes(y=gear), stat="identity", position="dodge") +
       facet_wrap(~cyl)

grob <- ggplotGrob(d)
strip_bg <- grid.ls(getGrob(grob, "strip.background.rect",
                            grep=TRUE, global=TRUE))$name
panel_bg <- grid.ls(getGrob(grob, "panel.background.rect",
                            grep=TRUE, global=TRUE))$name
strip_text <- grid.ls(getGrob(grob, "strip.text.x",
                              grep=TRUE, global=TRUE))$name
grob <- geditGrob(grob, strip_bg[2], gp=gpar(fill="gray60"))
grob <- geditGrob(grob, panel_bg[2], gp=gpar(fill="darkolivegreen2"))
grob <- geditGrob(grob, strip_text[2], gp=gpar(col="white"))
grid.draw(grob)

geditGrob example

更新:这应该适用于 ggplot2 0.9.3

grob <- ggplotGrob(d)

elem <- grob$grobs$panel2
panel_bg <- grid.ls(getGrob(elem, "panel.background.rect", grep=TRUE))$name
grob$grobs$panel2 <- editGrob(elem, panel_bg, gp=gpar(fill="darkolivegreen"), grep=TRUE)

elem <- grob$grobs$strip_t.1
strip_bg <- grid.ls(getGrob(elem, "strip.background.rect", grep=TRUE))$name
grob$grobs$strip_t.1 <- editGrob(elem, strip_bg, gp=gpar(fill="gray60"), grep=TRUE)

elem <- grob$grobs$strip_t.1
strip_text <- grid.ls(getGrob(elem, "strip.text.x.text", grep=TRUE))$name
grob$grobs$strip_t.1 <- editGrob(elem, strip_text, gp=gpar(col="white"), grep=TRUE)

grid.draw(grob)

You can modify the ggplot2 grobs, for instance:

library("ggplot2")
d <- ggplot(mtcars, aes(x=gear)) + 
       geom_bar(aes(y=gear), stat="identity", position="dodge") +
       facet_wrap(~cyl)

grob <- ggplotGrob(d)
strip_bg <- grid.ls(getGrob(grob, "strip.background.rect",
                            grep=TRUE, global=TRUE))$name
panel_bg <- grid.ls(getGrob(grob, "panel.background.rect",
                            grep=TRUE, global=TRUE))$name
strip_text <- grid.ls(getGrob(grob, "strip.text.x",
                              grep=TRUE, global=TRUE))$name
grob <- geditGrob(grob, strip_bg[2], gp=gpar(fill="gray60"))
grob <- geditGrob(grob, panel_bg[2], gp=gpar(fill="darkolivegreen2"))
grob <- geditGrob(grob, strip_text[2], gp=gpar(col="white"))
grid.draw(grob)

geditGrob example

Update: This should work with ggplot2 0.9.3

grob <- ggplotGrob(d)

elem <- grob$grobs$panel2
panel_bg <- grid.ls(getGrob(elem, "panel.background.rect", grep=TRUE))$name
grob$grobs$panel2 <- editGrob(elem, panel_bg, gp=gpar(fill="darkolivegreen"), grep=TRUE)

elem <- grob$grobs$strip_t.1
strip_bg <- grid.ls(getGrob(elem, "strip.background.rect", grep=TRUE))$name
grob$grobs$strip_t.1 <- editGrob(elem, strip_bg, gp=gpar(fill="gray60"), grep=TRUE)

elem <- grob$grobs$strip_t.1
strip_text <- grid.ls(getGrob(elem, "strip.text.x.text", grep=TRUE))$name
grob$grobs$strip_t.1 <- editGrob(elem, strip_text, gp=gpar(col="white"), grep=TRUE)

grid.draw(grob)
梦初启 2024-12-02 05:35:57

我知道这是一个老问题,所以原始海报可能早已消失,但我仍然认为它值得回答,作为未来搜索者的资源。 rcs 接受的答案确实有效,但我发现它相当不稳定且很糟糕。最后我决定采取更温和但更稳定的方法。使用这种方法,您只能更改背景,但这足以满足我的目的,也可能适合其他人,我的方法是使用 geom_rect 重新着色背景,如下所示:

highlights <- data.frame(cyl=c(8))

ggplot() + 
  geom_rect(data=highlights,aes(xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf), fill='red', alpha=0.2) +
  geom_bar(data = mtcars, aes(x=gear), position="dodge", fill = 'black') +
  facet_wrap(~cyl)

示例图

I know this is an old question so the original poster is probably long gone but I still think it is worth answering as a resource to future searchers. The accepted answer from rcs does work, but I found it to be rather unstable and hacky. In the end I decided that a more modest but more stable approach is in order. With this method you can only change the background but that suffices for my purposes and might for others, my approach is to use geom_rect to recolour the background, like so:

highlights <- data.frame(cyl=c(8))

ggplot() + 
  geom_rect(data=highlights,aes(xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf), fill='red', alpha=0.2) +
  geom_bar(data = mtcars, aes(x=gear), position="dodge", fill = 'black') +
  facet_wrap(~cyl)

Example plot

心欲静而疯不止 2024-12-02 05:35:57

这可能会帮助您更接近您想要的:

mtcars2 = subset(mtcars, cyl != 8)
   subs = subset(mtcars, cyl == 8)

require(ggplot2)
ggplot(mtcars2, aes(x=gear)) + 
    geom_bar(aes(y=gear, fill = 'black'), stat="identity", position="dodge") +
    geom_bar(data = subs, aes(x = gear), fill = 'blue', binwidth = 1) +
    facet_wrap(~cyl)

This might help you get a little bit closer to what you want:

mtcars2 = subset(mtcars, cyl != 8)
   subs = subset(mtcars, cyl == 8)

require(ggplot2)
ggplot(mtcars2, aes(x=gear)) + 
    geom_bar(aes(y=gear, fill = 'black'), stat="identity", position="dodge") +
    geom_bar(data = subs, aes(x = gear), fill = 'blue', binwidth = 1) +
    facet_wrap(~cyl)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文