Reactive axis labels in shiny with ggplot - display user selected label NOT variable name

I would like to make the axis labels in my Shiny plot display the LABEL of the variable (the one the user sees and selects) as opposed to the variable name itself.

So, ideally, if the user selected "Crime share" to plot on the y axis, the y axis label would say "Crime share", as opposed to the variable name "CRIMESHARE". My chart does the latter right now.

I am new to Shiny, and I have searched for an understandable answer to this issue, but I am stumped. I have tried to write a reactive with if else statements and save the ideal axis name to y_label. But putting either "y_label" or y_label() in ggplots labs(y = ) does not work.

Any suggestions on how best to approach that? Or is there a simpler more Shiny way of approaching this?

Thank you.

Sample data:

structure(list(ROADACCIDENT = c(142.69999694825, 120.40000152588, 
130.19999694825, 155.10000610352, NA, 181.10000610352, 54.20000076294, 
118.09999847413, 85.19999694825, 112.40000152588), CRIMESHARE = c(3142, 
1695, 1554, 2806, 3368, 3164, 1359, 2013, 711, 2446), MURDER = c(267, 
605, 309, 185, 65, 590, 93, 631, 501, 138), NAME = c("Khabarovsk Krai", 
"Chelyabinsk Oblast", "Novosibirsk Oblast", "Amur Oblast", "Magadan 
Oblast", 
"Krasnoyarsk Krai", "Karachay-Cherkess Republic", "Moscow", "Moscow", 
"Kostroma Oblast"), ID = c(29, 13, 49, 5, 41, 36, 26, 44, 44, 
 34), YEAR = c(2009, 1992, 1990, 2007, 2001, 2007, 1999, 2009, 
1991, 2000)), row.names = c(608L, 255L, 1009L, 102L, 852L, 753L, 
535L, 923L, 905L, 704L), class = "data.frame")

The UI:

library(shiny)
library(tidyverse)
library(stringr)
library(rsconnect)
library(shinythemes)
library(plotly)
data(crime_master)
crime_plot <- crime_master

ui <- fluidPage(theme = shinytheme("cerulean"),

titlePanel("My title"),

 sidebarLayout( 
  sidebarPanel( 

   h3("Select the inputs"),
   selectInput(inputId = "y", 
               label = "Indicator to display on Y-axis", 
               choices = c("Road accidents" = "ROADACCIDENT", 
                           "Crime share" = "CRIMESHARE", 
                           "Murders" = "MURDER"), 
              selected = "ROADACCIDENT"),

   selectizeInput(inputId = "region", 
                  label = "Select regions", 
                  choices = c(crime_plot$NAME), 
                  multiple = TRUE,
                  options = list(maxItems = 5))), 
 mainPanel(
   h3("Plot indicators over time"),
   plotlyOutput(outputId = "scatterplot"))))

The server:

server <- function(input, output){
  regions_subset <- reactive({ 
   req(input$region)
    filter(crime_plot, NAME %in% input$region)})

 y_label <- reactive({
    req(input$y)
    if(input$y == "ROADACCIDENT"){
      y_label <- "Road accidents"
    } else if(input$y == "CRIMESHARE"){
      y_label <- "Crime share"
    } else if(input$y == "MURDER"){
      y_label <- "Murders"
      }})


  output$scatterplot <- renderPlotly({
  ggplotly(ggplot(data = regions_subset(), 
                 aes_string(x = "YEAR", y = input$y, color = "NAME")) + 
                 geom_point() + 
                 labs(x = "Year", y = y_label()) +
                 scale_color_discrete(name = "Regions"))})

  output$event <- renderPrint({
    k <- event_data("plotly_hover")
    if (is.null(k)) "Hover on a point!" else d})}

shinyApp(ui = ui, server = server)

Hi @mlupion,

for me it is working so not sure why it's not working for you.
You can make it simpler though by putting the y-options in a variable, so you can use it later to get the right name. See below. Does this work for you?

library(shiny)
library(tidyverse)
library(stringr)
library(rsconnect)
library(shinythemes)
library(plotly)

crime_plot <- structure(list(ROADACCIDENT = c(142.69999694825, 120.40000152588, 
                                              130.19999694825, 155.10000610352, NA, 181.10000610352, 54.20000076294, 
                                              118.09999847413, 85.19999694825, 112.40000152588), 
                             CRIMESHARE = c(3142, 1695, 1554, 2806, 3368, 3164, 1359, 2013, 711, 2446), 
                             MURDER = c(267,605, 309, 185, 65, 590, 93, 631, 501, 138), 
                             NAME = c("Khabarovsk Krai", "Chelyabinsk Oblast", "Novosibirsk Oblast", "Amur Oblast", "Magadan Oblast", "Krasnoyarsk Krai", 
                                      "Karachay-Cherkess Republic", "Moscow", "Moscow", "Kostroma Oblast"), 
                             ID = c(29, 13, 49, 5, 41, 36, 26, 44, 44, 34), YEAR = c(2009, 1992, 1990, 2007, 2001, 2007, 1999, 2009, 1991, 2000)), 
                             row.names = c(608L, 255L, 1009L, 102L, 852L, 753L, 535L, 923L, 905L, 704L), class = "data.frame")
                        
crime_options <- c("Road accidents" = "ROADACCIDENT", 
                   "Crime share" = "CRIMESHARE", 
                   "Murders" = "MURDER")                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                                                         
ui <- fluidPage(theme = shinytheme("cerulean"),
                titlePanel("My title"),
                sidebarLayout( 
                  sidebarPanel( 
                    h3("Select the inputs"),
                    selectInput(inputId = "y", 
                                label = "Indicator to display on Y-axis", 
                                choices = crime_options, 
                                selected = crime_options[1]),
                    
                    selectizeInput(inputId = "region", 
                                   label = "Select regions", 
                                   choices = c(crime_plot$NAME), 
                                   multiple = TRUE,
                                   options = list(maxItems = 5))), 
                  mainPanel(
                    h3("Plot indicators over time"),
                    plotlyOutput(outputId = "scatterplot"))))

server <- function(input, output){
  regions_subset <- reactive({ 
    req(input$region)
    filter(crime_plot, NAME %in% input$region)})

  output$scatterplot <- renderPlotly({
    ggplotly(ggplot(data = regions_subset(), 
                    aes_string(x = "YEAR", y = input$y, color = "NAME")) + 
               geom_point() + 
               labs(x = "Year", y = names(crime_options[which(crime_options == input$y)])) +
               scale_color_discrete(name = "Regions"))})
  
  output$event <- renderPrint({
    k <- event_data("plotly_hover")
    if (is.null(k)) "Hover on a point!" else d})}

shinyApp(ui = ui, server = server)```
1 Like

This works beautifully. Thank you!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.