时间序列数据的多个x轴标签

时间:2016-08-29 06:44:55

标签: r ggplot2

我可以使用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()

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

现在,我想标记第1季,第2季的内容。其中一种方法是使用colorlinetype。但是,这似乎不起作用,因为它打破了时间序列的连续性。

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

plot2

另一方面,我喜欢Excel只需点击两下就可以在中绘制图形。它会创建一个漂亮的图形,显示x轴上的季节性信息以及日期。它基本上创建了一个3层的x轴。

plot3

我对这个主题有两个问题:

问题1:使用ggplot,如何在linetype中使用color(或ggplot}来创建连续图表(即没有休息时间)?我希望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))

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

enter image description here

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

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

我在SO上研究了这个主题,并且没有任何足够接近的东西。例如,this thread谈论改变刻度,但不是我正在寻找的。

2 个答案:

答案 0 :(得分:2)

您可以非常轻松地重新创建Excel图表的 intent ,如下所示:

RecyclerView

enter image description here

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

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)

enter image description here

答案 1 :(得分:1)

解决方法是修改数据框,即在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))

ggplot output

相关问题