Change x-axis ticks in ggplot

I have a chart that isn't showing the x-axis as I'd like. The x-axis is supposed to be a list of years (2011-2019), but it's reading them as numbers - here's what the chart currently looks like:

I'd like the Season on the x-axis to read as "2011", "2012", etc. I tried the following code with scale_x_discrete, but it didn't work:

ggplot() +
  geom_line(data = all_stats_wins, aes(x = season, y = fpts_pg, color = rusher_position)) +
  scale_x_discrete(limits = c("2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019"))+

This is what the chart looks like when I use the above code:

Finally, here's a snippet of what the data looks like:

   rusher_position game_result season total_rushing_f~ total_receiving~
   <chr>           <chr>        <dbl>            <dbl>            <dbl>
 1 QB              win           2011            470.               3.2
 2 QB              win           2012            617.               0.5
 3 QB              win           2013            575.               1.2
 4 QB              win           2014            444.              11.7
 5 QB              win           2015            560.               3.7
 6 QB              win           2016            391.              17.4
 7 QB              win           2017            503.              -0.1
 8 QB              win           2018            705.              16.6
 9 QB              win           2019            696.              71.6
10 RB              win           2011           4270             1510. 
11 RB              win           2012           4266.            1314. 
12 RB              win           2013           4035.            1790. 
13 RB              win           2014           4195             1719. 
14 RB              win           2015           3904.            1877. 
15 RB              win           2016           4199.            1779. 
16 RB              win           2017           4032.            2076. 
17 RB              win           2018           4244.            1915. 
18 RB              win           2019           4264.            2006. 
19 TE              win           2011             19.3           2844. 
20 TE              win           2012              1.3           2636.

Any ideas as to why this is occurring?

Try using

scale_x_continuous(breaks = 2011:2019)
1 Like
n <- 5000
set.seed(1)
x <- rnorm(n)
date <- Sys.Date() + c(1:n)
date <- lubridate::ymd(date)

data.frame(x = x, date = date) %>%
  ggplot2::ggplot() +
  geom_line(aes(date, x)) +
  scale_x_date(date_breaks = "1 year", date_labels = "%Y") +
  theme(axis.text.x=element_text(angle = 45, hjust = 1))

Rplot

So simple, yet so effective! Thanks, @FJCC!

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