R 使用镶嵌面网格时如何更改镶嵌面边框的颜色
使用R 使用镶嵌面网格时如何更改镶嵌面边框的颜色,r,ggplot2,R,Ggplot2,使用facet\u grid时,ggplot2将构成facet变量的主要类别划分为比通常更宽的白线。这很好地满足了大多数目的。有时,我想更清楚地显示这些主要分类之间的划分,并想用另一种颜色对刻面划分进行着色。有办法吗?谢谢。试试这个--你可以用strip.background来控制它,用元素直接调用来格式化 qplot(高速、高速、数据=mpg)+ 平面网格(.~制造商)+ 主题(strip.text.x=元素\文本(大小=8, color=“红色”, 角度=90), strip.backgro
facet\u grid
时,ggplot2
将构成facet变量的主要类别划分为比通常更宽的白线。这很好地满足了大多数目的。有时,我想更清楚地显示这些主要分类之间的划分,并想用另一种颜色对刻面划分进行着色。有办法吗?谢谢。试试这个--你可以用strip.background
来控制它,用元素直接调用来格式化
qplot(高速、高速、数据=mpg)+
平面网格(.~制造商)+
主题(strip.text.x=元素\文本(大小=8,
color=“红色”,
角度=90),
strip.background=element_rect(fill=“darkblue”,
颜色=NA)
)
您可能需要使用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)
库(ggplot2)
图书馆(gtable)
图书馆(网格)
p虽然晚了一年,但我发现这是一个很容易解决的问题:
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.space
。因此,守则应为:
ggplot(mpg、aes(连续油管、高速公路、颜色=系数(年))+
几何点()+
分面网格(圆柱体~drv)+
主题(面板间距=单位(.05,“线条”),
panel.border=element_rect(color=“black”,fill=NA,size=1),
strip.background=元素(color=“black”,size=1))
这很好地为镶嵌面标签出现的矩形背景上色,但不会改变类别之间的间距,即沿着分割镶嵌面组的绘图长度的白线。这真是太棒了。我想知道这种方法是否允许修改ggplot
对象,从而生成另一个ggplot
对象,该对象可以在以后的步骤中添加ggplot
层。在一个不相关的注释中,我在ggplot
对象中添加脚注并将它们保持为ggplot
类时遇到了麻烦。我已经尝试了stackoverflow上其他地方列出的所有方法。gtable
可能是另一种方法吗?我想你的问题的答案应该是“是”<代码>gtable
允许对初始GGPlot进行精心修改。例如,可以将行和列添加到ggplot布局,然后将GROB插入其中@巴普蒂斯特在上一节课上做了一些笔记,我将用不同数量的刻面行/列绘制图。您能想出一种数据敏感的方法来指定Rmin
和Bmin
?也许所有方面布局都使用相同的设置,因此不需要其他参数?有没有一种方法可以生成另一个ggplot
对象,而不必直接打印?我已经在许多示例中尝试了您的代码,它们都非常有效。非常感谢你。我已经将您的代码添加为一个名为colorFacet
的新函数,该函数将在RHmisc
软件包的下一个版本中发布,您将被列为作者。这看起来是一个简单的解决方案,但我不认为它可以推广到所有情况。如果想要在面板之间留出更多空间,会发生什么?甚至是默认的面板边距?面板边界线的大小需要增加。然后在面板内外绘制较厚的边界线。不仅情节看起来丑陋,人们最终会失去信息;例如,如果有一条足够粗的线,顶排中间中间的右上角在边界线下消失。或者,除了黑色以外,还有一种颜色吗?好的,你可以通过调整变量中的变量来做几件事:*在EntEntReCt中改变颜色(颜色=黑色)……对于panel.border和strip.background*来说,通过增加panel.margin=unit(.05,“线”)来增加间距。为了解决粗线覆盖点的问题,您可以更改绘图的x&y限制将颜色更改为红色,并且所有边界线都是红色,而不仅仅是面板之间的空间。或者,将镶嵌面边距更改为0.5行。现在试着用颜色填充刻面分割。