4
votes

Consider the following R code to produce a choropleth map in plotly:

#devtools::install_github("ropensci/plotly")
library(plotly)

df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_world_gdp_with_codes.csv')

# light grey boundaries
l <- list(color = toRGB("grey"), width = 0.5)

# specify map projection/options
g <- list(
  showframe = FALSE,
  showcoastlines = FALSE,
  projection = list(type = 'Mercator')
)

plot_ly(df, z = GDP..BILLIONS., text = COUNTRY, locations = CODE, type = 'choropleth',
        color = GDP..BILLIONS., colors = 'Blues', marker = list(line = l),
        colorbar = list(tickprefix = '$', title = 'GDP Billions US$'),
        filename="r-docs/world-choropleth") %>%
  layout(title = '2014 Global GDP<br>Source:<a href="https://www.cia.gov/library/publications/the-world-factbook/fields/2195.html">CIA World Factbook</a>',
         geo = g)

Is there a plotly build-in option to display country names on the map? If not, what would be a smart way of coding this?

To view the example: https://plot.ly/r/choropleth-maps/

Installation instructions for plotly: https://plot.ly/r/getting-started/

1
If you really need country names to help folks understand geography, perhaps a bar chart is a better choice for this visualization. Also [fixed] since plotly isn't in CRAN some folks may need the installation instructions. Finally, the country names are in the popups (which is somewhat the actual point of using an interactive visualization). - hrbrmstr
@hrbrmstr The whole point is more about beautifying the map rather than teaching geography ;) - kanimbla
We have differing opinions about what makes a statistical map "beautiful" then. - hrbrmstr
That's fine. Your point on installation instructions is well taken. I added a link explaining it. - kanimbla

1 Answers

7
votes

You can show country labels by adding a new scattergeo trace with mode set to "text" to just show the labels.

Here is an example. I'm using dplyr to filter out the 10 greatest rows.

df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_world_gdp_with_codes.csv')

# light grey boundaries
l <- list(color = toRGB("grey"), width = 0.5)

# specify map projection/options
g <- list(
  showframe = FALSE,
  showcoastlines = FALSE,
  projection = list(type = 'Mercator')
)

p <- (plot_ly(df, z = GDP..BILLIONS., text = COUNTRY, locations = CODE, type = 'choropleth',
        color = GDP..BILLIONS., colors = 'Blues', marker = list(line = l),
        colorbar = list(tickprefix = '$', title = 'GDP Billions US$'),
        inherit = FALSE, # don't pass arguments into the next trace
        filename="r-docs/choropleth-with-country-labels") %>%
  layout(title = '2014 Global GDP',
         geo = g) %>% 
  dplyr::arrange(dplyr::desc(GDP..BILLIONS.)))[seq(1, 10), ] %>%
  add_trace(type="scattergeo", # view all scattergeo properties here: https://plot.ly/r/reference/#scattergeo
            locations = CODE, text = COUNTRY, mode="text")

Example of a choropleth chart with country labels made in R with plotly

full screen, interactive version