Reduce space between y axis and cathegories in a ggplot

Viewed 1781

I have made the graph below with ggplot. I would like to reduce the distance between the y axis and the first category (a). Which function should I use? Thanks! :)

library(ggplot2)
library(reshape2)

data <- data.frame(a = rnorm(10), b = rnorm(10), c = rnorm(10), group = 1:10)
data <- melt(data, id = "group")

ggplot(data, aes(x = variable, y = value, group = group, color = as.factor(group))) + geom_point() + geom_line() + theme_minimal() + theme(axis.line = element_line(), panel.grid = element_blank())

enter image description here

1 Answers

Suppose we have the following plot:

library(ggplot2)

df <- data.frame(x = rep(LETTERS[1:3], 3),
                 y = rnorm(9),
                 z = rep(letters[1:3], each = 3))

ggplot(df, aes(x, y, colour = z, group = z)) +
  geom_line() +
  geom_point()

enter image description here

We can reduce the space between the extreme points and the panel edges by adjusting the expand argument in a scale function:

ggplot(df, aes(x, y, colour = z, group = z)) +
  geom_line() +
  geom_point() +
  scale_x_discrete(expand = c(0,0.1))

enter image description here

Setting expand = c(0,0) completely removes the space. The first argument is a relative number, the second an absolute; so in the example above we set the expand to 0.1 x-axis units.

Related