Can not create choropleth map with overlaying bubbles because choropleth data disappears

I'm having a bit of trouble with my map and was wondering if you guys had any ideas.

I'm trying to create a choropleth map displaying carbon emissions, with overlaying bubbles displaying something called GAIN. However, although every time I add the bubbles into the Shiny code, I lose nearly all visualization of the carbon emissions (see below).

image

If I reverse the order of my output$map by putting the bubbles before the choropleth fill, I gain back the visualization of the carbon emission data but lose all but 3 of my bubbles?

...#Installing Needed Packages to Run
library(leaflet)
library(tmap)
library(RColorBrewer)
library(rgeos)
library(rgdal)
library(shiny)
library(sp)

install and Process Data
carbontest <-read.csv("carbontest.csv")
Output.Areas<- readOGR(".", "ne_50m_admin_0_countries")
Meshblocktest <- merge(Output.Areas, carbontest, by.x="SOVEREIGNT", by.y="country_name")

#Carbon Emission Point Data
survey.marks <- read.csv("TESTTEST5.csv")
Survey.Points <- SpatialPointsDataFrame(survey.marks[,2:3], survey.marks,proj4string = CRS("+init=EPSG:4167"))
Survey.Points <- spTransform(Survey.Points, CRS("+init=epsg:4167"))
meshblocktesttransform <- spTransform(Meshblocktest, CRS("+init=epsg:4167"))

#The App
ui <- fluidPage(

mainPanel(
leafletOutput("map")
)
)
server <- function(input, output) {
output$map <- renderLeaflet({
tm <- tm_shape(meshblocktesttransform) +tm_fill("CarbonEmissions", palette = "Reds", style = "quantile") +
tm_shape(Survey.Points) + tm_bubbles(size = "GAIN", col = "GAIN", palette = "BuPu", style = "quantile") +

tmap_leaflet(tm)

})
}

shinyApp(ui, server)

I can not reproduce your code without your data, so just a general comment: consider switching your code from using package tmap to utilizing leaflet library directly (meaning via the leaflet package). Leaflet plays nicely with Shiny and there is even a tutorial by RStudio https://rstudio.github.io/leaflet/shiny.html

It can handle the polygons from Natural Earth easily, and leaflet::addCircleMarkers() is functionally equivalent to tmap::tm_bubbles().

To illustrate my point consider this mock code - built on Natural Earth polygons (we seem to be using the same, it is a good piece of data:) and Metro dataset from the tmap package.

For the sake of clarity I have omitted the shiny part.

library(tidyverse)
library(leaflet)
library(tmap) # for the Metro dataset
library(sf)

data("metro") # from tmap

metro <- metro %>% 
  select(pop = pop2020) %>% 
  mutate(pop = pop / max(pop)) # normalise the values

mapa <- st_read("./data/maps/ne_50m_admin_0_countries.shp") # replace with your path :)

pal <- colorNumeric(palette = "Reds",  domain = mapa$GDP_MD_EST)

leaflet() %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  setView(lng = 14.46, lat = 50.07, zoom = 1) %>%
  addPolygons(data = mapa, 
              fillColor = ~pal(mapa$GDP_MD_EST),
              fillOpacity = 0.75,
              stroke = F) %>% 
  addCircleMarkers(data = metro,
                   radius = 20 * metro$pop,
                   stroke = F)

2 Likes

Thanks mate! I ended up just switching from tm_bubbles to tm_dots because I was in a rush for a solution, but that map looks awesome

Glad to be of service! :slight_smile:

As is often the case you gain more control by using a tool (leaflet library in this case) directly, at the cost of having to spend some time learning new commands and interface.

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