3

This is not a duplicate since none of the methods in that putative duplicate apply here. None of them lead to the warning going away.

In fact I got an answer here from Konrad below - use suppressMessages. In the link that is asserted as a possible duplicate, they suggest suppressWarnings, which does not work.


After finally figuring out how to get R to use my timezone on the ggplot date axis correctly (found scale_x_datetime in a post here, before it was using my local timezone even though the data had the timezone set already), but it now complains with a warning:

Scale for 'x' is already present. Adding another scale for 'x', which will replace the existing scale. 

This is annoying because I have to do this a lot, and don't want to get in the habit of ignore all warnings. How can I turn this off? I obviously have tried suppressWarnings (with and without print) and options(warn=-1).

  • R-Version is 3.1.3
  • ggplot2_1.0.1
  • scales_0.2.4

    library(lubridate,quietly=T,warn.conflicts=T)
    library(ggplot2,quietly=T,warn.conflicts=T)
    library(scales,quietly=T,warn.conflicts=T)
    
    
    sclip.time <-  ymd_hms("2014-06-16 00:00:00",tz="US/Pacific")
    eclip.time <-  ymd_hms("2014-06-17 23:59:59",tz="US/Pacific")
    
    sdata.time <-  ymd_hms("2014-06-16 00:00:00",tz="US/Pacific")
    edata.time <-  ymd_hms("2014-06-17 23:59:59",tz="US/Pacific")
    
    
    xdata <- seq(sdata.time,edata.time,length.out=100)  
    xfrac <- seq(0,4*3.1416,length.out=100)
    ydata <- pmax(0.25,sin(xfrac))
    ydata <- sin(xfrac)
    ddf <- data.frame(x=xdata,y=ydata)
    
    date_format_tz <- function(format = "%Y-%m-%d", tz = "UTC") {
      function(x) format(x, format, tz=tz)
    }
    
    options(warn=-1)
    
    suppressWarnings(
    ggplot(ddf) + 
      geom_line(aes(x,y),col="blue") +
      geom_vline(xintercept=as.numeric(sclip.time),color="darkred") +
      geom_vline(xintercept=as.numeric(eclip.time),color="darkgreen") +
      xlim(sclip.time,edata.time) +
      scale_x_datetime(  breaks = date_breaks("1 day"),
                         labels = date_format_tz("%Y-%m-%d %H:%M", tz="US/Pacific"))
    )
    

    enter image description here

Mike Wise
  • 22,131
  • 8
  • 81
  • 104

3 Answers3

9

You have to use the combination of suppressMessages and print as in the snippet below:

suppressMessages(print(
  ggplot(ddf) + 
    geom_line(aes(x,y),col="blue") +
    geom_vline(xintercept=as.numeric(sclip.time),color="darkred") +
    geom_vline(xintercept=as.numeric(eclip.time),color="darkgreen") +
    xlim(sclip.time,edata.time) +
    scale_x_datetime(  breaks = date_breaks("1 day"),
                       labels = date_format_tz("%Y-%m-%d %H:%M", tz="US/Pacific"))
))
Konrad
  • 17,740
  • 16
  • 106
  • 167
  • Tried that, didn't work. Tried it again just not to be sure. Still didn't work. – Mike Wise Apr 23 '15 at 12:31
  • @MikeWise, very odd I tried the code and it [runs fine](http://pastebin.com/QhszRDWL). – Konrad Apr 23 '15 at 12:34
  • I still have the warning. Would love to know how you got rid of it. – Mike Wise Apr 23 '15 at 12:39
  • 2
    That was it. I just noticed you used supressMessages, I was trying supressWarnings. By the way you don't have to use a print (in this case), it goes away without it. – Mike Wise Apr 23 '15 at 12:49
  • suppressMessages() without print() worked for me too. Just in case you are getting the plot object the command will be: suppressMessages(p <- ggplot(...)) – Ajay Jul 12 '23 at 02:43
2

Actually, the message does point to a problem with your following code snippet:

  ... + xlim(sclip.time,edata.time) +
  scale_x_datetime(  breaks = date_breaks("1 day"),
                     labels = date_format_tz("%Y-%m-%d %H:%M", tz="US/Pacific"))

Already the first command will add a scale, and the second command will replace that scale. So the message tells you that the first command has no effect.

You should combine the two and add the limits to scale_x_datetime:

  ... +
  scale_x_datetime(breaks = date_breaks("1 day"),
                   labels = date_format_tz("%Y-%m-%d %H:%M", tz="US/Pacific"),
                   limits = c(sclip.time,edata.time))
jarauh
  • 1,836
  • 22
  • 30
1

A way you might get what you want is to use the "try" function, with the option silent=T :

try(silent=T, [R-script]) 

It is generally a bad idea to do it that way, because you become blind to the errors that might occur, but if you are really certain of what you're doing..

Etienne
  • 53
  • 7