如何使用 ggtext element_textbox 包裹分面标签
How to wrap facet labels using ggtext element_textbox
我正在使用 ggtext
中的 element_textbox_simple()
在我的 ggplot2
主题中包装分面标签。但是条带高度似乎并没有随着额外的行而增加。如何让文本框正确换行?
我考虑过在 facet_grid()
中使用 labeller = label_wrap_gen()
参数,但我觉得 element_textbox()
更适用于不同方面 sizes/counts。
library(ggplot2)
library(ggtext)
mpg2 <- mpg
mpg2$drv[mpg2$drv == '4'] <- 'This is a long way of writing Four-weel drive vehicle'
mpg2$cyl[mpg2$cyl == 8] <- 'This is a long way of writing Eight-cylinder vehicle, which is very powerful'
ggplot(mpg2, aes(displ, cty)) +
geom_point() +
facet_grid(vars(drv), vars(cyl)) +
theme(
strip.background = element_rect(fill = 'black', colour = 'black'),
strip.text.x = ggtext::element_textbox_simple(colour = 'red', face = 'bold', size = 10, hjust = 0.5, vjust = 0.5, halign = 0.5, valign = 0.5),
strip.text.y = ggtext::element_textbox_simple(colour = 'red', face = 'bold', size = 10, hjust = 0.5, vjust = 0.5, halign = 0.5, valign = 0.5, orientation = "right-rotated")
)
对于strip text
,建议的解决方案是使用ggtext::element textbox()
,它可以根据可用宽度换行文本。但是,我们又面临一个新问题:无法自动确定换行文字的高度。
示例代码:
library(ggplot2)
library(ggtext)
ggplot(mpg2, aes(displ, cty)) +
geom_point() +
facet_grid(vars(drv), vars(cyl))+
theme(
strip.background = element_rect(fill = 'black', colour = 'black'),
strip.text.x = ggtext::element_textbox_simple( width = unit(1, "npc"),
height = unit(10 * 0.5, "lines"),
colour = 'red',
face = 'bold',
size = 10,
hjust = 0.5,
vjust = 0.5,
halign = 0.5,
valign = 0.5),
strip.text.y = ggtext::element_textbox_simple(width = unit(1, "npc"),
height = unit(10 * 0.5, "lines"),
colour = 'red', face = 'bold', size = 10,
hjust = 0.5,
vjust = 0.5,
halign = 0.5,
valign = 0.5,
orientation = "right-rotated"))
剧情:
示例数据:
mpg2 <- mpg
mpg2$drv[mpg2$drv == '4'] <- 'This is a long way of writing Four-weel drive vehicle'
mpg2$cyl[mpg2$cyl == 8] <- 'This is a long way of writing Eight-cylinder vehicle, which is very powerful'
我正在使用 ggtext
中的 element_textbox_simple()
在我的 ggplot2
主题中包装分面标签。但是条带高度似乎并没有随着额外的行而增加。如何让文本框正确换行?
我考虑过在 facet_grid()
中使用 labeller = label_wrap_gen()
参数,但我觉得 element_textbox()
更适用于不同方面 sizes/counts。
library(ggplot2)
library(ggtext)
mpg2 <- mpg
mpg2$drv[mpg2$drv == '4'] <- 'This is a long way of writing Four-weel drive vehicle'
mpg2$cyl[mpg2$cyl == 8] <- 'This is a long way of writing Eight-cylinder vehicle, which is very powerful'
ggplot(mpg2, aes(displ, cty)) +
geom_point() +
facet_grid(vars(drv), vars(cyl)) +
theme(
strip.background = element_rect(fill = 'black', colour = 'black'),
strip.text.x = ggtext::element_textbox_simple(colour = 'red', face = 'bold', size = 10, hjust = 0.5, vjust = 0.5, halign = 0.5, valign = 0.5),
strip.text.y = ggtext::element_textbox_simple(colour = 'red', face = 'bold', size = 10, hjust = 0.5, vjust = 0.5, halign = 0.5, valign = 0.5, orientation = "right-rotated")
)
对于strip text
,建议的解决方案是使用ggtext::element textbox()
,它可以根据可用宽度换行文本。但是,我们又面临一个新问题:无法自动确定换行文字的高度。
示例代码:
library(ggplot2)
library(ggtext)
ggplot(mpg2, aes(displ, cty)) +
geom_point() +
facet_grid(vars(drv), vars(cyl))+
theme(
strip.background = element_rect(fill = 'black', colour = 'black'),
strip.text.x = ggtext::element_textbox_simple( width = unit(1, "npc"),
height = unit(10 * 0.5, "lines"),
colour = 'red',
face = 'bold',
size = 10,
hjust = 0.5,
vjust = 0.5,
halign = 0.5,
valign = 0.5),
strip.text.y = ggtext::element_textbox_simple(width = unit(1, "npc"),
height = unit(10 * 0.5, "lines"),
colour = 'red', face = 'bold', size = 10,
hjust = 0.5,
vjust = 0.5,
halign = 0.5,
valign = 0.5,
orientation = "right-rotated"))
剧情:
示例数据:
mpg2 <- mpg
mpg2$drv[mpg2$drv == '4'] <- 'This is a long way of writing Four-weel drive vehicle'
mpg2$cyl[mpg2$cyl == 8] <- 'This is a long way of writing Eight-cylinder vehicle, which is very powerful'