3D world map with plotly

By Data Tricks, 27 September 2017

1 Load packages and read in the data

There are many sources of world map data. Github https://github.com/plotly/datasets has some useful and fun datasets to practice on. For the purposes of this tutorial we’ll use the 2014 world GDP dataset.

library(plotly)
df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_world_gdp_with_codes.csv')
df[,2] <- as.numeric(df[,2])
df[,2] <- log(df[,2])

Note that the GDP values in column 2 of the dataframe have been transformed to a log scale. This is because there are some large outliers in the GDP data which makes it difficult to distinguish between many countries as they will be shaded in very similar colours.

2 Define the appearance of the map

Purely for code aesthetics, we’ll create a couple of variables containing information about the appearance of the map.

#Set country boundaries as light grey
l <- list(color = toRGB("#d1d1d1"), width = 0.5)
#Specify map projection and options
g <- list(
     showframe = FALSE,
     showcoastlines = FALSE,
     projection = list(type = 'orthographic'),
     resolution = '100',
     showcountries = TRUE,
     countrycolor = '#d1d1d1',
     showocean = TRUE,
     oceancolor = '#c9d2e0',
     showlakes = TRUE,
     lakecolor = '#99c0db',
     showrivers = TRUE,
     rivercolor = '#99c0db')

3 Plot the map

Finally, we can plot the data on to a 3D globe using the plot_geo command in plotly.

p <- plot_geo(df) %>%
     add_trace(z = ~GDP..BILLIONS., color = ~GDP..BILLIONS., colors = 'Reds',
     text = ~COUNTRY, locations = ~CODE, marker = list(line = l)) %>%
     colorbar(title = 'GDP (log)') %>%
     layout(title = '', geo = g)
print(p)

Extra layers can be added, for example adding bubbles to represent the GDP values using add_trace:

p <- plot_geo(df) %>%
     add_trace(opacity = 0.5, locations = ~CODE, colors = 'Reds', marker = list(size=~(10*GDP..BILLIONS.)), type="scattergeo", mode="markers") %>%
     add_trace(z = ~GDP..BILLIONS., color = ~GDP..BILLIONS., colors = 'Reds', text = ~COUNTRY, locations = ~CODE, marker = list(line = l)) %>%
     colorbar(title = 'GDP (log)') %>%
     layout(title = '', geo = g)
print(p)

Note that the GDP values have been multiplied by 10 so that the bubbles are not too small.

Tags: , , , ,

4 thoughts on “3D world map with plotly”

  1. pranamya says:

    I wanted to get a country name or code when I click on the country from the 3d globe as created above in r.

    What can I do to get a country code/name so that I want to filter my dataset on the selected country and draw another visualization for that country.

    1. Data Tricks says:

      Thanks for your question pranamya. It’s a good question – you can add a dropdown list to a plotly chart, but this only seems to change the styling or type of the existing chart, rather than manipulation of the data to use in a different visualisation. There are several articles about this such as this one. To change the underlying data and affect a different visualisation, however, one solution that springs to mind is to create a small shiny app.

  2. Pranamya Korde says:

    Hi,
    Thanks for the reply. Actually I have created a shiny app in which I have used similar map globe to visualize each country and its happiness score. I have used similar code as above.
    Now I want to create a radar plot in a way that when I click on any country from the globe above, the radar graph gets changed accordingly.
    Do you have any idea how can I achieve this feature?

    1. Data Tricks says:

      Hi Pranamya,

      I’ve had a look at various online resources and I’m not sure if this can be done with a 3D plotly map. It might be worth exploring whether this is possible with a leaflet map instead. I came across this article on Stack Overflow -> https://stackoverflow.com/questions/48432061/turn-states-on-a-map-into-clickable-objects-in-shiny which doesn’t do exactly what you want, but it might get you started.

      This is something I’d really like to know how to do too, so I’ll keep an eye out for other resources that might help you, but in the meantime sorry I couldn’t be of more help.

      Tom

Leave a Reply

Your email address will not be published.

Please note that your first comment on this site will be moderated, after which you will be able to comment freely.