考虑以下 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")
full screen, interactive version
关于r - plotly 等值线图 : display country names,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32548781/