Did scale_x_discrete params change (specifically `limits`)?

Hi! Recently I was trying to re-knit an old Rmd with a graph that made use of scale_x_discrete(), but running the same code now seems to yield a different (buggy) plot. In particular, I've isolated the issue to the inclusion of the limit parameter. Whereas two months ago I had no issue, I can't get the original plots today.

What might be my issue here? I'm wondering whether it has to do with a particular version of ggplot2 I'm using (I just reinstalled the latest via install.packages('tidyverse') to my new machine today). Or are there alternative ways to get the plot I'm aiming for?

Reprex:

library(tidyverse)
beer_states <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-03-31/beer_states.csv')

beer_states %>% 
  filter(state == "CA") %>% 
  ggplot(aes(x = year, y = barrels)) + 
  geom_col() +
  scale_x_discrete(limits = 2008:2019)

The columns all scrunch up to the rightmost side of the x-axis.

Expected behavior:

library(tidyverse)
beer_states <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-03-31/beer_states.csv')

beer_states %>% 
  filter(state == "CA") %>% 
  ggplot(aes(x = year, y = barrels)) + 
  geom_col()

I am trying to make a plot similar to the above, except with the x-axis tickmarks at each year between 2008 and 2019 (inclusive).

I'm not sure if anything has changed recently, but I think you might want to use scale_continuous() instead of scale_discrete()

library(tidyverse)

beer_states <- read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-03-31/beer_states.csv')
#> Parsed with column specification:
#> cols(
#>   state = col_character(),
#>   year = col_double(),
#>   barrels = col_double(),
#>   type = col_character()
#> )

beer_states %>% 
  filter(state == "CA") %>% 
  ggplot(aes(x = year, y = barrels)) + 
  geom_col() +
  scale_x_continuous(breaks = 2008:2019)

Created on 2020-06-10 by the reprex package (v0.3.0)

1 Like

Yes, you're right. Thanks a ton, Matt!

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.