'How to fill based on a "numeric" column in geom_polygon in Shiny App

I have this following Slider

sliderInput(
        inputId = "year",  #  variable name that is used in server.R input$var_name
        label = "Year Selector",  # Title tha appears above the slider
        min = 2005, max = 2020, # min and max on the slider
        value = 2007 # initial variable value
      )

And server code that has a plot command to display the map

birth_map_data <- reactive({
      full_join(vn_spatial_region, birthDataByRegion[c("Region/Year", as.character(input$year))], by = c("sub_region_en" = "Region/Year"))
    })
    
    vietnam_birth_map_data <- reactive({
      full_join(vietnam_prov_df, birth_map_data(), by = c("prov_names" = "Name"))
    })


    output$distPlot <- renderPlot({
      ggplot() +
      geom_sf(data = vn_spatial_region) +
      geom_polygon(data=vietnam_birth_map_data(), aes(fill = `as.character(input$year)`, x = long, y = lat, group = group), color = "grey80")
    })

The fill argument in the aes is based on the corresponding year, so for example in 2005 then it would be fill = /``2005``/ (in back ticks). However, in the reactive context, I got object 'as.character(input$year)' not found (in back ticks).

How would I tackle this problem?



Solution 1:[1]

have you tried:

output$distPlot <- renderPlot({
      ggplot() +
      geom_sf(data = vn_spatial_region) +
      geom_polygon(data=vietnam_birth_map_data(), aes(fill = input$year, x = long, y = lat, group = group), color = "grey80")
    })

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Big_J