The goal is to create an interactive (single) plot in Shiny in which the user can select any of the wanted variables of the dataframe. For now the variable freq_prov_tot is plotted. The plot itself is working, but I do not know how to include other variables to "choose" from in an interactive manner. Also a tooltip is included. I'm using ggiraph due to the posibility of integrating this interactive polygon map into Shiny quite easily.
If you have any idea how this can be fixed, help is very much appreciated.
The dataframe I'm using is as follows. For protection of the used data all provinces are given a value of 1 for the variable freq_prov_tot.
counties_e<- fortify(Iran3, region = "NAME_1")
counties_e$freq_prov_tot<- ifelse(counties_e$id == "Alborz",1,
ifelse(counties_e$id == "Ardebil",1,
ifelse(counties_e$id == "Bushehr",1,
ifelse(counties_e$id == "Chahar Mahall and Bakhtiari",1,
ifelse(counties_e$id == "East Azarbaijan",1,
ifelse(counties_e$id == "Esfahan",1,
ifelse(counties_e$id == "Fars",1,
ifelse(counties_e$id == "Gilan",1,
ifelse(counties_e$id == "Golestan",1,
ifelse(counties_e$id == "Hamadan",1,
ifelse(counties_e$id == "Hormozgan",1,
ifelse(counties_e$id == "Ilam",1,
ifelse(counties_e$id == "Kerman",1,
ifelse(counties_e$id == "Kermanshah",1,
ifelse(counties_e$id == "Khuzestan",1,
ifelse(counties_e$id == "Kohgiluyeh and Buyer Ahmad",1,
ifelse(counties_e$id == "Kordestan",1,
ifelse(counties_e$id == "Lorestan",1,
ifelse(counties_e$id == "Markazi",1,
ifelse(counties_e$id == "Mazandaran",1,
ifelse(counties_e$id == "North Khorasan",1,
ifelse(counties_e$id == "Qazvin",1,
ifelse(counties_e$id == "Qom",1,
ifelse(counties_e$id == "Razavi Khorasan",1,
ifelse(counties_e$id == "Semnan",1,
ifelse(counties_e$id == "Sistan and Baluchestan",1,
ifelse(counties_e$id == "South Khorasan",1,
ifelse(counties_e$id == "Tehran",1,
ifelse(counties_e$id == "West Azerbaijan",1,
ifelse(counties_e$id == "Yazd",1,
ifelse(counties_e$id == "Zanjan",1, 0)))))))))))))))))))))))))))))))
The code for the tooltip
provinces_e <- sprintf("<p>%s</p>",
as.character(counties_e$id) )
table_e <- paste0(
"<table><tr><td>Total Number of Environmental Issues:</td>",
sprintf("<td>%.0f</td>", counties_e$freq_prov_tot),
"</tr></table>"
)
counties_e$labs <- paste0(provinces_e, table_e)
the code for the ui and server
ui_e <- fluidPage(
# Application title
titlePanel("Spatial Distribution of Environmental Issues in Iran during 1930 - 2018"),
fluidRow(column(12,
ggiraph::ggiraphOutput("county_map")))
)
server_e <- function(input, output) {
output$county_map<- renderggiraph({
p<- ggplot(counties_e, aes(x=long, y=lat, group = group, fill = freq_prov_tot)) +
xlab("Longitude") + ylab("Lattitude") + labs(fill = "Number of Environmental Issues") +
coord_map("polyconic" ) +
geom_polygon_interactive(aes(tooltip = labs))
ggiraph(code = print(p))
})
}
shinyApp(ui = ui_e, server = server_e)
Counties_e can be downloaded with the following link:
https://drive.google.com/file/d/1TOyZIADTCnRFyWLehxS7Td9v-BOZXARS/view?usp=sharing