How can I combine Choropleth and Scatter layer in a plotly map?

Hello!

I’m trying to switch from RShiny to Dash and I have some trouble with recreating pretty basic map stuff in Plotly.
I want to make scattermap on top of a layer of custom choropleths (neighbourhoods in NL). These choropleths have a value that is the sum of an attribute of the points in a scatter layer.

Is there an option, like there is for instance in leaflet, to make a scatterplot layer and a custom choropleth layer from a panda’s geodataframe and overlay them on a basemap? See the example below, made with R and Leaflet:

I’ve seen some examples of adding rasters in the layers component of the layout, but I haven’t seen a working example with choropleth and scatterplot layers and it seems like you don’t have as much control over the second layer if you do it this way.

Thanks in advance!

Something of the following form should work

choropleth_data = {
     # Your choropleth data here
}

scatter_data = {
     # Your scatter data here
}

figure = {
    "data": [
        choropleth_data,
        scatter_data
    ]
   "layout": {
        # Your layout dictionary here
   }
}

Thank you all so much! This worked :slight_smile:

Hello. Having a lot of difficulty with this as well. Surprised there’s not a lot I can find on Google. Currently the Jupyter link doesn’t work (@empet). Also, I’m not really sure how to use @sjtrny’s example using R.

@joeflack4

To define a go.Figure() that contains both a chroplethmapbox and a scattermapbox, proceed as follows:

fig = go.Figure(go.Chroplethmapbox()) # here you set all attributes needed for a Choroplethmapbox
fig.add_scattermapbox(lat = your_list_of_latitudes,
                      lon = your_list_of_longitudes,
                      mode = 'markers+text',
                      text = some_text,  #a list of strings, one  for each geographical position  (lon, lat)              
                      below='',                 
                      marke_size=12, marker_color='rgb(235, 0, 100)'))
fig.update_layout((title_text ='Your plot title', title_x =0.5, 
                   mapbox = dict(center= dict(lat=52.370216, lon=4.895168),  #change to the center of your map          
                                 accesstoken= "your-mapbox-access-token",
                                 zoom=6, #change this value correspondingly, for your map
                                 style="light"  # set your prefered mapbox style
                               ))

Thanks for taking the time to help! This definitely looks like a solution in R, per my request.

However, it looks like I have two issues:

1. I didn’t describe my requirements properly

My reqs are: chloropleth + scatter + open street maps + ggplotly

I think the issue I have might be ggplotly…

2. Couldn’t find those functions in the plotly library

Is there a reason this isn’t working?

> library('plotly')
> 
> go.Figure()
Error in go.Figure() : could not find function "go.Figure"
> 

@joeflack4
Unfortunately you posted your question within an old thread under plotly.py category. I don’t work with R, hence I cannot suggest any solution. I’m sorry!!

Hi @joeflack4

Here is the documentation for Plotly R, and this section has examples of maps with scatter plots. If this doesn’t answer your question you could try creating a new topic using the R tag.

Hey guys,

Thanks. Weird, the URL and the breadcrumbs at the top don’t say anything about Python, at least I can’t see.

Appreciate. I do think this is a ggplotly() issue. I’m just moving to native plot_ly but will post over there if I want to go back.

Hi there, so this helps and was able to overlay points over a set of boundaries. But i also intend to be able to select points by drawing polygon or lasso select , the app breaks in case i try lasso select.

Any idea why? or how can i achieve the same

image

I am able to carry out a lasso select when i have the scattermap only but when i overlay it over a choropleth with boundaries i am not able to.