0

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.

(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.

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.

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.

Community
  • 1
  • 1
patapouf_ai
  • 17,605
  • 13
  • 92
  • 132
  • Did you run the code that creates the example data in your question? It doesn't run. You need to pass a character vector to `as.Date(...)` to create Dates this way. Also, `other=(...)` is syntactically incorrect. – jlhoward Dec 03 '14 at 20:29
  • so by using axis.Date instead of axis I can get normal looking dates, but it doesn't give the number of ticks specified in pretty. – patapouf_ai Dec 03 '14 at 20:31
  • hey jlhoward. thanks. indeed you are right. above I forgot to put the quotes arround the dates and the c in other. let me see if I can edit that. – patapouf_ai Dec 03 '14 at 20:32
  • I try to use pretty.Date but that just gives me the error that it can't find pretty.Date – patapouf_ai Dec 03 '14 at 20:42
  • `pretty.Date` [is in the `grDevices` package](http://www.rdocumentation.org/advanced_search?utf8=%E2%9C%93&q=&package_name=&function_name=pretty.Date&title=&description=&author=). So load that... – Gregor Thomas Dec 03 '14 at 20:49
  • Thanks @Gregor . I have grDevices loaded. But it still gives me that error. If I just use pretty on the dates it doesn't give any errors. It looks like pretty.Date can't be called directly but must be called from the wrapper pretty? – patapouf_ai Dec 03 '14 at 21:29
  • Yeah, looks like you're correct. Calling `grDevices:::pretty.Date()` is equivalent to `pretty()` on a `Date` object. Why were you trying to call `pretty.Date()` directly? – Gregor Thomas Dec 03 '14 at 21:38
  • because calling pretty is not giving me the desired outcome ... and because that trick helped with axis. – patapouf_ai Dec 03 '14 at 22:13

1 Answers1

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

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:

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

enter image description here

jbaums
  • 27,115
  • 5
  • 79
  • 119
  • Thank you! That is exactly what I was looking for! Sorry I can't upvote you yet because I don't have enough reputation, I'll do it once I'm able to. – patapouf_ai Dec 05 '14 at 12:55