1

I am trying something that has already been bothering many before me, but mostly for continuous scales, and mostly for the y-axis. These question come closest:

However, neither

  • scale_x_discrete(expand = ...) nor
  • coord_cartesian(expand = ...) change anything! (Nothing happens, actually)

What do I do wrong?

my_dat <- data.frame(x = 'x', y = rnorm(400))
ggplot(my_dat, aes(x, y)) + 
  geom_jitter() 

enter image description here

sessionInfo()
R version 3.4.3 (2017-11-30)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.3

...

other attached packages:
[1] bindrcpp_0.2     ggbeeswarm_0.6.0 reshape2_1.4.3  
[4] stringr_1.2.0    ggplot2_2.2.1    lubridate_1.7.1 
[7] tidyr_0.7.1      purrr_0.2.4      dplyr_0.7.4 
tjebo
  • 21,977
  • 7
  • 58
  • 94

2 Answers2

2

Short answer: In your plot, the space shaded in red has nothing to do with expansion, so changing the expand = ... parameter won't affect anything. Change the width parameter in geom_jitter instead.

Explanation: If you check the help file for geom_jitter(), you'll see that the default jitter width is 40% of data resolution:

... defaults to 40% of the resolution of the data: this means the jitter values will occupy 80% of the implied bins. Categorical data is aligned on the integers, so a width or height of 0.5 will spread the data so it's not possible to see the distinction between the categories.

We can also illustrate this by varying the width parameter. Observe that with width = 0.4 (default), there's space to the left & right of the blue points. With width = 0.5, the red points go all the way to the ends:

ggplot(my_dat, aes(x, y)) +
  geom_jitter(width = 0.4, color = "blue") +
  geom_jitter(width = 0.5, color = "red")

plot

As for the expand = ... parameter in scale_x_discrete / coord_cartesian, they do make a difference... if your axis has more than 1 value:

my_dat2 <- my_dat
my_dat2$x <- sample(c('y', 'z'), size = 400, replace = TRUE)

p2 <- ggplot(my_dat2, aes(x, y)) +
  geom_jitter(width = 0.4, color = "blue") +
  geom_jitter(width = 0.5, color = "red")

The plot parameters are same, but now there are two possible factor values along the x-axis. Observe that while there's no distinct break in red points (with width = 0.5) between category y & category z, there's an additional clear gap to the left and right. That's the space resulting from expansion:

set.seed(1) # set seed for consistent jitter values in each plot
p2

plot 1

Set horizontal expansion to c(0, 0), and the gap disappears. Now the red points go all the way to the left / right edges:

set.seed(1)
p2 +
  scale_x_discrete(expand = c(0, 0))

plot 2

Set expand = FALSE in coord_cartesian, and vertical expansion disappears as well. Now the red points go all the way to the edge in all four directions:

set.seed(1)
p2 +
  coord_cartesian(expand = FALSE)

plot 3

(In all three cases, the blue points with default jitter width of 0.4 do not reach the edge, because they are only supposed to occupy 80% of the implied bin.

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
0

You need to change the oob option as well. See http://www.hafro.is/~einarhj/education/ggplot2/scales.html for some examples (search for oob on the page.)

Melissa Key
  • 4,476
  • 12
  • 21