Supressing Warnings in scale_x_datetime

谁说胖子不能爱 提交于 2020-04-17 04:05:10

问题


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


回答1:


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



回答2:


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




回答3:


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


来源:https://stackoverflow.com/questions/29823327/supressing-warnings-in-scale-x-datetime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!