Jack
Jack

Reputation: 857

How to increase spaces between categories in ggplot?

I aim to increase the space between all the categories on the x-axis. For example, the estimate -173.22 extends to the side of Italy. If i increase the space between the categories then i make sure the estimate texts don't overlap. Another way to do it is to decrease the size of the text, but i would like to keep it as it is.

The only related questions on StackOverflow are with geom_bar or geom_col: Adding space between bars in ggplot2. But they don't seem to solve the problem. Could someone help with it please?

enter image description here

Here is the data and the code:

df= structure(list(cntry = structure(1:3, .Label = c("France", "Italy", 
                                                 "United Kingdom"), class = "factor"), term = c("unemp", "unemp", "unemp"), 
               estimate = c(-173.22, 202.12, 508.44
               )), row.names = c(NA, -3L), class = "data.frame")

df %>%
  mutate(label = replace(round(estimate, 2),cntry==1, '')) %>%
  ggplot(aes(estimate, cntry,label=label)) + 
  geom_text(hjust= -0.2, size= 10)  + 
  geom_point(mapping=aes(x=estimate, y=cntry), size=5) +
  coord_flip()   

Upvotes: 0

Views: 88

Answers (1)

Jon Spring
Jon Spring

Reputation: 66415

You can control how much padding is added on the sides of France and UK with:

scale_y_discrete(expand = expansion(add = .2)) +

or if you want more padding on the UK side:

scale_y_discrete(expand = expansion(add = c(.2, 0.5))) +

enter image description here

Upvotes: 1

Related Questions