Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line height spacing for text in ggplot

I am trying to reduce the space between my long axis labels. I base R graphics I whould use lheight, but is seems to have no effect in ggplot. Is there a ggplot equivalent?

Toy example to show the problem:

library("tidyverse")
df0 <- mtcars %>% 
  rownames_to_column("car") %>%
  mutate(car = str_wrap(car, width = 10))

ggplot(data = df0, aes(x = car, y = mpg)) +
  geom_bar(stat = "identity") +
  coord_flip()

# has no effect
par(lheight = 0.5)
ggplot(data = df0, aes(x = car, y = mpg)) +
  geom_bar(stat = "identity") +
  coord_flip()

enter image description here

like image 564
guyabel Avatar asked Sep 27 '16 10:09

guyabel


1 Answers

You may be looking for a combination of options. The closest to lheight is likely setting lineheight in element_text. I also made the font smaller, just to show options.

ggplot(data = df0, aes(x = car, y = mpg)) +
  geom_bar(stat = "identity") +
  coord_flip() +
  theme(axis.text.y = element_text(lineheight = 0.5, 
                                   size = 6))

enter image description here

like image 194
Mark Peterson Avatar answered Oct 03 '22 05:10

Mark Peterson