geotheory
geotheory

Reputation: 23660

Line spacing for wrapped text in ggplot

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?

Upvotes: 28

Views: 8844

Answers (1)

erc
erc

Reputation: 10133

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

Upvotes: 49

Related Questions