Interactive Map in R

Hello! I'm trying to recreate the US Centers for Disease Control (CDC) 2022 Monkeypox Outbreak Global Map but I'm not familiar with creating interactive maps and tutorials I have come across are very far from the simplicity of this map.

So far, I have a static map that needs to be interactive so it has a popup on mouse hover that shows the country name and number of cases. My code so far and a tribble of the subset of the CDC dataset are below:

tibble::tribble(
     ~country, ~cases,                                 ~category,                                ~asof,
    "Andorra",      4, "Has not historically reported monkeypox", "Data as of 05 Aug 2022 5:00 PM EDT",
  "Argentina",     31, "Has not historically reported monkeypox", "Data as of 05 Aug 2022 5:00 PM EDT",
  "Australia",     58, "Has not historically reported monkeypox", "Data as of 05 Aug 2022 5:00 PM EDT",
    "Austria",    160, "Has not historically reported monkeypox", "Data as of 05 Aug 2022 5:00 PM EDT",
    "Bahamas",      1, "Has not historically reported monkeypox", "Data as of 05 Aug 2022 5:00 PM EDT",
   "Barbados",      1, "Has not historically reported monkeypox", "Data as of 05 Aug 2022 5:00 PM EDT"
  )

# join tables --------------------------------------------------------------

countries_clean <- sqldf(
  "SELECT * FROM country_loc
  FULL OUTER JOIN mpx_cases
  ON country_loc.country = mpx_cases.country"
) |> 
  select(1:4, 6:8) |>
  drop_na()

# plot map --------------------------------------------------------------
world <- map_data("world")

ggplot() +
  geom_map(
    data = world, map = world,
    aes(long, lat, map_id = region),
    color = "black", fill = "lightgray", size = 0.1
  ) +
  geom_point(
    data = countries_clean,
    aes(longitude, latitude, size = cases, color = category)
  ) +
  theme_void() +
  theme(
    plot.title = element_text(face = "bold"),
    legend.position = "right"
  ) +
  labs(
    title = "2022 Monkeypox Outbreak Global Map",
    subtitle = "Data as of 05 Aug 2022 3:38:PM MST",
    caption = "Source: US Centers for Disease Control and Prevention"
  )

For an interactive map you can convert the ggplot map using ggiraph or plotly packages. For plotly you would save your plot to a variable and pass it to plotly::ggplotly(). Plotly is the easiest way but ggiraph might give you more options.

For an interactive map with popups you could also try the leaflet package. Leaflet for R - Popups and Labels

thanks @mrjoh3. I've been meaning to look into plotly, maybe now is the time...lol.

1 Like

Thanks @mrjoh3 , I finally solved this (using Plotly) with help from some online article. It's missing some aesthetics and not the perfect map (like the CDC website) I want it to be but it has the functionality I needed.

There's a Kaggle notebook for the complete code.

# plot map ------------------------------------------------------

g <- list(showland = TRUE,
          landcolor = toRGB("gray95"), 
          subunitcolor = toRGB("gray85"))

fig <- plot_ly(type = 'scattergeo', mode = 'markers')
fig <- plot_geo(countries_clean, lat = ~latitude, lon = ~longitude)
fig <- fig %>% add_markers(
  text = ~paste(country, category, paste("Cases:", cases), sep = "<br />"),
  color = ~category, symbol = I("square"), size = I(15), hoverinfo = "text"
)
fig <- fig %>% layout(
  title = '2022 Monkeypox Outbreak Global Map', geo = g
)

fig

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.