change the distance between the bar and the Y axis in r

Asked

Viewed 89 times

3

How can I change the distance of the bars relative to the y axis using ggplot?

using the code:

k <- c("a","b","c","d","e","f")
j <- c(3,5,8,1,2,6)

df <- data.frame(k,j)

ggplot(df, aes(y=reorder(k, -j), x=j))+
  geom_col(fill="#70A2E7",  width = 0.85)+
  theme_bw(10)

graphic inserir a descrição da imagem aqui

1 answer

3


The option expand of scales exists for this. Can be used with the function expansion for greater control:

library(ggplot2)

p <- ggplot(df, aes(j, reorder(k, -j))) +
  geom_col(fill="#70A2E7", width = 0.85) +
  theme_bw(10)

# Distância aditiva (adiciona espaço na mesma escala dos dados plotados)
p + scale_x_continuous(expand = expansion(add = c(0, 0.5)))

# Distância multiplicativa (adiciona espaço proporcional, p.e. 0.1 para 10%)
p + scale_x_continuous(expand = expansion(mult = c(0.1, 0.2)))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.