plotly / plotly/plotly.R

Choropleth Map produces just a coloured rectangle

Open
#1,811 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
2.7k
Forks
641
PR merge metrics
No merged PRs in 30d

Description

I am trying to draw a Plotly choropleth map using a custom geojson created using the Natural Earth 10m map-unit dataset. (I need Wales/Scotland/Ireland/England etc).

If I plot with mapbox I get the expected output (although my polygons don't quite fit the background) however if I try to just use choropleth I get a square of just one colour (I can still see hover values though).

A subset of my data for USSR countries:

    library(data.table)
    library(plotly)
    countries <- rjson::fromJSON(file="https://raw.githubusercontent.com/awgymer/map-data/master/ne_10m_admin_0_map_units.json")

    first_gms <- fread(
    "team_name,geounit,played_for
    USSR,BLR,14671
    USSR,TJK,6516
    USSR,UZB,12484
    USSR,MDA,7007
    USSR,UKR,739
    USSR,KAB,7182
    USSR,LVA,6559
    USSR,KAZ,7182
    USSR,ARM,10895
    USSR,RUS,6516
    USSR,AZE,11213
    USSR,KGZ,12481
    USSR,GEA,5518
    USSR,EST,14671
    USSR,TKM,6516
    USSR,GEG,5518
    USSR,LTU,6845")
    
    fig <-  plot_ly(first_gms) %>% add_trace(
      type="choroplethmapbox",
      geojson=boundaries,
      featureidkey="properties.GU_A3",
      z=~played_for,
      locations=~geounit,
      colorscale="Viridis",
      marker=list(line=list(
        width=0)
      )
    ) %>% layout(
      mapbox=list(
        style="carto-positron"
      )
    )
    
    
    fig2 <-  plot_ly(first_gms) %>% add_trace(
      type="choropleth",
      geojson=boundaries,
      featureidkey="properties.GU_A3",
      z=~played_for,
      locations=~geounit,
      colorscale="Viridis",
      marker=list(line=list(
        width=0)
      )
    ) %>% layout(
      geo = list(resolution=50)
    )

fig works but fig2 is just all one colour. I cannot for the life of me work out the reason!

As an addendum: Is there any way to specify hover for countries which have no value in z/locations?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the two traces in the issue with the linked Natural Earth GeoJSON and compare the working choroplethmapbox output with the flat-colour choropleth output. Trace how the R choropleth trace handles geojson, featureidkey, locations, and z; done means the standard choropleth displays the country values distinctly, with the missing-location hover behavior addressed or clarified.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
data-visualization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.