Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a choropleth map using plot_geo()?

I am trying to plot a choropleth map in R and found some very promising code using the function plot_geo() here: https://plot.ly/r/choropleth-maps/. Unfortunately I could not find out how to adapt the code to my problem and I couldn’t find helpful explanations on the internet.

Here's the original code:

# specify some map projection/options
g <- list(
  scope = 'usa',
  projection = list(type = 'albers usa'),
  showlakes = TRUE,
  lakecolor = toRGB('white')
)

plot_geo(df, locationmode = 'USA-states') %>%
  add_trace(
    z = ~total.exports, text = ~hover, locations = ~code,
    color = ~total.exports, colors = 'Purples'
  ) %>%
  colorbar(title = "Millions USD") %>%
  layout(
    title = '2011 US Agriculture Exports by State<br>(Hover for breakdown)',
    geo = g
  )

The data I am using is a numeric value, determining the colour to fill a country, and the two-digit ISO code of all European countries. So since I want to create a map of Europe, I set scope = 'europe' but I don’t understand what projection = list(type = 'albers usa') does and what I need to specify instead. I just dropped it thus.

g <- list(
  scope = 'europe')

I tried running the following code but the resulting map is “empty”, i.e. there is no colouring of any country.

plot_geo(data) %>%
  add_trace(
    z = ~value, locations = ~LAND_ISO,
    color = ~value, colors = 'Purples'
  ) %>%
  colorbar(title = "") %>%
  layout(geo = g
  )

empty map using plot_geo

My first guess was that maybe proving the ISO codes is not the proper way to specify the locationsargument. If so, what is required instead? Or do I need to set the locationmodeargument used in the code above? What would that be for Europe?

Can anyone help me with this? Thank you!

like image 546
alex Avatar asked Oct 30 '22 17:10

alex


1 Answers

Your code seems to work for me. I created a dataframe as shown below and passed it to the code you provided above and the output I recieved was:

Europe map

I expect that the issue is with the formatting of the data frame you pass to your code, check that the ISO-3 codes are strings and that the values are numeric etc.

library(plotly)

#Create dataframe with toy data:
  LAND_ISO <- c("AUT","BEL","BGR","HRV","CYP","CZE","DNK","EST","FIN","FRA","DEU","GRC","HUN","IRL","ITA","LVA","LTU","LUX","MLT","NLD","POL","PRT","ROU","SVK","SVN","ESP","SWE","GBR")
  value <- runif(length(LAND_ISO), 1, 10)

  data <- data.frame(LAND_ISO, value)

# Run your code:
g <- list(
  scope = 'europe')

plot_geo(data) %>%
  add_trace(
    z = ~value, locations = ~LAND_ISO,
    color = ~value, colors = 'Purples'
  ) %>%
  colorbar(title = "") %>%
  layout(geo = g
  )

FYI I believe

projection = list(type = 'albers usa')

just selects the projection method used to create a 2D map from a 3D globe - See https://en.wikipedia.org/wiki/Map_projection for more details.

like image 196
Graeme Avatar answered Nov 14 '22 08:11

Graeme