'How to print an interactive world map for a given input- flex_dashboard in R

I am working on a dashboard and the aim is to have a world map with the population growth rate for a given year, which can be selected from a dropdown. I am making it using flex_dashboard. I have got the data which has 265 countries and the population growth rate data ranges from 1961 to 2020. My data looks like this,

pop_F data

I have done a left join to add the pop_F data with world data by Region. My code is here:

world_map <- map_data("world")
world_map <- subset(world_map, region != "Antarctica")


world_map_joined <- left_join(world_map, pop_F, by = c('region' = 'Country'))


unique_Year <- pop_F %>% .$Year %>% unique %>% sort


selectInput("Year", label = h4("Please select a Year from the dropdown below:"),
    choices = unique_Year) # dropdown for selecting all the uniques years, from 1961 to 2020

renderPlotly({
  ggplotly(
  ggplot(pop_F %>% filter(Year == input$Year)) +
  geom_polygon(data = world_map_joined, aes(x = long, y = lat, fill = Population_growth_rate)) +
 labs(title = 'Population growth rate of the countries in a given year') +
  theme(text = element_text(family = "Gill Sans", color = "#FFFFFF")
        ,panel.background = element_rect(fill = "#444444")
        ,plot.background = element_rect(fill = "#444444")
        ,panel.grid = element_blank()
        ,plot.title = element_text(size = 30)
        ,plot.subtitle = element_text(size = 10)
        ,axis.text = element_blank()
        ,axis.title = element_blank()
        ,axis.ticks = element_blank()
        ,legend.position = "none"
        )
  )
})

I would like to view it as:

Desired output with the dropdown of year.

Presently, I have bar plot for this tab, as shown in the image but it looks really messy. But it changes dynamically with the input from the dropdown. I would like to have the same functionality on a world map. I am out of sorts here. I want to create this dashboard using flex-dashboard. current dashboard with bar plot

Thanks.

r


Sources

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

Source: Stack Overflow

Solution Source