plotly choropleth地图:显示国家名称

plotly choropleth地图:显示国家名称,r,plotly,choropleth,R,Plotly,Choropleth,请考虑以下R代码,以在plotly中生成choropleth贴图: #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 =

请考虑以下R代码,以在plotly中生成choropleth贴图:

#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)
\devtools::安装github(“ropensci/plotly”)
图书馆(绘本)

df您可以通过添加设置为
“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")

df如果你真的需要国家名称来帮助人们理解地理,也许条形图是更好的可视化选择。另外,由于plotly不在CRAN中,一些人可能需要安装说明。最后,国家名称出现在弹出窗口中(这在某种程度上是使用交互式可视化的实际意义)。@hrbrmstr整个要点更多的是美化地图,而不是教授地理;)我们对统计地图的“美”有不同的看法,没关系。你关于安装说明的观点很好理解。我添加了一个链接来解释它。