考虑使用以下 R 代码在 plotly 中生成等值线图:
#devtools::install_github("ropensci/plotly")图书馆(情节)df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_world_gdp_with_codes.csv')# 浅灰色边界l <- 列表(颜色 = toRGB(灰色"),宽度 = 0.5)# 指定地图投影/选项g <- 列表(展示框 = FALSE,showcoastlines = FALSE,投影=列表(类型='墨卡托'))plot_ly(df, z = GDP..BILLIONS., text = COUNTRY, locations = CODE, type = 'choropleth',颜色 = GDP..BILLIONS.,颜色 = 'Blues',标记 = 列表(行 = l),colorbar = list(tickprefix = '$', title = 'GDP Billions US$'),文件名="r-docs/world-choropleth") %>%layout(title = '2014 年全球 GDP<br>来源:<a href="https://www.cia.gov/library/publications/the-world-factbook/fields/2195.html">CIA World Factbook</a>',地理 = g)
是否有一个内置选项可以在地图上显示国家名称?如果不是,那么编写此代码的聪明方法是什么?
查看示例:
全屏互动版
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/
解决方案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")
full screen, interactive version
上一篇:像 Excel 中一样平滑散布(ggplot2 + plotly)平滑、Excel、plotly
下一篇:访问在web.config中的一个configSection属性?属性、web、config、configSection