Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Supressing Warnings in scale_x_datetime

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

like image 482
Mike Wise Avatar asked Oct 21 '25 13:10

Mike Wise


1 Answers

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"))
))
like image 175
Konrad Avatar answered Oct 23 '25 05:10

Konrad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!