绘出choropleth地图:显示国家名称 [英] plotly choropleth map: display country names

查看:134
本文介绍了绘出choropleth地图:显示国家名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下R代码以绘制出曲线图:

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?

要查看示例,请执行以下操作: https://plot.ly/r/choropleth-maps/

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

plotly的安装说明: https://plot.ly/r/getting-started/

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

推荐答案

您可以通过添加新的 mode 设置为仅显示标签.

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

这里是一个例子.我正在使用dplyr过滤掉10个最大的行.

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")

全屏交互式版本

这篇关于绘出choropleth地图:显示国家名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆