0

Right now I have a graph that is displaying the months as numbers 01,02, …, and I would like to change them into the actual month name. I used this code but get a discrete value error each time.

scale_x_continuous(breaks = c(01,02,03,04,05,06,07,08,09,10,11,12), labels = c("Jan_21", "Feb_21", "Mar_21", "Apr_21", "May_21", "Jun_21", "Jul_21", "Aug_21", "Sep_21","Oct_21","Nov_21","Dec_21"))

So I tried to change it to

scale_x_discrete(breaks = 1:12, labels = c("Jan_21", "Feb_21", "Mar_21", "Apr_21", "May_21", "Jun_21", "Jul_21", "Aug_21", "Sep_21","Oct_21","Nov_21","Dec_21"))

but now only the last three months oct, nov, dec show up on my chart.

full code:

ggplot(aes(x=month, y = number_of_rides, fill = member_casual)) +geom_col(position = "dodge") + labs(x="Month", y = "Total Number of Rides", title = "Table #2: Number of Rides per Month by Rider Type", fill = "Type of Membership") +
    scale_y_continuous(breaks = c(0e+00, 1e+05, 2e+05, 3e+05, 4e+05), labels = c("0","100k","200k","300k","400k")) + 
    scale_x_discrete(breaks = 1:12, labels = c("Jan_21", "Feb_21", "Mar_21", "Apr_21", "May_21", "Jun_21", "Jul_21", "Aug_21", "Sep_21","Oct_21","Nov_21","Dec_21")) + 
    theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1))

Any pointers?

Pax
  • 664
  • 4
  • 23
  • 1
    I suggest first converting the numeric months to factors and assigning the names with the `levels=` argument. Try: https://stackoverflow.com/questions/48920182/how-to-convert-months-as-factors-while-still-maintaining-the-months-in-sequence . Documentation: https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/factor – RobertoT Jun 03 '22 at 18:14
  • If the data has months coded as "01,02,etc." then you might try `scale_x_discrete(breaks = formatC(1:12, width = 2, flag = "0"), labels = c("Jan_21", "Feb_21", "Mar_21", "Apr_21", "May_21", "Jun_21", "Jul_21", "Aug_21", "Sep_21","Oct_21","Nov_21","Dec_21"))`. If you specify 1:12 you get 1,2,3,4...etc which only matches your data for the two-digit months Oct, Nov, Dec. To use your data as is, you need "01" not 1. If you're doing more manipulation, it might be better to change it to a date upstream. – Jon Spring Jun 03 '22 at 18:15

0 Answers0