R 在多时间序列图中标记X轴

R 在多时间序列图中标记X轴,r,time-series,R,Time Series,我在三个单独的屏幕上绘制了三个时间序列图。除了x轴之外,所有的作品都很好,不会画出我要画的东西 我使用了来自的代码,它适用于一个屏幕,即一个屏幕上的所有三个图形,但它拒绝在三屏幕格式上注册,并且没有错误消息 dev.new() plot(tp,screens=c(1,2,3),col=c("deepskyblue","seagreen3","orchid4"),ylim=c(0,1600),lwd="3",ylab=(expression("TP"~~(mu*"g/L"))),xlab=NA,

我在三个单独的屏幕上绘制了三个时间序列图。除了x轴之外,所有的作品都很好,不会画出我要画的东西

我使用了来自的代码,它适用于一个屏幕,即一个屏幕上的所有三个图形,但它拒绝在三屏幕格式上注册,并且没有错误消息

dev.new()
plot(tp,screens=c(1,2,3),col=c("deepskyblue","seagreen3","orchid4"),ylim=c(0,1600),lwd="3",ylab=(expression("TP"~~(mu*"g/L"))),xlab=NA,
main="Seasonal variation of total phosphorus in Water Friendly Farming catchments",cex.main=2,xaxt="n")
legend(locator(1),legend=c("Barkby Brook","Eye Brook","Stonton Brook"),fill=c("deepskyblue","seagreen3","orchid4"),bty="n")
times<-time(tp)
ticks<-seq(times[1],times[length(times)],by=90)
axis(1,at=ticks,labels=c("Spring 2012","Summer 2012","Autumn 2012","Winter 2012","Spring 2013","Summer 2013","Autumn 2013","Winter 2013","Spring 2014"))
dev.new()
绘图(tp,screens=c(1,2,3),col=c(“深蓝”、“海绿3”、“兰花4”),ylim=c(01600),lwd=“3”,ylab=(表达式(“tp”~~(mu*“g/L”)),xlab=NA,
main=“节水农业集水区总磷的季节变化”,cex.main=2,xaxt=“n”)
图例(定位器(1),图例=c(“巴克比溪”、“眼溪”、“斯顿溪”),填充=c(“深蓝”、“海绿3”、“兰花4”),bty=“n”)

时间您试图强制
日期轴上的
文本
标签,因此没有输出。对于自定义文本标签,您需要将
xts/zoo
转换为
data.frame
,然后再打印,打印不带任何时间索引的数据 并在轴(1,…)中提供自定义标签

代码

使用示例xts数据

#Load sample xts/zoo data, lubridate for month/year functions

require(PerformanceAnalytics)
require(lubridate)
data(edhec)

#Convert to data.frame for plotting, keep data until Dec 1998 and single time series 

df<-data.frame(date=index(edhec[,1]['/1998']),edhec[,1]['/1998'],row.names=NULL)


#label break dates,monthly data,hence seq index = 3

ticks<-df$date[seq(1,length(df$date),3)]

#label breaks, at = seq(1,length(df$date),3)

#Map months to seasons (USA)
Month2Season<-function(mon){

Winter<-c(12,1,2)
Spring<-c(3,4,5)
Summer<-c(6,7,8)
Autumn<-c(9,10,11)

season<-NULL

if      (any(mon %in% Winter)) season="Winter"  
else if (any(mon %in% Spring)) season="Spring" 
else if (any(mon %in% Summer)) season="Summer" 
else if (any(mon %in% Autumn)) season="Autumn" 

return(season)
}

#get seasons for all label break dates

season_labels<-sapply(ticks,function(x) paste0(Month2Season(month(x)),"-",year(x)))

# provide numeric index for label breaks of at =

plot(df$Convertible.Arbitrage,type="l",xaxt="n")
axis(1,at=seq(1,length(df$date),3),labels=season_labels)
#加载样本xts/zoo数据,润滑月/年功能
要求(性能分析)
要求(润滑)
数据(edhec)
#转换为data.frame进行绘图,将数据保留到1998年12月和单个时间序列

df您试图强制
日期轴上的
文本
标签,因此没有输出。对于自定义文本标签,您需要将
xts/zoo
转换为
data.frame
,然后再打印,打印不带任何时间索引的数据 并在轴(1,…)中提供自定义标签

代码

使用示例xts数据

#Load sample xts/zoo data, lubridate for month/year functions

require(PerformanceAnalytics)
require(lubridate)
data(edhec)

#Convert to data.frame for plotting, keep data until Dec 1998 and single time series 

df<-data.frame(date=index(edhec[,1]['/1998']),edhec[,1]['/1998'],row.names=NULL)


#label break dates,monthly data,hence seq index = 3

ticks<-df$date[seq(1,length(df$date),3)]

#label breaks, at = seq(1,length(df$date),3)

#Map months to seasons (USA)
Month2Season<-function(mon){

Winter<-c(12,1,2)
Spring<-c(3,4,5)
Summer<-c(6,7,8)
Autumn<-c(9,10,11)

season<-NULL

if      (any(mon %in% Winter)) season="Winter"  
else if (any(mon %in% Spring)) season="Spring" 
else if (any(mon %in% Summer)) season="Summer" 
else if (any(mon %in% Autumn)) season="Autumn" 

return(season)
}

#get seasons for all label break dates

season_labels<-sapply(ticks,function(x) paste0(Month2Season(month(x)),"-",year(x)))

# provide numeric index for label breaks of at =

plot(df$Convertible.Arbitrage,type="l",xaxt="n")
axis(1,at=seq(1,length(df$date),3),labels=season_labels)
#加载样本xts/zoo数据,润滑月/年功能
要求(性能分析)
要求(润滑)
数据(edhec)
#转换为data.frame进行绘图,将数据保留到1998年12月和单个时间序列

df您试图强制
日期轴上的
文本
标签,因此没有输出。对于自定义文本标签,您需要将
xts/zoo
转换为
data.frame
,然后再打印,打印不带任何时间索引的数据 并在轴(1,…)中提供自定义标签

代码

使用示例xts数据

#Load sample xts/zoo data, lubridate for month/year functions

require(PerformanceAnalytics)
require(lubridate)
data(edhec)

#Convert to data.frame for plotting, keep data until Dec 1998 and single time series 

df<-data.frame(date=index(edhec[,1]['/1998']),edhec[,1]['/1998'],row.names=NULL)


#label break dates,monthly data,hence seq index = 3

ticks<-df$date[seq(1,length(df$date),3)]

#label breaks, at = seq(1,length(df$date),3)

#Map months to seasons (USA)
Month2Season<-function(mon){

Winter<-c(12,1,2)
Spring<-c(3,4,5)
Summer<-c(6,7,8)
Autumn<-c(9,10,11)

season<-NULL

if      (any(mon %in% Winter)) season="Winter"  
else if (any(mon %in% Spring)) season="Spring" 
else if (any(mon %in% Summer)) season="Summer" 
else if (any(mon %in% Autumn)) season="Autumn" 

return(season)
}

#get seasons for all label break dates

season_labels<-sapply(ticks,function(x) paste0(Month2Season(month(x)),"-",year(x)))

# provide numeric index for label breaks of at =

plot(df$Convertible.Arbitrage,type="l",xaxt="n")
axis(1,at=seq(1,length(df$date),3),labels=season_labels)
#加载样本xts/zoo数据,润滑月/年功能
要求(性能分析)
要求(润滑)
数据(edhec)
#转换为data.frame进行绘图,将数据保留到1998年12月和单个时间序列

df您试图强制
日期轴上的
文本
标签,因此没有输出。对于自定义文本标签,您需要将
xts/zoo
转换为
data.frame
,然后再打印,打印不带任何时间索引的数据 并在轴(1,…)中提供自定义标签

代码

使用示例xts数据

#Load sample xts/zoo data, lubridate for month/year functions

require(PerformanceAnalytics)
require(lubridate)
data(edhec)

#Convert to data.frame for plotting, keep data until Dec 1998 and single time series 

df<-data.frame(date=index(edhec[,1]['/1998']),edhec[,1]['/1998'],row.names=NULL)


#label break dates,monthly data,hence seq index = 3

ticks<-df$date[seq(1,length(df$date),3)]

#label breaks, at = seq(1,length(df$date),3)

#Map months to seasons (USA)
Month2Season<-function(mon){

Winter<-c(12,1,2)
Spring<-c(3,4,5)
Summer<-c(6,7,8)
Autumn<-c(9,10,11)

season<-NULL

if      (any(mon %in% Winter)) season="Winter"  
else if (any(mon %in% Spring)) season="Spring" 
else if (any(mon %in% Summer)) season="Summer" 
else if (any(mon %in% Autumn)) season="Autumn" 

return(season)
}

#get seasons for all label break dates

season_labels<-sapply(ticks,function(x) paste0(Month2Season(month(x)),"-",year(x)))

# provide numeric index for label breaks of at =

plot(df$Convertible.Arbitrage,type="l",xaxt="n")
axis(1,at=seq(1,length(df$date),3),labels=season_labels)
#加载样本xts/zoo数据,润滑月/年功能
要求(性能分析)
要求(润滑)
数据(edhec)
#转换为data.frame进行绘图,将数据保留到1998年12月和单个时间序列

df不幸的是,当您绘制多个绘图时,默认的
plot.zoo
功能会在内部更改
par
设置,然后在该功能存在时自动重置它们。当它重置
par
参数时,这将重置打印曲面,因此对
轴的后续调用不起作用。仅绘制单个时间序列时,此函数不执行此操作

没有超简单/健壮的方法来解决这个问题。但是,如果您觉得很大胆,您可以对该函数执行手术并在其中插入新代码。(这不是一般推荐的做法,但在这种情况下,它会让你的生活更轻松)。我是用zoo版本1.7-10做的。要确保这对您有效,请检查

body(plot.zoo)[19:20]
返回

do.call("title", title.args)(return(invisible(x)))
因为我们将在这两个语句之间添加一行代码。我们将用

plot.zoo2<-plot.zoo
bl<-as.list(body(plot.zoo2))
bl<-c(
    bl[1:19], 
    quote(axis(1,at=ticks,labels=tick.lables)), 
    bl[20]
)
body(plot.zoo2)<-as.call(bl)


不幸的是,当您绘制多个绘图时,默认的
plot.zoo
功能会在内部更改
par
设置,然后在该功能存在时自动重置设置。当它重置
par
参数时,这将重置打印曲面,因此对
轴的后续调用不起作用。仅绘制单个时间序列时,此函数不执行此操作

没有超简单/健壮的方法来解决这个问题。但是,如果您觉得很大胆,您可以对该函数执行手术并在其中插入新代码。(这不是一般推荐的做法,但在这种情况下,它会让你的生活更轻松)。我是用zoo版本1.7-10做的。要确保这对您有效,请检查

body(plot.zoo)[19:20]
返回

do.call("title", title.args)(return(invisible(x)))
因为我们将在这两个语句之间添加一行代码。我们将用

plot.zoo2<-plot.zoo
bl<-as.list(body(plot.zoo2))
bl<-c(
    bl[1:19], 
    quote(axis(1,at=ticks,labels=tick.lables)), 
    bl[20]
)
body(plot.zoo2)<-as.call(bl)


不幸的是,当您绘制多个绘图时,默认的
plot.zoo
功能会在内部更改
par
设置,然后在该功能存在时自动重置设置。当它重置
par
参数时,这将重置打印曲面,因此对
轴的后续调用不起作用。仅绘制单个时间序列时,此函数不执行此操作

没有超简单/健壮的方法来解决这个问题。但是,如果您觉得很大胆,您可以对该函数执行手术并在其中插入新代码。(这不是一般推荐的做法,但在这种情况下,它会让你的生活更轻松)。我是用zoo版本1.7-10做的。要确保这对您有效,请检查

body(plot.zoo)[19:20]
返回

do.call("title", title.args)(return(invisible(x)))
因为我们将在这两个语句之间添加一行代码。我们将用

plot.zoo2<-plot.zoo
bl<-as.list(body(plot.zoo2))
bl<-c(
    bl[1:19], 
    quote(axis(1,at=ticks,labels=tick.lables)), 
    bl[20]
)
body(plot.zoo2)<-as.call(bl)


不幸的是,当您绘制多个绘图时,默认的
plot.zoo
功能会在内部更改
par
设置,然后在该功能存在时自动重置设置。当重置
par
参数时,将重置打印表面