'Plot ggplot for layer-specific group on a leaflet map in shiny
I have created a shiny app that renders a leaflet map. This also uses a function to allow the user to toggle between 4 different maps. I would also like to plot a ggplot line plot in the corner of the map specific to each group layer. E.g. when GDP is selected a scatter plot appears in the bottom right corner to show its relationship with happiness. I have tried to research how to do this, but have had no luck. Any suggestions would be greatly appreciated.
I would like for the first group (World Happiness Index) to not show a line plot, but for the subsequent 3 groups to show a unique plot each. For the sake of demonstration I do not mind a random ggplot being rendered.
My code for rendering the leaflet map in the server code is as follows:
output$map <- renderLeaflet({
map <- worldCountries %>%
leaflet() %>%
addTiles() %>%
setView( lat=10, lng=0 , zoom=2) %>% #sets default map pan
addPolygons(### happiness #########
data = worldCountries,
fillColor = ~mypalettewhi(happiness_score),
stroke=TRUE,
fillOpacity = 0.9,
color="white",
weight=0.7,
label = mytext,
labelOptions = label,
highlightOptions = highlight,
group = "World Happiness Index"
) %>%
addPolygons(### GDP #################
data = worldCountries,
fillColor = ~mypalettegdp(gdp),
stroke=TRUE,
fillOpacity = 0.9,
color="black", #black is added as highlight as yellows will blend
weight=0.7,
label = mytext,
labelOptions = label,
highlightOptions = highlightgdp,
group = "GDP"
) %>%
addPolygons(### population #################
data = worldCountries,
fillColor = ~mypalettepop(pop_density_log), #log is used to diminish extreme values
stroke=TRUE,
fillOpacity = 0.9,
color="white",
weight=0.7,
label = mytext,
labelOptions = label,
highlightOptions = highlight,
group = 'Population Density'
) %>%
addPolygons( ### covid stingency ##############
data = worldCountries,
fillColor = ~mypalettecovid(avg_covid_score),
stroke=TRUE,
fillOpacity = 0.9,
color="white",
weight=0.7,
label = mytext,
labelOptions = label,
highlightOptions = highlight,
group = "Covid-19 Stringency Score"
) %>%
addLayersControl( #controls layers
baseGroups = c("World Happiness Index", "GDP", "Population Density", "Covid-19 Stringency Score"), #base groups indicates these will be toggled between groups
options = layersControlOptions(collapsed = FALSE)
) %>%
addLegend( #happiness legend
values=~happiness_score,
opacity=0.9,
title = "World Happiness<br /> Index Score",
position = "bottomleft",
colors = c('#ffffb2', '#fed976', '#feb24c', '#fd8d3c', '#fc4e2a', '#e31a1c', '#b10026'),
labels = c("Less Happy", "", "", "", "", "", "More Happy"),
group = "World Happiness Index" #group it belongs to
) %>%
hideGroup(c('World Happiness Index','GDP', 'Population Density', 'Covid-19 Stringency Score')) %>%
showGroup('World Happiness Index')
})
My code for the function to toggle the map groups within the server code is:
#### update legend when the selected layer group changes ######################
observeEvent(input$map_groups, {
my_map <- leafletProxy("map") %>% clearControls()
if (input$map_groups == 'World Happiness Index'){#### Happiness Legend ####
my_map <- my_map %>%
addLegend(
opacity=0.9,
title = "World Happiness<br /> Index Score",
position = "bottomleft",
colors = c('#ffffb2', '#fed976', '#feb24c', '#fd8d3c', '#fc4e2a', '#e31a1c', '#b10026'),
labels = c("Less Happy", "", "", "", "", "", "More Happy"),
values = worldCountries$happiness_score
)
}else if (input$map_groups == 'GDP'){ #### GDP Legend ####
my_map <- my_map %>%
addLegend(
opacity=0.9,
title = "GDP per Capita (US$)",
position = "bottomleft",
colors = c('#ffffd3', '#d9f0a3', '#addd8e', '#7bce7c', '#41ab5d', '#238443', '#005a32'),
labels = c("Lower GDP", "", "", "", "", "", "Greater GDP"),
values = worldCountries$gdp
)
}else if (input$map_groups == 'Population Density'){
my_map <- my_map %>%
addLegend( #### pop density legend ####
opacity=0.9,
title = "Population Density<br /> (per km\u00B2)",
position = "bottomleft",
colors = c('#f2f0f7', '#dadaeb', '#bcbddc', '#9e9ac8', '#807dba', '#6a51a3', '#4a1486'),
labels = c("Less Dense", "", "", "", "", "", "More Dense"),
values = worldCountries$pop_density_log
)
}else{
my_map <- my_map %>%
addLegend( #code for covid legend
opacity=0.9,
title = "Covid-19 Stringency Score",
position = "bottomleft",
colors = c('#eff3ff', '#c6dbef', '#9ecae1', '#6baed6', '#4292c6', '#2171b5', '#084594'),
labels = c("Less Stringent ", "", "", "", "", "", "More Stringent"),
values = worldCountries$avg_covid_score
)
}
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|




