4

I've noticed an unexpected difference in the rendering of a ggplot between local output and a Shiny application. The axes labels are entirely different:

Local: enter image description here Shiny App: enter image description here

The axes of the two plots differ by exactly 1 day - how very odd! What happened during the publishing to Shiny process?

Data

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

Local code

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

Shiny

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 Answer 1

4

1430434800 seconds since Unix epoch is 30 Apr 2015 23:00:00 GMT, which is dangerously close to end of the day.

When tz argument to as.POSIX* functions is missing or empty string, current (machine-dependent) timezone will be used.

It seems that you are currently at GMT or west, while R Shiny server is at GMT+1 or east. These two computers evaluate the same fixed point in time into different "wall-clock" times, and since reference point is close to midnight, there is artificial change of date.

To fix that, you should provide timezone definition, so POSIX* functions don't have to fallback to unpredictable default.

Looks like you can do that by giving tzone property an "GMT" value:

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

Sign up to request clarification or add additional context in comments.

2 Comments

Great! Thanks, I'll look into this - as you saw I'm GMT, so I'll try this in the morning.
This worked for me - I think it's useful to note there was an issue on ggplot2 about "scale_x_datetime converts POSIXct to local timezone" that I found aftet you gave me the hat tip to timezones being the issue - github.com/hadley/ggplot2/issues/533

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.