I am attempting to create a ggplot in shiny that responds to user-defined input related to date. Seems like a straight forward problem and one that I've successfully accomplished before.
In this problem, I have values for MODIS in the dataframe weatherYear that the user is selecting. Each 16 calendar days is a distinct MODIS value (e.g., Jan 1-16 = 1, Jan 17-Feb 1 = 2, and so forth. This reveals weather data in similar time increments as satellite imagery shown in another window of my shiny app.
If I use the following code in R (adapted as necessary), and vary the MODIS value, the graphs come out perfectly.
output$weather <- renderPlot({
weatherData <- filter(weatherYear, weatherYear$MODIS <= input$index)
ggplot(weatherData, aes(x=DOY), order = DOY) +
geom_ribbon(aes(ymin = TempMin, ymax = TempMax), alpha = 0.3, fill = "indianred2") +
geom_line(aes(y = TempAvg), color = "black") +
geom_bar(aes(y = Precip*20), stat = "identity", color = "blue") +
labs(y = "Mean Daily Temperature (F)", x = "Month") +
coord_cartesian(ylim = c(-5,105), xlim = c(1, 365)) +
scale_y_continuous(sec.axis = sec_axis(~./20, name="Precipitation (in)")) +
scale_x_continuous(expand = c(0, 0),
breaks = c(15,45,75,105,135,165,195,228,258,288,320,350),
labels = c("J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D"))
In shiny, the default value of 1 shows perfectly:
Shiny ggplot for default MODIS period
However, as soon as another index period is selected, a large gap is introduced and additional non-selected index values are graphed:
Same ggplot when MODIS index value of 2 is selected
I'm stumped...