ggplot中换行文本的行距

时间:2018-07-02 10:00:44

标签: r ggplot2

我需要更改包装的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))

enter image description here

但是theme(text=element_text(lineheight = ...))无效,因为theme仅适用于non-data components of the plot,因此我不清楚如何实现。有建议吗?

1 个答案:

答案 0 :(得分:4)

只需使用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条)

enter image description here