1

I am generating a barplot using ggplot which looks like this: enter image description here As you can see, the axis labels are very squished. Because the actual barplot itself doesn't add much information, I would like to decrease the size of it (for example to the size of the area indicated with the green line above the plot), while simultaneously giving more space to the axis label (blue line in the plot), so I can do the line break after 50 characters instead of 30. I tried to change the plot.margin and played around with the hjust argument of the theme function, but didnt manage to accomplish what I want.

I would very much appreciate any help you can offer!

nhaus
  • 786
  • 3
  • 13

1 Answers1

1

We could tweak with plot.margin in theme() and element_text() as already answered here:

Here is an example:

library(tidyverse)

mtcars %>% 
  mutate(cyl = case_when(cyl == 4 ~ "a very very very long name", 
                         cyl == 6 ~ "a more more more more more name",
                         cyl == 8 ~ "a longest longest longest longest name")) %>% 
  count(cyl, sort=TRUE) %>% 
  ggplot(aes(x=fct_reorder(cyl, n), y=n, fill=-n)) +
  geom_col()+
  scale_fill_gradient(low="blue", high="red")+
  coord_flip()+
  labs(title= "My title")+
  theme(axis.title.x=element_text(vjust=-2, size=30)) +
  theme(axis.title.y=element_text(angle=90, vjust=-0.5)) +
  theme(axis.text.y = element_text(size=20))+
  theme(plot.title=element_text(size=15, vjust=3)) +
  theme(plot.margin = unit(c(4,1,1,1), "cm"))

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66