2015-09-13 49 views
2

考虑下述R代码在plotly生产地区分布图: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内置的选项,以显示在地图上的国名?如果不是,那么编码这种方法的聪明方式是什么?

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

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

+0

如果你真的需要国名,以帮助人们了解地理,也许条形图是该可视化是更好的选择。另外[固定]因为情节不在CRAN中,有些人可能需要安装说明。最后,国家名称在弹出窗口(这是使用交互式可视化的实际点)。 – hrbrmstr

+0

@hrbrmstr整个观点更多的是美化地图而不是教地理;) – kanimbla

+0

我们对于什么使得统计地图“美丽”的观点有不同的看法。 – hrbrmstr

回答

2

您可以通过添加一个新的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

+0

太棒了,谢谢你! – kanimbla

相关问题