Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 501
Canovice Avatar asked Jan 23 '26 05:01

Canovice


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

like image 81
tamtam Avatar answered Jan 25 '26 21:01

tamtam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!