How to create a choropleth map using plot_geo ()?

I am trying to build a choropleth map in R and found very promising code using the function plot_geo()here: https://plot.ly/r/choropleth-maps/ . Unfortunately, I could not understand how to adapt the code to my problem, and I could not find useful explanations on the Internet.

Here is the source 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 use is a numerical value that defines the color to fill in the country and the two-digit ISO code of all European countries. Therefore, since I want to create a map of Europe, I install scope = 'europe', but I do not understand what it does projection = list(type = 'albers usa')and what I need to indicate instead. I just threw it that way.

g <- list(
  scope = 'europe')

, "", .. - .

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

plot_geo

, , , ISO locations. , ? locationmode, ? ?

- ? !

+4
1

, , . , , , , , :

Map of Europe

, , , , ISO-3 ..

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

projection = list (type = 'albers usa')

, 2D- 3D-. . https://en.wikipedia.org/wiki/Map_projection.

0

Source: https://habr.com/ru/post/1659457/


All Articles