R 时间序列数据的多个x轴标签
我能够使用R 时间序列数据的多个x轴标签,r,ggplot2,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
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()
另一方面,我喜欢Excel只需点击两次就可以绘制出这个图表。它创建了一个漂亮的图表,在x轴上显示季节信息和日期。它实质上创建了一个3层x轴
关于这个话题,我有两个问题:
问题1:使用ggplot
,如何在ggplot
中使用linetype
(或color
)创建连续图形(即无中断)?我更喜欢线型
而不是颜色
。
作为一个例子并回答评论:下面是我使用不同数据集创建的图
df您可以很容易地重新创建Excel绘图的意图,如下所示:
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)
一种解决方法是修改数据框,即在季节
列发生变化时向数据框添加额外的行。
这样说:
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))
库(“plyr”)
#在每个季节结束时增加额外的线路
tmp虽然您的意图很明确,特别是对于您的线型
示例,ggplot2
做的是正确的事情——例如,您希望连接第1季和第2季的线段有什么线型?@tchakravarty感谢您的反馈。现在,我在想,如果我们能用颜色来表现连续性,那会有帮助。我将在上面补充说明。同样的逻辑也适用于颜色。我认为最好的办法是尝试复制Excel为您创建的层次轴。还有一些以前的解决方案。@tchakravarty我已经添加了图表。我无法为时间序列数据复制此数据。我真的迷路了…@ss0208535请每个问题问一个问题。您的Q2是的副本。
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)
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))