首页 > 解决方案 > ggplot中换行文本的行距

问题描述

我需要更改包装 geom_text 图层中的行距。

library(ggplot2)
library(stringr)
txt = c('one two three', 'four five six', 'seven eight nine')
p = ggplot(data=NULL, aes(x=1:3, y=1:3, label = str_wrap(txt, width = 3))) + 
  geom_text() + expand_limits(x = c(0.5, 3.5), y = c(0.5, 3.5))

在此处输入图像描述

theme(text=element_text(lineheight = ...))没有效果,因为theme仅适用于plot 的非数据组件,所以我不清楚如何实现这一点。建议?

标签: rggplot2

解决方案


只需使用lineheight,例如:

ggplot(data = NULL, aes(x = 1:3, y = 1:3, 
                        label = str_wrap(txt, width = 3))) + 
  geom_text(lineheight = .5) + 
  expand_limits(x = c(0.5, 3.5), y = c(0.5, 3.5))

(秒?geom_text

在此处输入图像描述


推荐阅读