这不是重复项,因为假定的重复项中的方法均不适用于此处。它们都不会导致警告消失。
事实上,我从下面的 Konrad 那里得到了答案——使用 suppressMessages
。在被断言为可能重复的链接中,他们建议使用 suppressWarnings
,但这是行不通的。
在最终弄清楚如何让 R 在 ggplot
日期轴上正确使用我的时区之后(在此处的帖子中找到 scale_x_datetime
,然后才使用我的本地时区即使数据已经设置了时区),但它现在会发出警告:
Scale for 'x' is already present. Adding another scale for 'x', which will replace the existing scale.
这很烦人,因为我必须经常这样做,并且不想养成忽略所有警告的习惯。我怎样才能关闭它?我显然已经尝试过 suppressWarnings
(有和没有打印)和 options(warn=-1).
- R 版本为 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")) )
最佳答案
您必须使用 suppressMessages
和 print
的组合,如下面的代码片段所示:
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"))
))
关于r - 在 scale_x_datetime 中抑制警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29823327/