【问题标题】:How can I order the months chronologically in ggplot2 short of writing the months out?在没有写出月份的情况下,如何在 ggplot2 中按时间顺序排列月份?
【发布时间】:2016-07-01 09:59:12
【问题描述】:

我正在尝试绘制计数 v/s 月

ggplot(dat, aes(x=month, y=count,group=region)) +
    geom_line(data=mcount[mcount$region == "West coast", ],colour="black",stat="identity", position="dodge")+
     geom_point(data=mcount[mcount$region == "West coast", ],colour="black", size=2, shape=21, fill="white")+
     theme_bw()+
     theme(legend.key = element_rect(colour = "black")) +
     guides(fill = guide_legend(override.aes = list(colour = NULL)))+
     ggsave("test.png",width=6, height=4,dpi=300)

但我想按时间顺序排列从 1 月到 12 月的月份。如果没有把所有月份都写出来,我该怎么做呢?

输入

structure(list(region = structure(c(6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 
3L), .Label = c("West coast", "Arizona", "Front range", "Flash flood alley", 
"Mississippi valley", "Appalachians"), class = "factor"), month = structure(c(1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 4L, 12L, 11L, 
5L, 2L, 9L, 8L, 6L, 10L, 3L, 7L, 8L, 10L, 5L, 1L, 6L, 7L, 4L, 
6L, 8L, 2L, 1L, 7L, 5L, 3L, 11L, 12L, 9L, 10L, 2L, 7L, 3L, 6L, 
12L, 11L, 10L, 9L, 4L, 1L, 11L, 4L, 2L, 1L, 12L, 9L, 3L, 8L, 
5L, 6L, 10L, 7L, 5L, 8L, 11L, 12L, 4L, 3L, 9L, 2L), .Label = c("Apr", 
"Dec", "Oct", "Mar", "May", "Jul", "Sep", "Jun", "Nov", "Aug", 
"Jan", "Feb"), class = "factor"), count = c(566, 545, 427, 751, 
357, 399, 568, 433, 454, 347, 511, 251, 267, 207, 167, 142, 417, 
109, 117, 373, 207, 130, 125, 145, 7, 14, 2, 2, 7, 3, 107, 74, 
135, 48, 80, 53, 117, 125, 59, 53, 103, 30, 21, 18, 8, 22, 26, 
37, 20, 5, 11, 1, 96, 29, 109, 8, 33, 53, 6, 1, 5, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0)), .Names = c("region", "month", "count"), row.names = c(NA, 
-72L), class = c("data.table", "data.frame"))

【问题讨论】:

  • 您是在问如何获取当前图表并按时间顺序组织它,还是按时间顺序排列标签?
  • 在旁注中,您是否坚持使用 ggplot2 来做这件事?你看过使用带有日期的 dygraphs 吗?您需要做的就是将您的月份列转换为日期类型。
  • @InfiniteFlashChess 除了 ggplot2 之外从未使用过任何东西。很高兴学习有用的库。随意添加。谢谢。
  • 这似乎是一个小问题,可以改变您的整个方法。它有一个非常简单的修复方法。
  • 我还要指出,如果你需要一个时间序列对象来使用 dygraphs,那么“这个确切的问题”是不变的。您需要知道如何以非字母顺序对字符/因子变量进行排序,以获得具有正确排序的时间序列对象。 Dygraphs 看起来很棒,我是交互式图形的忠实粉丝 - 我只是认为它们与这个问题的问题完全无关。

标签: r ggplot2


【解决方案1】:

使用内置的month.namemonth.abb 变量以正确的顺序指定因子的水平。在这种情况下,你有缩写,所以month.abb 是合适的。

your_data$month = factor(your_data$month, levels = month.abb)

我认为以正确的顺序创建因子是最好的方法,但您也可以使用离散刻度的 limits 参数对轴进行排序(有关详细信息,请参阅 ?discrete_scale)。

+ scale_x_discrete(limits = month.abb)

语言环境

如果您在非英语语言环境中,您可以使用一些日期格式构建自己的月份名称常量(基本上是从 Brian Ripley in this R-Help thread 偷来的):

 month.name.loc = format(ISOdate(2004, 1:12, 1), "%B")
 month.abb.loc  = format(ISOdate(2004, 1:12, 1), "%b")

如果您想使用与您所在地区不同的月份名称/缩写,withr 包很有用。

【讨论】:

  • 不知道这些变量,好+1。
  • 这真是太好了,以前我完全看不到。谢谢!需要注意的一件事:month.abb 似乎只识别英文的月份名称。如果他们匹配语言环境那就太好了......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-25
  • 2018-06-06
  • 1970-01-01
  • 2020-07-23
相关资源
最近更新 更多