我正在使用来自element_textbox_simple()的ggtext来在ggtext主题中包装小块标签。但是,带状高度似乎并没有随着增加的线而增加。如何使文本框正确包装?
我已经考虑过在labeller = label_wrap_gen()中使用facet_grid()参数,但我觉得element_textbox()对于不同的方面大小/计数更为通用。
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")
)

发布于 2022-03-16 09:59:23
对于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'https://stackoverflow.com/questions/71494583
复制相似问题