【问题标题】:Multiple x-axis labels for time-series data时间序列数据的多个 x 轴标签
【发布时间】:2016-08-29 06:44:55
【问题描述】:

我可以使用ggplot2 绘制时间序列数据。但是,我想强调季节性信息以及时间序列数据。

这是我的代码:

library(zoo)
library(ggplot2)

a <- read.table(text = "
       Season Quarter  Sales
       Season1  2014Q1  20 
       Season1  2014Q2  40 
       Season1  2014Q3  60 
       Season1  2014Q4  80 
       Season2  2015Q1  30 
       Season2  2015Q2  40 
       Season2  2015Q3  80 
       Season3  2015Q4  90 
       Season3  2016Q1  100 
       Season3  2016Q2  120 
       Season3  2016Q3  140
     ", header = TRUE, sep = "")
a$Quarter<-as.yearqtr(a$Quarter)
a$Quarter<-as.Date(a$Quarter)

ggplot(data=a,aes(x=Quarter, y=Sales)) +
       geom_line()

这很有效,因为我能够绘制时间序列数据。

现在,我想标记第 1 季、第 2 季等的构成要素。一种方法是使用 colorlinetype。但是,这似乎不起作用,因为它破坏了时间序列的连续性。

# doesn't work...
ggplot(data=a,aes(x=Quarter, y=Sales)) +
       geom_line(aes(linetype=Season))

另一方面,我喜欢 Excel 如何在两次单击中绘制此图表。 它创建了一个漂亮的图表,在 x 轴上显示季节性信息以及日期。它本质上创建了一个 3 层的 x 轴。

关于这个话题我有两个问题:

问题 1: 使用ggplot,如何在ggplot 中使用linetype(或color)来创建连续图(即没有中断)?我更喜欢linetype 而不是color作为示例并回答评论:这是我使用不同数据集创建的图表。

df <- data.frame(x = 1:3, y = 1:3, z = c(1,3,5))
ggplot(df, aes(x, y, color = factor(z))) +
       geom_line(aes(group = 1))

我无法为时间序列数据复制上述行为。 这是我从上面的代码中得到的图表:

问题 2: 使用 ggplot,我如何创建一个多级 x 轴(类似于 Excel 为我所做的),以显示带有日期的季节性信息? {请参阅我创建的 Excel 图表。} 我不想使用此选项创建图例。我还想澄清一下,如果我们不使用黑客方法,通过调整和重新调整 x 和 y 值来应用 annotate(或者可能是 geom_text)方法来放置这些多级标签,我将不胜感激适合他们。这是因为它违背了使用编程语言绘制图形的目的,并且随着数据的变化它不会起作用。

如果您有任何问题,请告诉我。我很感激你的想法。我是ggplot2 的绝对初学者。我从 Excel 和 STATA 转换到 ggplot 仅 5 天。所以,如果我的问题太基本,我深表歉意。

我在 SO 上研究了这个主题,但没有任何足够接近的东西。例如,this thread 谈到改变刻度,但不是我要找的。​​p>

【问题讨论】:

  • 虽然您的意图很明确,特别是对于您的 linetype 示例,ggplot2 正在做正确的事情 - 例如,linetype 您希望连接第 1 季和第 2 季的部分有吗?
  • @tchakravarty 感谢您的反馈。现在,我在想,如果我们可以通过使用颜色来显示连续性,那会有所帮助。我将在上面添加说明。
  • 同样的逻辑也适用于颜色。我认为您最好的选择是尝试复制 Excel 为您创建的分层轴。 SO herehere 上有一些以前的解决方案。
  • @tchakravarty 我已经添加了图表。我无法为时间序列数据复制它。我真的迷路了……
  • @ss0208535 请对每个 SO 问题提出一个问题。您的 Q2 与 this 重复。

标签: r ggplot2


【解决方案1】:

您可以很容易地重新创建 Excel 绘图的 intent,如下所示:

library(plyr)
ss <- ddply(a, .(Season), summarize, date = min(Quarter))
ss$date <- as.numeric(ss$date)

ggplot(data=a,aes(x=Quarter,y=Sales)) +
  geom_line() +
  geom_vline(data = ss, aes(xintercept = date), colour = "grey50") +
  geom_text(data = ss, aes(x = as.Date(date), y = Inf, label = Season), 
            hjust = -0.1, vjust = 1.1)

使用颜色时线条中断的一种解决方法是在彩色线之外绘制一条连续的灰色线:

ggplot(data=a,aes(x=Quarter,y=Sales)) +
  geom_line(colour = "grey80") +
  geom_line(aes(colour = Season)) +
  geom_vline(data = ss, aes(xintercept = date), colour = "grey50") +
  geom_text(data = ss, aes(x = as.Date(date), y = Inf, label = Season), 
            hjust = -0.1, vjust = 1.1)

【讨论】:

    【解决方案2】:

    Season 列发生变化时,一种解决方法是修改数据框,即在数据框中添加额外的行。 像这样:

    library("plyr")
    
    # add additional lines at end of every season 
    tmp <- ddply(a, "Season",
                 function(x) {
                   x[nrow(x)+1, "Season"] <- x[nrow(x), "Season"]
                   x
                 })
    # fill NA values with values of next season
    tmp$Quarter <- na.locf(tmp$Quarter, fromLast=TRUE, na.rm=FALSE)
    tmp$Sales <- na.locf(tmp$Sales, fromLast=TRUE, na.rm=FALSE)
    tmp <- na.omit(tmp)   # drop last line
    tmp
    #     Season    Quarter Sales
    # 1  Season1 2014-01-01    20
    # 2  Season1 2014-04-01    40
    # 3  Season1 2014-07-01    60
    # 4  Season1 2014-10-01    80
    # 5  Season1 2015-01-01    30
    # 6  Season2 2015-01-01    30
    # 7  Season2 2015-04-01    40
    # 8  Season2 2015-07-01    80
    # 9  Season2 2015-10-01    90
    # 10 Season3 2015-10-01    90
    # 11 Season3 2016-01-01   100
    # 12 Season3 2016-04-01   120
    # 13 Season3 2016-07-01   140
    
    ggplot(data=tmp, aes(x=Quarter, y=Sales)) +
           geom_line(aes(colour=Season, linetype=Season))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-16
      • 2021-12-20
      • 2017-01-17
      • 1970-01-01
      相关资源
      最近更新 更多