我有一个带有灰色小平面和白色小平面文字的白色背景上的情节:
ggplot(data = data.frame(x = rep(1:2, 2), y = rep(1:2,2), color = c("Ap", "Ap", "B", "B")),
aes(x = x, y = y, color = color)) +
geom_point() +
facet_grid(~color) +
theme(panel.background = element_blank(),
strip.text = element_text(color = "white", size = 23))
Run Code Online (Sandbox Code Playgroud)

我的问题是下行(p,g,q,j)越过了方面.我希望条带背景在文本周围有一个边距,以便构面文本中的所有字形始终严格在构面矩形内.我可以在方面文本中添加换行符,color = c("Ap\n", "Ap\n", "B\n", "B\n")但边距太大(或者lineheight要求太难看了).这有ggplot2解决方案吗?
对于ggplot v2.2.0 In theme,在strip_text元素中指定边距(参见此处)
# Set text size
size = 26
library(ggplot2)
library(grid)
p = ggplot(data = data.frame(x = rep(1:2, 2), y = rep(1:2,2), color = c("Ap", "Ap", "B", "B")),
aes(x = x, y = y, color = color)) +
geom_point() +
facet_grid(~color) + theme_bw() +
theme(strip.text = element_text(color = "white", size = size))
p +
theme(strip.text.x = element_text(margin = margin(.1, 0, .3, 0, "cm")))
Run Code Online (Sandbox Code Playgroud)
原始
您可以使用ggplot布局来调整条带的高度.例如,高度可以设置为绝对高度,unit(1, "cm")或者,正如我在这里所做的那样,设置为调整为字体大小的高度.
编辑:更新到ggplot2 2.0.0
进一步编辑: grid:::unit.list()不再需要更新到网格3.0.0.
# Set text size
size = 26
library(ggplot2)
library(grid)
p = ggplot(data = data.frame(x = rep(1:2, 2), y = rep(1:2,2), color = c("Ap", "Ap", "B", "B")),
aes(x = x, y = y, color = color)) +
geom_point() +
facet_grid(~color) + theme_bw() +
theme(strip.text = element_text(color = "white", size = size))
# Get ggplot grob
g <- ggplotGrob(p)
# Set the relevant height
g$heights[3] = unit(2, "grobheight", textGrob("a", gp=gpar(fontsize = size)))
grid.newpage()
grid.draw(g)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
102 次 |
| 最近记录: |