1

I have a Shiny app which has both a reactive table, and a ggplot2 line chart (time series) that uses the reactive table as the dataset. I am using scale_x_date(date_breaks = "1 week") in the line chart, and the min and max values of the dates seen change appropriately with the reactive table.

My problem is that I would like the week intervals to be a Saturday (corresponding with the end of our fiscal week, and all the timestamps in the reactive table) but ggplot2 defaults to showing Mondays. How may I change the x-axis to show Saturdays but also scale appropriately in response to the reactive table?

Saurabh Chauhan
  • 3,161
  • 2
  • 19
  • 46
Ralph
  • 255
  • 2
  • 18
  • Not exactly, since the dataset for my ggplot2 object is a reactive table so I don't think I can reference it in the sequence – Ralph Aug 09 '18 at 15:27

2 Answers2

0

How about like this, borrowing from here but using lubridate to set the lowest break at the preceding Saturday?

library(lubridate)

scale_x_date(breaks = seq(floor_date(min(data.set$week.start), unit = 'week',
   week_start = getOption('lubridate.week.start', 6)),  max(data.set$week.start),by="week"),
   labels = date_format(format = "%Y-%m-%d"))
Mako212
  • 6,787
  • 1
  • 18
  • 37
0

I just needed

output$fcstlinechart_weekly <- renderPlotly({
p1 <- ggplot2::ggplot(data= my_reactive_table(),
aes(x=Week_End_Date,y=Forecast)+
  geom_line()+ scale_x_date(
   breaks= unique(data_frame_source_for_reactive_table$Week_End_Date))
p2 <- ggplotly(p1)
p2$elementId <- NULL
p2
})

And the plot rendering was "smart enough" to only show date values that exist in the filtered reactive table.

Ralph
  • 255
  • 2
  • 18