R语言 我怎样才能把我的月份按时间顺序排列呢?

vlju58qv  于 2023-01-15  发布在  其他
关注(0)|答案(2)|浏览(173)

我的月份在ggplot2中按字母顺序显示,当在facet_wrap命令中使用它们时,我已经使用了所有命令,但没有任何命令起作用

不使用任何month.name或month.abb命令时

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))

使用month.name命令时

我已经使用了month.name和month.abb命令,但它不起作用,而是显示NA值

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"))

我试着把我的月份安排得井井有条,但就是搞不清楚。

s4n0splo

s4n0splo1#

您可以按如下方式将月份作为因子:

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))

这会将级别的顺序设置为您指定的顺序。

gdx19jrr

gdx19jrr2#

或者万一你还想这么做,

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

相关问题