如何在R中使用2个不同的y轴和X轴上的DATE进行绘图?

时间:2021-11-24 05:32:50

Basically the question is the same as this one: How can I plot with 2 different y-axes? except that I have a date on the x axis and pretty(range()) doesn't work for me using dates.

基本上问题与这个问题相同:我如何用2个不同的y轴绘图?除了我在x轴上有一个日期和漂亮(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个蜱,因为漂亮的要求。

1 个解决方案

#1


1  

For axis.Date, you need to specify the argument name at when setting the tick/label positions. 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,您需要在设置刻度/标签位置时指定参数名称。如果你不这样做,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. However, the intervals are inconsistent. Perhaps it might be better to specify a date sequence, such as:

产生你想要的10个滴答。但是,间隔不一致。也许最好指定一个日期序列,例如:

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')

如何在R中使用2个不同的y轴和X轴上的DATE进行绘图?

#1


1  

For axis.Date, you need to specify the argument name at when setting the tick/label positions. 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,您需要在设置刻度/标签位置时指定参数名称。如果你不这样做,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. However, the intervals are inconsistent. Perhaps it might be better to specify a date sequence, such as:

产生你想要的10个滴答。但是,间隔不一致。也许最好指定一个日期序列,例如:

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')

如何在R中使用2个不同的y轴和X轴上的DATE进行绘图?