首页 > 解决方案 > R - 在条形图描述中自动设置新行

问题描述

在此处输入图像描述

嗨,如果描述太长,如我的示例所示,有人知道我如何设法设置新行。

感谢您的帮助!

标签: rbar-chart

解决方案


为此,您可以使用stringr::str_wrap()函数,该函数将字符向量和要放置换行符“\n”的位置作为参数。

这是使用 ggplot2 的可重现示例。


df <- data.frame(label = c(
    "This is a very long long long label to wrap in two lines",
    "This another very long long long label to wrap in two lines",
    "This is third long, very long long label to wrap in two lines"
),

value = c(500, 600, 800)
)


df %>% 
    ggplot(aes(x = value, y = str_wrap(label, 35))) +
    geom_col()

在此处输入图像描述


推荐阅读