r - plotly 等值线图 : display country names

标签 r plotly choropleth

考虑以下 R 代码,以 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)

是否有一个 plotly 内置的选项来在 map 上显示国家/地区名称?如果不是,那么编码这个的聪明方法是什么?

查看示例:https://plot.ly/r/choropleth-maps/

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

最佳答案

您可以通过添加新的 scattergeo 来显示国家/地区标签。跟踪 mode 设置为 "text"只显示标签。

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

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

关于r - plotly 等值线图 : display country names,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32548781/

相关文章:

r - 如何使用 pROC 的 ggroc 将 AUC 添加到多 ROC 图中

r - 如何从 R 中的 kmeans 获取簇距离空间?

python - 对 plotly 在线/离线/袖扣和不同版本感到困惑

javascript - 使用 CSS 绘制 Axis 位置

R plotly choropleth禁用鼠标滚轮缩放

r - 在 R 中对非常大的数据集进行 PCA

r - 如何使用 chisq.test() 正确进行卡方检验?

python - 在绘图上显示数据框

Python Folium Choropleth map KeyError : None

r - choroplethr 图例未打印某些图,scale_fill_brewer() 的问题