Home > Back-end >  Changing the variable displayed on the x-axis in ggplot
Changing the variable displayed on the x-axis in ggplot

Time:12-23

I have a dataframe which contains a variable for week-since-2017. So, it counts up from 1 to 313 in that column. I mutated another variable into the dataframe to indicate the year. So, in my scatterplot, I have each week as a point, but the x-axis is horrid, counting up from 1 to 313. Is there a way I can change the scale at the bottom to instead display the variable year, possibly even adding vertical lines in between to show when the year changes?

Currently, I have this:

ggplot(HS, aes(as.integer(Obs), Total))   geom_point(aes(color=YEAR))   geom_smooth()   labs(title="Weekly Sales since 2017",x="Week",y="Written Sales")   theme(axis.line = element_line(colour = "orange", size = 1, linetype = "solid"))

enter image description here

CodePudding user response:

You can convert the number of weeks to a number of days using 7 * Obs and add this value on to the start date (as.Date('2017-01-01')). This gives you a date-based x axis which you can format as you please.

Here, we set the breaks at the turn of each year so the grid fits to them:

ggplot(HS, aes(as.Date('2017-01-01')   7 * Obs, Total))   
  geom_point(aes(color = YEAR))   
  geom_smooth()   
  labs(title = "Weekly Sales since 2017", x = "Week", y = "Written Sales")   
  theme(axis.line = element_line(colour = "orange", size = 1))  
  scale_x_date('Year', date_breaks = 'year', date_labels = '%Y')

enter image description here


Data used

Obviously, we don't have your data, so I had to create a reproducible set with the same names and similar values to yours for the above example:

set.seed(1)

HS <- data.frame(Obs = 1:312, 
                 Total = rnorm(312, seq(1200, 1500, length = 312), 200)^2,
                 YEAR = rep(2017:2022, each = 52))
  • Related