Grant
Grant

Reputation: 87

In R shiny is there a way to freeze the x-axis on an interactive plot?

I am working on creating an interactive line graph using ggplot in R shiny. The way I am doing it is having the user in the UI select the values they want to see in the plot using a picker input. In the server the data is then filtered based on the selection from the user. I specified the x axis in ggplot using scale_x_continuous. I set a sequence to be 0 through 24 with breaks by 1. However when a user would select, say 1-10, the x-axis would change to only show 1-10.

I want the x-axis to remain 0 through 24 no matter what the user selects. Is there a way to do that? Would the solution be in specifying the ggplot x-axis limits in a different way limits or in how I am using the picker input to filter the data?

Thanks!

Here is my code if it helps:

library(shiny)
library(tidyverse)
library(shinyWidgets)

# Create sample dataset and name bin
set.seed(1234) 
hour <- sample(1:24, size=100, replace=TRUE)
calls <- sample(1:55, size=100, replace =TRUE)
mydata <- data.frame(hour,calls)
name <- mydata %>% 
  group_by(hour) %>% 
  summarize(n())

name_bin <- as.data.frame(name$hour)

ui <- fluidPage(
  h1 ("R shiny ggplot Question"),
  pickerInput(
    inputId = "myPicker",
    label = "Select Hour in the day",  
    choices = name_bin, 
    options = list(
      `actions-box` = TRUE,
      size = 10, 
      `selected-text-format` = "count > 3"),
    multiple = TRUE),
  box(
    width = 11,
    plotOutput("graph1"))
)

server <- function(input,output,session) {

output$graph1 <- renderPlot({

  g1 <- dplyr::filter(mydata,hour %in% input$myPicker) 

  g2 <- g1 %>% 
    group_by(hour) %>% 
    summarize(number = n())

  ggplot(g2, aes(x=hour, y=number)) + 
    geom_line(color = 'darkblue') + geom_point() +
    scale_x_continuous(breaks = seq(0,24, by=1)) +  ###### Better way to set x-limits?
    scale_y_continuous(breaks = seq(0, max(g2$number), by=5))+ 
    theme_minimal()+
    labs(y="Number of Calls", x="Hour in the day",title = "")
})
}

shinyApp(ui, server)

Upvotes: 2

Views: 910

Answers (1)

chemdork123
chemdork123

Reputation: 13833

There are a few ways to set the limits on your axis: you were almost there!

The call to scale_x_continuous in your renderPlot() function only sets the breaks in the axis (kind of how it's labeled). You want to set the limits, and you can do that within the scale_x_continuous function via the argument limits=. See the documentation for more information, but this should work for you:

scale_x_continuous(breaks = seq(0,24, by=1), limits = c(0,24))

Alternatively, if you just want to set the limits, but the default "breaks" in the axis are fine, you can access x and y limits more easily using xlim(0,24) in place of scale_x_continuous.

Use caution though: setting limits of the axis will result in dropping that data - meaning it won't be used in any further stat functions. If you still need to keep that data for other stat functions within the plot, you can use coord_cartesian() (see the documentation here).

Upvotes: 2

Related Questions