更改分面标签大小的外观
Changing the Appearance of Facet Labels size
我知道有人在这里问了这个问题:Is there a way to increase the height of the strip.text bar in a facet?
我想降低 strip.text 栏的高度而不改变文本大小。在当前情况下,文本和条形栏墙之间总是有 space。
这是我到目前为止尝试过的方法,
library(gcookbook) # For the data set
library(ggplot2)
ggplot(cabbage_exp, aes(x=Cultivar, y=Weight)) + geom_bar(stat="identity") +
facet_grid(.~ Date) +
theme(strip.text = element_text(face="bold", size=9,lineheight=5.0),
strip.background = element_rect(fill="lightblue", colour="black",
size=1))
在我的例子中,即使更改为 5
,lineheight
似乎也没有任何影响。为什么?
如何使条形栏的尺寸变小一点,但保持文本尺寸不变?
在@Sandy Muspratt 回答后编辑
如果只有一排 facets
.
我们可以减小条带尺寸
g = ggplotGrob(p)
g$heights[c(3)] = unit(.4, "cm") # Set the height
grid.newpage()
grid.draw(g)
然而,在我的真实数据中,我有很多行如下图所示,当我更改 g$heights 的元素时,没有任何反应!
p = ggplot(cabbage_exp, aes(x=Cultivar, y=Weight)) + geom_bar(stat="identity") +
facet_wrap(~ Date,ncol = 1) +
theme(strip.text = element_text(face="bold", size=9),
strip.background = element_rect(fill="lightblue", colour="black",size=1))
g = ggplotGrob(p)
g$heights
# [1] 5.5pt 0cm 0.66882800608828cm #1null 0cm 0.193302891933029cm
# [7] 0.66882800608828cm 1null 0cm #0.193302891933029cm 0.66882800608828cm 1null
# [13] 0.456194824961948cm 0cm 1grobheight 5.5pt
然后我尝试更改 1,7 and 11
个元素
g$heights[c(3,7,11)] = unit(.4, "cm") # Set the height
grid.newpage()
grid.draw(g)
分面标签大小没有变化。
> g$heights
[1] 5.5pt 1grobheight
[3] sum(0.2cm, sum(0.15cm, 0.8128cm, 0cm, 0.15cm), 0.2cm)+0.2cm 0.2
[5] 1null 0cm
[7] 0.193302891933029cm 0.2
[9] 1null 0cm
[11] 0.193302891933029cm 0.2
[13] 1null 0cm
[15] 0.193302891933029cm 0.2
[17] 1null 0.456194824961948cm
[19] 0cm 1grobheight
[21] 5.5pt
使用边距
关于 ggplot2 ver 2.1.0:在 theme
中,在 strip_text
元素中指定边距(参见 here)。
library(ggplot2)
library(gcookbook) # For the data set
p = ggplot(cabbage_exp, aes(x=Cultivar, y=Weight)) + geom_bar(stat="identity") +
facet_grid(. ~ Date) +
theme(strip.text = element_text(face="bold", size=9),
strip.background = element_rect(fill="lightblue", colour="black",size=1))
p +
theme(strip.text.x = element_text(margin = margin(.1, 0, .1, 0, "cm")))
原答案更新为ggplot2 v2.2.0
您的 facet_grid 图表
这将降低条带的高度(如果需要,一直到零高度)。需要为一个条带和三个grobs设置高度。这将适用于您的特定 facet_grid 示例。
library(ggplot2)
library(grid)
library(gtable)
library(gcookbook) # For the data set
p = ggplot(cabbage_exp, aes(x=Cultivar, y=Weight)) + geom_bar(stat="identity") +
facet_grid(. ~ Date) +
theme(strip.text = element_text(face="bold", size=9),
strip.background = element_rect(fill="lightblue", colour="black",size=1))
g = ggplotGrob(p)
g$heights[6] = unit(0.4, "cm") # Set the height
for(i in 13:15) g$grobs[[i]]$heights = unit(1, "npc") # Set height of grobs
grid.newpage()
grid.draw(g)
您的 Facet_wrap 图表
页面下方有 3 个条带。所以要改的strip高度有3个,grob的高度有3个要改。
以下将适用于您的特定 facet_wrap 示例。
p = ggplot(cabbage_exp, aes(x=Cultivar, y=Weight)) + geom_bar(stat="identity") +
facet_wrap(~ Date,ncol = 1) +
theme(strip.text = element_text(face="bold", size=9),
strip.background = element_rect(fill="lightblue", colour="black",size=1))
g = ggplotGrob(p)
for(i in c(6,11,16)) g$heights[[i]] = unit(0.4,"cm") # Three strip heights changed
for(i in c(17,18,19)) g$grobs[[i]]$heights <- unit(1, "npc") # The height of three grobs changed
grid.newpage()
grid.draw(g)
如何找到相关的身高和身高?
g$heights
returns 高度向量。 1null 高度是绘图面板。条带高度是之前的一个 - 即 6, 11, 16.
g$layout
returns 一个数据框,最后一列中有 grob 的名称。需要改身高的是那些名字以"strip"开头的人。它们位于第 17、18、19 行。
概括一下
p = ggplot(cabbage_exp, aes(x=Cultivar, y=Weight)) + geom_bar(stat="identity") +
facet_wrap(~ Date,ncol = 1) +
theme(strip.text = element_text(face="bold", size=9),
strip.background = element_rect(fill="lightblue", colour="black",size=1))
g = ggplotGrob(p)
# The heights that need changing are in positions one less than the plot panels
pos = c(subset(g$layout, grepl("panel", g$layout$name), select = t))
for(i in pos) g$heights[i-1] = unit(0.4,"cm")
# The grobs that need their heights changed:
grobs = which(grepl("strip", g$layout$name))
for(i in grobs) g$grobs[[i]]$heights <- unit(1, "npc")
grid.newpage()
grid.draw(g)
每行多个面板
几乎可以使用相同的代码,即使标题和图例位于顶部。 pos
的计算发生了变化,但即使没有这种变化,代码也会运行。
library(ggplot2)
library(grid)
# Some data
df = data.frame(x= rnorm(100), y = rnorm(100), z = sample(1:12, 100, T), col = sample(c("a","b"), 100, T))
# The plot
p = ggplot(df, aes(x = x, y = y, colour = col)) +
geom_point() +
labs(title = "Made-up data") +
facet_wrap(~ z, nrow = 4) +
theme(legend.position = "top")
g = ggplotGrob(p)
# The heights that need changing are in positions one less than the plot panels
pos = c(unique(subset(g$layout, grepl("panel", g$layout$name), select = t)))
for(i in pos) g$heights[i-1] = unit(0.2, "cm")
# The grobs that need their heights changed:
grobs = which(grepl("strip", g$layout$name))
for(i in grobs) g$grobs[[i]]$heights <- unit(1, "npc")
grid.newpage()
grid.draw(g)
我知道有人在这里问了这个问题:Is there a way to increase the height of the strip.text bar in a facet?
我想降低 strip.text 栏的高度而不改变文本大小。在当前情况下,文本和条形栏墙之间总是有 space。
这是我到目前为止尝试过的方法,
library(gcookbook) # For the data set
library(ggplot2)
ggplot(cabbage_exp, aes(x=Cultivar, y=Weight)) + geom_bar(stat="identity") +
facet_grid(.~ Date) +
theme(strip.text = element_text(face="bold", size=9,lineheight=5.0),
strip.background = element_rect(fill="lightblue", colour="black",
size=1))
在我的例子中,即使更改为 5
,lineheight
似乎也没有任何影响。为什么?
如何使条形栏的尺寸变小一点,但保持文本尺寸不变?
在@Sandy Muspratt 回答后编辑
如果只有一排 facets
.
g = ggplotGrob(p)
g$heights[c(3)] = unit(.4, "cm") # Set the height
grid.newpage()
grid.draw(g)
然而,在我的真实数据中,我有很多行如下图所示,当我更改 g$heights 的元素时,没有任何反应!
p = ggplot(cabbage_exp, aes(x=Cultivar, y=Weight)) + geom_bar(stat="identity") +
facet_wrap(~ Date,ncol = 1) +
theme(strip.text = element_text(face="bold", size=9),
strip.background = element_rect(fill="lightblue", colour="black",size=1))
g = ggplotGrob(p)
g$heights
# [1] 5.5pt 0cm 0.66882800608828cm #1null 0cm 0.193302891933029cm
# [7] 0.66882800608828cm 1null 0cm #0.193302891933029cm 0.66882800608828cm 1null
# [13] 0.456194824961948cm 0cm 1grobheight 5.5pt
然后我尝试更改 1,7 and 11
个元素
g$heights[c(3,7,11)] = unit(.4, "cm") # Set the height
grid.newpage()
grid.draw(g)
分面标签大小没有变化。
> g$heights
[1] 5.5pt 1grobheight
[3] sum(0.2cm, sum(0.15cm, 0.8128cm, 0cm, 0.15cm), 0.2cm)+0.2cm 0.2
[5] 1null 0cm
[7] 0.193302891933029cm 0.2
[9] 1null 0cm
[11] 0.193302891933029cm 0.2
[13] 1null 0cm
[15] 0.193302891933029cm 0.2
[17] 1null 0.456194824961948cm
[19] 0cm 1grobheight
[21] 5.5pt
使用边距
关于 ggplot2 ver 2.1.0:在 theme
中,在 strip_text
元素中指定边距(参见 here)。
library(ggplot2)
library(gcookbook) # For the data set
p = ggplot(cabbage_exp, aes(x=Cultivar, y=Weight)) + geom_bar(stat="identity") +
facet_grid(. ~ Date) +
theme(strip.text = element_text(face="bold", size=9),
strip.background = element_rect(fill="lightblue", colour="black",size=1))
p +
theme(strip.text.x = element_text(margin = margin(.1, 0, .1, 0, "cm")))
原答案更新为ggplot2 v2.2.0
您的 facet_grid 图表
这将降低条带的高度(如果需要,一直到零高度)。需要为一个条带和三个grobs设置高度。这将适用于您的特定 facet_grid 示例。
library(ggplot2)
library(grid)
library(gtable)
library(gcookbook) # For the data set
p = ggplot(cabbage_exp, aes(x=Cultivar, y=Weight)) + geom_bar(stat="identity") +
facet_grid(. ~ Date) +
theme(strip.text = element_text(face="bold", size=9),
strip.background = element_rect(fill="lightblue", colour="black",size=1))
g = ggplotGrob(p)
g$heights[6] = unit(0.4, "cm") # Set the height
for(i in 13:15) g$grobs[[i]]$heights = unit(1, "npc") # Set height of grobs
grid.newpage()
grid.draw(g)
您的 Facet_wrap 图表
页面下方有 3 个条带。所以要改的strip高度有3个,grob的高度有3个要改。
以下将适用于您的特定 facet_wrap 示例。
p = ggplot(cabbage_exp, aes(x=Cultivar, y=Weight)) + geom_bar(stat="identity") +
facet_wrap(~ Date,ncol = 1) +
theme(strip.text = element_text(face="bold", size=9),
strip.background = element_rect(fill="lightblue", colour="black",size=1))
g = ggplotGrob(p)
for(i in c(6,11,16)) g$heights[[i]] = unit(0.4,"cm") # Three strip heights changed
for(i in c(17,18,19)) g$grobs[[i]]$heights <- unit(1, "npc") # The height of three grobs changed
grid.newpage()
grid.draw(g)
如何找到相关的身高和身高?
g$heights
returns 高度向量。 1null 高度是绘图面板。条带高度是之前的一个 - 即 6, 11, 16.
g$layout
returns 一个数据框,最后一列中有 grob 的名称。需要改身高的是那些名字以"strip"开头的人。它们位于第 17、18、19 行。
概括一下
p = ggplot(cabbage_exp, aes(x=Cultivar, y=Weight)) + geom_bar(stat="identity") +
facet_wrap(~ Date,ncol = 1) +
theme(strip.text = element_text(face="bold", size=9),
strip.background = element_rect(fill="lightblue", colour="black",size=1))
g = ggplotGrob(p)
# The heights that need changing are in positions one less than the plot panels
pos = c(subset(g$layout, grepl("panel", g$layout$name), select = t))
for(i in pos) g$heights[i-1] = unit(0.4,"cm")
# The grobs that need their heights changed:
grobs = which(grepl("strip", g$layout$name))
for(i in grobs) g$grobs[[i]]$heights <- unit(1, "npc")
grid.newpage()
grid.draw(g)
每行多个面板
几乎可以使用相同的代码,即使标题和图例位于顶部。 pos
的计算发生了变化,但即使没有这种变化,代码也会运行。
library(ggplot2)
library(grid)
# Some data
df = data.frame(x= rnorm(100), y = rnorm(100), z = sample(1:12, 100, T), col = sample(c("a","b"), 100, T))
# The plot
p = ggplot(df, aes(x = x, y = y, colour = col)) +
geom_point() +
labs(title = "Made-up data") +
facet_wrap(~ z, nrow = 4) +
theme(legend.position = "top")
g = ggplotGrob(p)
# The heights that need changing are in positions one less than the plot panels
pos = c(unique(subset(g$layout, grepl("panel", g$layout$name), select = t)))
for(i in pos) g$heights[i-1] = unit(0.2, "cm")
# The grobs that need their heights changed:
grobs = which(grepl("strip", g$layout$name))
for(i in grobs) g$grobs[[i]]$heights <- unit(1, "npc")
grid.newpage()
grid.draw(g)