ggplot2本地和Shiny托管输出之间的差异

时间:2015-10-07 20:42:23

标签: r ggplot2 shiny

我注意到在本地输出和Shiny应用程序之间呈现ggplot的意外差异。轴标签完全不同:

本地: enter image description here 闪亮的应用程序: enter image description here

两个地块的轴正好相差1天 - 多么奇怪!在发布到Shiny流程期间发生了什么?

数据

data.df <- structure(list(Report.Date = structure(c(1430434800, 1433026800, 
1435618800, 1438297200, 1440975600, 1443567600, 1430434800, 1433026800, 
1435618800, 1438297200, 1440975600, 1443567600), tzone = "", class = c("POSIXct", 
"POSIXt")), variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L), .Label = c("Datasets.Deposited", "Datasets.Published"
), class = "factor"), value = c(0L, 21L, 32L, 43L, 56L, 73L, 
0L, 4L, 9L, 21L, 29L, 49L)), .Names = c("Report.Date", "variable", 
"value"), row.names = c(NA, -12L), class = "data.frame")

本地代码

library(ggplot2)
library(scales)
base <- ggplot(data.df, aes(Report.Date, value))
plot <- base + geom_area(aes(group = variable, fill= variable), position = 'identity') + geom_point(aes(color = series), color = "black")
plot <- plot + xlab("Date") + ylab("Number of Deposits/Published")
plot  +
  scale_y_continuous(breaks = seq(0,round(max(data.df$value)+5,-1),5)) +
  scale_x_datetime(breaks = date_breaks("1 month"), labels = date_format("%Y-%b-%d"), minor_breaks = "1 month") + 
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

闪亮

ui.R

shinyUI(
  fluidPage(
    plotOutput("plot")
  )
)

server.R

library(ggplot2)
library(scales)
shinyServer(
  function(input, output){
    output$plot <- renderPlot({
      data.df <- 
      base <- ggplot(data.df, aes(Report.Date, value))
      plot <- base + geom_area(aes(group = variable, fill= variable), position = 'identity') + geom_point(aes(color = series), color = "black")
      plot <- plot + xlab("Date") + ylab("Number of Deposits/Published")
      plot  +
        scale_y_continuous(breaks = seq(0,round(max(data.df$value)+5,-1),5)) +
        scale_x_datetime(breaks = "1 month", labels = date_format("%d-%b-%Y"), minor_breaks = "1 month") + 
        theme(axis.text.x = element_text(angle = 45, hjust = 1))
    }
    )
  }
)

1 个答案:

答案 0 :(得分:4)

自Unix时代开始以来的1430434800秒是格林威治标准时间2015年4月30日23:00:00,危险地接近当天结束。

tz函数的as.POSIX*参数缺失或空字符串current (machine-dependent) timezone will be used时。

您似乎目前在GMT或西部,而R Shiny服务器在GMT + 1或东部。这两台计算机将相同的固定时间点评估为不同的“挂钟”时间,并且由于参考点接近午夜,因此日期会有人为的变化。

要解决此问题,您应该提供时区定义,因此POSIX *函数不必回退到不可预测的默认值。

看起来您可以通过为tzone属性提供“GMT”值来实现这一目标:

data.df <- structure(list(Report.Date = structure(c(1430434800, 1433026800, 1435618800, 1438297200, 1440975600, 1443567600, 1430434800, 1433026800, 1435618800, 1438297200, 1440975600, 1443567600), tzone = "GMT", class = c("POSIXct", "POSIXt")), …