当使用facet_grid时如何改变分面边框的颜色?

19
在使用 facet_grid 时,ggplot2 会用比通常更宽的白线将组成分面变量的主要类别进行划分。这通常可以满足大多数目的。有时候我想更清楚地显示这些主要分类之间的界限,并希望用另一种颜色对分面进行着色。有没有办法做到这一点?谢谢。
3个回答

28

虽然有一年的延迟,但我发现这很容易解决:

ggplot(mpg, aes(cty, hwy, color = factor(year)))+ 
  geom_point()+
  facet_grid(cyl ~ drv) +
  theme(panel.margin=unit(.05, "lines"),
        panel.border = element_rect(color = "black", fill = NA, size = 1), 
        strip.background = element_rect(color = "black", size = 1))

更新 2021-06-01


ggplot2 3.3.3开始,属性panel.margin已被弃用,我们应该使用panel.spacing代替。因此,代码应为:

ggplot(mpg, aes(cty, hwy, color = factor(year)))+ 
  geom_point()+
  facet_grid(cyl ~ drv) +
  theme(panel.spacing = unit(.05, "lines"),
        panel.border = element_rect(color = "black", fill = NA, size = 1), 
        strip.background = element_rect(color = "black", size = 1))

facet wrap example


这看起来是一个简单的解决方案,但我并不认为它适用于所有情况。如果有人想要在面板之间获得更多的空间呢?或者即使是默认面板边距呢?需要增加面板边界线的大小。但是,然后粗边界线会被绘制在面板内部和外部。不仅绘图看起来难看,而且最终会丢失信息;例如,如果线条足够粗,在顶部行中间面板的右上角点将消失在边界线下面。 - Sandy Muspratt
或者,有人想要一种不是黑色的颜色? - Sandy Muspratt
1
好的,通过调整 theme() 中的变量,您可以做几件事情:
  • 更改 panel.borderstrip.background 的颜色,使用 element_rect(color = "black"...)
  • 通过增加 panel.margin=unit(.05, "lines") 来增加间距
为了解决粗线覆盖点的问题,您可以更改绘图的 x 和 y 轴限制。
- yake84
将颜色更改为红色,并且所有边界线都是红色的,而不仅仅是面板之间的空白处。 - Sandy Muspratt
或者,将面板边距更改为0.5行。现在尝试用颜色填充面板分区。 - Sandy Muspratt

2
你可能需要使用ggplot的布局表和gtable函数。
library(ggplot2)
library(gtable)
library(grid)

p <- ggplot(mtcars, aes(mpg, wt)) + geom_point() + 
     facet_grid(am ~ cyl)
## Get the plot grob
gt <- ggplotGrob(p)

## Check the layout
gtable_show_layout(gt)   # Vertical gaps are in columns 5 and 7
                         # and span rows 4 to 9
                         # Horizontal gap is in row 8
                         # and spans columns 4 to 9


## To automate the selection of the relevant rows and columns:
## Find the panels in the layout
## (t, l, b, r refer to top, left, bottom, right);
## The gaps' indices are one to the right of the panels' r index (except the right most panel);
## and one below the panels' b index (except the bottom most panel);
## Rmin and Rmax give the span of the horizontal gap;
## Bmin and Bmax give the span of the vertical gap;
## cols and rows are the columns and row indices of the gaps.

panels = subset(gt$layout, grepl("panel", gt$layout$name), t:r)

# The span of the horizontal gap
Rmin = min(panels$r)
Rmax = max(panels$r) + 1

#The span of the vertical gap
Bmin = min(panels$t) - 1
Bmax = max(panels$t)

# The columns and rows of the gaps
cols = unique(panels$r)[-length(unique(panels$r))] + 1
rows = unique(panels$t)[-length(unique(panels$t))] + 1

# The grob - orange rectangle
g = rectGrob(gp = gpar(col = NA, fill = "orange"))

## Add orange rectangles into the vertical and horizontal gaps
gt <- gtable_add_grob(gt, 
      rep(list(g), length(cols)),
      t=Bmin, l=cols, b=Bmax)

gt <- gtable_add_grob(gt, 
      rep(list(g), length(rows)),
      t=rows, l=Rmin, r=Rmax)

## Draw it
grid.newpage()
grid.draw(gt)

enter image description here


1
我认为你的问题的答案是“是”。gtable允许对初始ggplots进行一些复杂的修改。例如,可以向ggplot布局添加行和列,然后将grobs插入其中。@baptiste在https://github.com/baptiste/gtable/wiki/Description上整理了一些笔记。 - Sandy Muspratt
也许所有的facet布局都使用相同的设置,因此不需要其他参数?还有一种方法可以生成另一个ggplot对象,而不必直接打印吗? - Frank Harrell
我已经在多个示例上尝试了您的代码,它们都完美地工作了。非常感谢您。我已将您的代码添加为一个名为colorFacet的新函数,它将出现在下一个版本的R Hmisc包中,并将您列为作者。 - Frank Harrell
那有助于澄清。另一个问题是是否有一种方法可以获得ggplotgrid的结果,而不必仅仅打印结果对象。一些用户可能希望在最终打印之前更改图形对象或添加图层。 - Frank Harrell
@FrankHarrell,这篇帖子表明“可能是”,但不容易实现。 - Sandy Muspratt
显示剩余3条评论

0
试试这个——你可以使用strip.background来控制它,使用element_rect调用进行格式化。
qplot(hwy, cty, data = mpg) +
  facet_grid(. ~ manufacturer) +   
  theme(strip.text.x = element_text(size = 8,
                                    colour = "red", 
                                    angle = 90),
        strip.background = element_rect(fill = "darkblue",
                                        colour = NA)
  )

这将很好地为出现面标签的矩形背景着色,但不会改变类别之间的间隙 - 即沿绘图长度分隔面组的白线。 - Frank Harrell

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接