Reduce line-height in R ggplot in geom_label() and geom_text(), when line-breaking with \n

Viewed 523

The following code creates the following graph:

data.frame(x = c(1,2,3,4), y = c(1,2,3,4)) %>% 
  ggplot() + 
  geom_point(aes(x = x, y = y)) + 
  geom_label(aes(x = 2.5, y = 2.5, label = 'label here \n with break'), fill = '#dddddd')

enter image description here

We need to reduce the gap between the two rows of text. I believe this corresponds to the css properly line-height, which when reduced will bring lines closer together as the line height is smaller.

We cannot render 2 geom_labels with different y values, as the vertical positioning doesn't remain consistent with 2 geom_labels when resizing the graph. We need to use the 1 geom_label() with \n for line break and we need to reduce the gap between the two lines of text. Is this possible to do?

1 Answers

Try the lineheight argument in geom_label:

data.frame(x = c(1,2,3,4), y = c(1,2,3,4)) %>% 
  ggplot() + 
  geom_point(aes(x = x, y = y)) + 
  geom_label(aes(x = 2.5, y = 2.5, label = 'label here \n with break'), 
             fill = '#dddddd', lineheight = 0.5) 

enter image description here

Related