Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在ggplot2中的镶嵌面之间添加彩色边框_R_Ggplot2 - Fatal编程技术网

R 在ggplot2中的镶嵌面之间添加彩色边框

R 在ggplot2中的镶嵌面之间添加彩色边框,r,ggplot2,R,Ggplot2,我希望在每个面之间添加一条深灰色的粗条。我不希望在每个方面都有一个边界,而是在情节之间有一个深灰色的条带 library(ggplot2) ggplot(mpg, aes(cty, hwy, color = factor(year))) + geom_point() + facet_wrap(~ cyl, nrow = 1) 我发现了一个问题,尽管我的数据与上面的示例相同,但只有一行,而不是一个面网格。这是基于您链接的问题的未接受答案。我认为公认的答案不是最好的。它在每个绘图面板

我希望在每个面之间添加一条深灰色的粗条。我不希望在每个方面都有一个边界,而是在情节之间有一个深灰色的条带

library(ggplot2)

ggplot(mpg, aes(cty, hwy, color = factor(year))) + 
  geom_point() +
  facet_wrap(~ cyl, nrow = 1) 

我发现了一个问题,尽管我的数据与上面的示例相同,但只有一行,而不是一个面网格。

这是基于您链接的问题的未接受答案。我认为公认的答案不是最好的。它在每个绘图面板和条带周围添加了彩色边框。镶嵌面_包裹面板之间的列数与镶嵌面_网格面板之间的列数不同;因此,对镶嵌面_包裹图进行了轻微调整


这是基于您链接的问题的未接受答案。我认为公认的答案不是最好的。它在每个绘图面板和条带周围添加了彩色边框。镶嵌面_包裹面板之间的列数与镶嵌面_网格面板之间的列数不同;因此,对镶嵌面_包裹图进行了轻微调整


没有主题元素,所以它会涉及grid/gridExtra黑客攻击。没有主题元素,所以它会涉及grid/gridExtra黑客攻击。
library(ggplot2)
library(grid)
library(gtable)

p = ggplot(mpg, aes(cty, hwy, color = factor(year))) + 
  geom_point() +
  facet_wrap(~ cyl, nrow = 1) 

gt <- ggplotGrob(p)


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

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

# The columns of the gaps (two to the right of the panels
cols = unique(panels$r)[-length(unique(panels$r))] + 2

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

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

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