简体   繁体   English

如何在X轴上以DATE绘制2个不同的y轴?

[英]How can I plot with 2 different y-axes in R with DATE on X axis?

Basically the question is the same as this one: How can I plot with 2 different y-axes? 基本上,问题与这一问题相同: 如何绘制2个不同的y轴? except that I have a date on the x axis and pretty(range()) doesn't work for me using dates. 除了我在x轴上有一个日期,而且pretty(range())对于使用日期的方式对我不起作用。

(sorry I don't have enough "reputation" to comment the above question to ask for more info so I need to start a new question). (对不起,我没有足够的“声誉”来评论以上问题,以寻求更多信息,因此我需要开始一个新问题)。

I wrote the following function: 我写了以下函数:

  PvsRef<-function(id,feature){
  ymax= max(data[[feature]], na.rm=TRUE)
  plotdata = data[data$ID==id,]

  plot(plotdata$date,plotdata$ref, type="l", col="red", axes=FALSE, ylim=c(0,48), main=paste0("Reference vs.",feature), sub=as.character(id),  xlab="", ylab="")
  axis(2, ylim=c(0,48),col="red",las=1)
  mtext("Reference",side=2,col="red",line=2.5)
  box()

  par(new=TRUE)

  plot(plotdata$date,plotdata[[feature]], type="b", col="blue", axes=FALSE, ylim=c(0,ymax),  xlab="", ylab="")
  mtext(feature,side=4,col="blue",line=4) 
  axis(4, ylim=c(0,ymax), col="blue",col.axis="blue",las=1)

  axis(1,pretty(range(plotdata$date),10))
  mtext("Date",side=1,col="black",line=2.5)  


  axis(1,pretty(range(plotdata$date),10))
  mtext("Date",side=1,col="black",line=2.5)  


  }

the x-axis however shows weird numbers instead of dates. 但是,x轴显示的是奇怪的数字而不是日期。

example data: 示例数据:

data = data.frame(ID=c(1,1,1,1,1,1), date=as.Date(c("2000-01-01","2000-02-01","2000-03-01","2000-04-01","2000-05-01","2000-06-01")), ref=c(30,23,43,12,34,43), other=c(120,140,230,250,340,440))

then I want to run the function using 然后我想使用运行该功能

PvsRef(1,"other")

but no proper x-axis appears. 但没有出现正确的x轴。

EDIT: 编辑:

if instead I use 相反,如果我使用

  axis.Date(1,pretty(range(plotdata$date),10))
  mtext("Date",side=1,col="black",line=2.5)

proper dates show up, but there isn't 10 ticks as demanded by pretty. 正确的日期出现了,但是没有漂亮的日期要求的10个刻度。

For axis.Date , you need to specify the argument name at when setting the tick/label positions. 对于axis.Dateat设置刻度/标签位置时,需要在处指定参数名称。 If you fail to do this, axis.Date treats the second argument as x , which results in a suitable grid of labels being chosen (see ?axis.Date ). 如果不这样做, axis.Date会将第二个参数视为x ,这将导致选择合适的标签网格 (请参见?axis.Date )。

Changing that line to, for example: 将该行更改为例如:

axis.Date(1, at=pretty(range(plotdata$date), 10), format='%d %b', las=2, 
          cex.axis=0.8)`

yields the 10 ticks you are looking for. 产生您正在寻找的10个滴答声。 However, the intervals are inconsistent. 但是,时间间隔不一致。 Perhaps it might be better to specify a date sequence, such as: 也许指定一个日期序列可能更好,例如:

seq(min(plotdata$date), max(plotdata$date), length.out=10)

So your function could be adjusted to: 因此您的功能可以调整为:

f <- function(id, feature) {
  opar <- par()
  on.exit(suppressWarnings(par(opar)))
  par(mar=c(6, 4, 4, 5))
  ymax <- max(data[[feature]], na.rm=TRUE)
  plotdata <- data[data$ID==id,]

  plot(plotdata$date, plotdata$ref, type="l", col="red", axes=FALSE, 
       ylim=c(0, 48), main=paste0("Reference vs.", feature), 
       sub=as.character(id), xlab="", ylab="")
  mtext("Reference", side=2, col="red", line=2.5)
  axis(2, ylim=c(0, 48), col='red', col.axis="red", las=1)

  par(new=TRUE)  
  plot(plotdata$date, plotdata[[feature]], type="b", col="blue", axes=FALSE, 
       ylim=c(0,ymax),  xlab="", ylab="")
  mtext(feature, side=4, col="blue", line=3) 
  axis(4, ylim=c(0, ymax), col="blue", col.axis="blue", las=1)

  axis.Date(1, at=seq(min(plotdata$date), max(plotdata$date), length.out=10), 
            format='%d %b', las=2, cex.axis=0.8)
  box(lwd=2)
}  

f(1, 'other')

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM