0
votes

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. Should I write a reactive statement with a series of if statements (i.e. if the user selected "CRIMESHARE" --> "Crime share")? If so, any suggestions on how best to approach that? Or is there a simpler more Shiny way of approaching this? I can generate sample data if need be, but I figured this is more of a conceptual questions.

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)})

  output$scatterplot <- renderPlotly({
  ggplotly(ggplot(data = regions_subset(), 
                 aes_string(x = "YEAR", y = input$y, color = "NAME")) + 
                 geom_point() + 
                 labs(x = "Year", y = 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
Hi loops, can you tell us how to find crime_master data? I loaded up all the packages you specify above and data is not in them. Which package is it in? I think I may have a possible solution to your problem. Thanks :) - mysteRious
I generated a sample data frame with ten entries from crime_master (which is huge and imported from an rds file). I added it in the question. Does that work? - loops

1 Answers

2
votes

when I remove req(inputs$region) reactive, the graph changes axis labels whenever you change regions or crime type. You can also add theme in the ggplotly so that the words do not overlap with the axis label as it changes:

server <- function(input, output){

regions_subset <- reactive({ 
   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 = input$y) +
             theme(text=element_text(size=10), 
                   axis.text.y=element_text(angle=90, hjust=1)) +
             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)

If you want to use the labels and not the variable names, change just these two chunks:

 selectInput(inputId = "y", 
            label = "Indicator to display on Y-axis", 
            choices = c("Road accidents", 
                        "Crime share", 
                        "Murders"), 
            selected = "Road accidents"),

AND

output$scatterplot <- renderPlotly({
yvarnames <- c("ROADACCIDENT","CRIMESHARE","MURDER")
ggplotly(ggplot(data = regions_subset(), 
           aes_string(x = "YEAR", y = yvarnames[input$y], color = "NAME")) + 
           geom_point() + 
           labs(x = "Year", y = input$y) +
           theme(text=element_text(size=10), axis.text.y=element_text(angle=90, hjust=1)) +
           scale_color_discrete(name = "Regions"))
})