具有 2 个 y 轴(次要 y 轴)的 2 个 ts 对象(时间序列)的 ggplot
ggplot of 2 ts-objects (time series) with 2 y axes (secondary y-axis)
我想使用 2 个 y 轴将 2 个 ts 对象分别绘制为条形和线条。我如何在 ggplot 中执行此操作?
我有2个ts-objects:一个是变量的值,另一个是每年的变化。数据是月度的。我想将两个 ts-objects 绘制到一张图中,值作为线,增长率作为条形图。为此,我需要一个辅助 y 轴,因为这两个变量的尺度非常不同。
我通常使用 ts.plot 绘制 ts-objects,它很容易容纳第二个 y 轴,但我无法绘制条形图,只能绘制线条。
使用 ggplot,我在如何使用 ts-object 上苦苦思索...使用 autoplot,我可以生成绘图和辅助轴,但后者似乎确实独立于我的数据。在下面的示例中,如何让线条和条形重叠?
# REPRODUCIBLE EXAMPLE
library(ggplot2)
library(ggfortify) # to use autoplot
library(seasonal) # to get the example ts data AirPassengers
library(dplyr) # to use the pipe-operator
# Genereate year-on-year change
YearOverYear <- function (x,periodsPerYear){
if(NROW(x)<=periodsPerYear){
stop("too few rows")
}
else{
indexes<-1:(NROW(x) - periodsPerYear)
return(c(rep(NA,periodsPerYear), (x[indexes+periodsPerYear]- x[indexes]) / x[indexes]))
}
}
AirPassengers.gr <- YearOverYear(AirPassengers, 12) %>%
ts(., start = start(AirPassengers), frequency = 12)
p <- autoplot(AirPassengers, ts.geom = 'line', ts.colour = 'dodgerblue')
autoplot(AirPassengers.gr*100, ts.geom = 'bar', ts.colour = 'red', p=p) +
scale_y_continuous(sec.axis = sec_axis(~./1))
很高兴认识你,伊莎贝尔
我只是将 ts.object
更改为 data.table
,然后使用基本的 ggplot
方法。此外,您可以应用任何棘手的技能。
库加载
library(ggplot2)
library(ggfortify) # to use autoplot
library(seasonal) # to get the example ts data AirPassengers
library(dplyr) # to use the pipe-operator
library(zoo);library(data.table)
数据处理
YearOverYear <- function (x,periodsPerYear){
if(NROW(x)<=periodsPerYear){
stop("too few rows")
}
else{
indexes<-1:(NROW(x) - periodsPerYear)
return(c(rep(NA,periodsPerYear), (x[indexes+periodsPerYear]- x[indexes]) / x[indexes]))
}
}
AirPassengers.gr <- YearOverYear(AirPassengers, 12) %>%
ts(., start = start(AirPassengers), frequency = 12)
lubridate::as_date(time(AirPassengers))
DF = data.frame(Passengers = as.matrix(AirPassengers),
date = zoo::as.Date(time(AirPassengers)))
DF.gr = data.frame(value = as.matrix(AirPassengers.gr),
date = zoo::as.Date(time(AirPassengers.gr)))
DF = merge(DF,DF.gr, by = 'date')
setDT(DF)
绘制代码
scale_value = max(DF$Passengers, na.rm = TRUE)/ max(DF$value, na.rm = TRUE)
ggplot(DF) +
geom_line(aes(x= date, y= Passengers), color = 'dodgerblue') +
geom_bar(aes(x= date, y = value*scale_value), stat = 'identity') +
scale_y_continuous(sec.axis = sec_axis(~./scale_value, name = 'NEW'))
如果您有任何问题,请随时提出。
我想使用 2 个 y 轴将 2 个 ts 对象分别绘制为条形和线条。我如何在 ggplot 中执行此操作?
我有2个ts-objects:一个是变量的值,另一个是每年的变化。数据是月度的。我想将两个 ts-objects 绘制到一张图中,值作为线,增长率作为条形图。为此,我需要一个辅助 y 轴,因为这两个变量的尺度非常不同。
我通常使用 ts.plot 绘制 ts-objects,它很容易容纳第二个 y 轴,但我无法绘制条形图,只能绘制线条。
使用 ggplot,我在如何使用 ts-object 上苦苦思索...使用 autoplot,我可以生成绘图和辅助轴,但后者似乎确实独立于我的数据。在下面的示例中,如何让线条和条形重叠?
# REPRODUCIBLE EXAMPLE
library(ggplot2)
library(ggfortify) # to use autoplot
library(seasonal) # to get the example ts data AirPassengers
library(dplyr) # to use the pipe-operator
# Genereate year-on-year change
YearOverYear <- function (x,periodsPerYear){
if(NROW(x)<=periodsPerYear){
stop("too few rows")
}
else{
indexes<-1:(NROW(x) - periodsPerYear)
return(c(rep(NA,periodsPerYear), (x[indexes+periodsPerYear]- x[indexes]) / x[indexes]))
}
}
AirPassengers.gr <- YearOverYear(AirPassengers, 12) %>%
ts(., start = start(AirPassengers), frequency = 12)
p <- autoplot(AirPassengers, ts.geom = 'line', ts.colour = 'dodgerblue')
autoplot(AirPassengers.gr*100, ts.geom = 'bar', ts.colour = 'red', p=p) +
scale_y_continuous(sec.axis = sec_axis(~./1))
很高兴认识你,伊莎贝尔
我只是将 ts.object
更改为 data.table
,然后使用基本的 ggplot
方法。此外,您可以应用任何棘手的技能。
库加载
library(ggplot2)
library(ggfortify) # to use autoplot
library(seasonal) # to get the example ts data AirPassengers
library(dplyr) # to use the pipe-operator
library(zoo);library(data.table)
数据处理
YearOverYear <- function (x,periodsPerYear){
if(NROW(x)<=periodsPerYear){
stop("too few rows")
}
else{
indexes<-1:(NROW(x) - periodsPerYear)
return(c(rep(NA,periodsPerYear), (x[indexes+periodsPerYear]- x[indexes]) / x[indexes]))
}
}
AirPassengers.gr <- YearOverYear(AirPassengers, 12) %>%
ts(., start = start(AirPassengers), frequency = 12)
lubridate::as_date(time(AirPassengers))
DF = data.frame(Passengers = as.matrix(AirPassengers),
date = zoo::as.Date(time(AirPassengers)))
DF.gr = data.frame(value = as.matrix(AirPassengers.gr),
date = zoo::as.Date(time(AirPassengers.gr)))
DF = merge(DF,DF.gr, by = 'date')
setDT(DF)
绘制代码
scale_value = max(DF$Passengers, na.rm = TRUE)/ max(DF$value, na.rm = TRUE)
ggplot(DF) +
geom_line(aes(x= date, y= Passengers), color = 'dodgerblue') +
geom_bar(aes(x= date, y = value*scale_value), stat = 'identity') +
scale_y_continuous(sec.axis = sec_axis(~./scale_value, name = 'NEW'))
如果您有任何问题,请随时提出。