geom_vline() dateRangeInput()

740 Views Asked by At

I have set up a line graph in shiny. The x axis has dates covering 2014 to current date.

I have set up various vertical lines using geom_vline() to highlight points in the data.

I'm using dateRangeInput() so the user can choose the start/end date range to look at on the graph.

One of my vertical lines is in Feb 2014. If the user uses the dateRangeInput() to look at dates from say Jan 2016 the vertical line for Feb 2014 is still showing on the graph. This is also causing the x axis to go from 2014 even though the data line goes from Jan 2016 to current date.

Is there a way to stop this vertical line showing on the graph when it's outside of the dataRangeInput()? Maybe there's an argument in geom_vline() to deal with this?

library(shiny)
library(tidyr)
library(dplyr)
library(ggplot2)

d <- seq(as.Date("2014-01-01"),Sys.Date(),by="day")

df <- data.frame(date = d , number = seq(1,length(d),by=1))

lines <- data.frame(x = as.Date(c("2014-02-07","2017-10-31", "2017-08-01")),
                y = c(2500,5000,7500),
                lbl = c("label 1", "label 2", "label 3"))

#UI
ui <- fluidPage(

#date range select:
dateRangeInput(inputId = "date", label = "choose date range",
             start = min(df$date), end = max(df$date),
             min = min(df$date), max = max(df$date)),


#graph:
plotOutput("line") 

)



#SERVER: 
server <- function(input, output) {
data <- reactive({ subset(df, date >= input$date[1] & date <= input$date[2]) 
})

#graph:
output$line <- renderPlot({

my_graph <- ggplot(data(), aes(date, number )) + geom_line() +
  geom_vline(data = lines, aes(xintercept = x, color = factor(x)  )) +
  geom_label(data = lines, aes(x = x, y = y,
                               label = lbl, colour = factor(x),
                               fontface = "bold" )) +
  scale_color_manual(values=c("#CC0000", "#6699FF", "#99FF66")) +
  guides(colour = "none", size = "none")


 return(my_graph)

 })

}

shinyApp(ui = ui, server = server)

pic of graph:

1

There are 1 best solutions below

0
On

As mentioned by Aimée in a different thread:

In a nutshell, ggplot2 will always plot all of the data that you provide and the axis limits are based on that unless you specify otherwise. So because you are telling it to plot the line & label, they will appear on the plot even though the rest of the data doesn't extend that far. You can resolve this by telling ggplot2 what you want the limits of your x axis to be, using the coord_cartesian function.

# Set the upper and lower limit for the x axis
dateRange <- c(input$date[1], input$date[2])

my_graph <- ggplot(df, aes(date, number)) + geom_line() +
  geom_vline(data = lines, aes(xintercept = x, color = factor(x)  )) +
  geom_label(data = lines, aes(x = x, y = y,
                               label = lbl, colour = factor(x),
                               fontface = "bold" )) +
  scale_color_manual(values=c("#CC0000", "#6699FF", "#99FF66")) +
  guides(colour = "none", size = "none") +
  coord_cartesian(xlim = dateRange)