Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line spacing for wrapped text in ggplot

Tags:

r

ggplot2

I need to change the line spacing in wrapped geom_text layer.

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

But theme(text=element_text(lineheight = ...)) has no effect because theme only works for non-data components of the plot, so I'm not clear how to achieve this. Suggestions?

like image 455
geotheory Avatar asked Jul 02 '18 10:07

geotheory


1 Answers

Just use lineheight, e.g.:

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))

(s. ?geom_text)

enter image description here

like image 50
erc Avatar answered Oct 19 '22 03:10

erc