Home > Back-end >  how can i make my months in choronological order?
how can i make my months in choronological order?

Time:01-11

my months are showing in ggplot2 in alphabetical order when in use them in facet_wrap command i have used all the commands but nothing is working

When I'm not using any month.name or month.abb command

fina_result<- read_csv("my_final_project.csv")
fina_result %>% 
  ggplot(aes(x = weekday, y = `count of ride_id`, fill = user_type))  
  geom_col(position = "dodge")  
  facet_wrap(~months)   
  theme(axis.text.x = element_text(angle = 45))

when m not using any month.name or month.abb command

When I'm using month.name command

I have used month.name and month.abb command but its not working instead it shows an NA value

fina_result<- read_csv("my_final_project.csv")
fina_result %>% 
  ggplot(aes(x = weekday, y = `count of ride_id`, fill = user_type))  
  geom_col(position = "dodge")  
  facet_wrap(~months)  
  theme(axis.text.x = element_text(angle = 45))

fina_result$months = factor(fina_result$months, levels = month.name)
fina_result$weekday<-ordered(fina_result$weekday, levels=c("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"))

when I'm using month.name command

i tried to make my months in order but they are not sorting out.

CodePudding user response:

You can make the months a factor as follows:

fina_result<- read_csv("my_final_project.csv")

fina_result$months <- factor(fina_result$months, levels = c("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"))

fina_result %>% 
  ggplot(aes(x = weekday, y = `count of ride_id`, fill = user_type))  
  geom_col(position = "dodge")  
  facet_wrap(~months)   
  theme(axis.text.x = element_text(angle = 45))

This will set the order of the levels to the order you specified.

CodePudding user response:

Or in case you need to do this again,

months <- factor(month.name, levels = month.name[1:12])
  • Related