R - plotly - 合并气泡图和区域地图

4
我希望您能在plotly中将两种类型的地图合并成一张地图,即气泡图和区域填充图。目标是通过悬停鼠标在地图上来展示国家层面(区域填充)和城市层面(气泡)的人口规模。以下是用于区域填充地图的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 示例代码如下:

library(plotly)
    df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_us_cities.csv')
    df$hover <- paste(df$name, "Population", df$pop/1e6, " million")

    df$q <- with(df, cut(pop, quantile(pop)))
    levels(df$q) <- paste(c("1st", "2nd", "3rd", "4th", "5th"), "Quantile")
    df$q <- as.ordered(df$q)

    g <- list(
      scope = 'usa',
      projection = list(type = 'albers usa'),
      showland = TRUE,
      landcolor = toRGB("gray85"),
      subunitwidth = 1,
      countrywidth = 1,
      subunitcolor = toRGB("white"),
      countrycolor = toRGB("white")
    )

    plot_ly(df, lon = lon, lat = lat, text = hover,
            marker = list(size = sqrt(pop/10000) + 1),
            color = q, type = 'scattergeo', locationmode = 'USA-states',
            filename="r-docs/bubble-map") %>%
      layout(title = '2014 US city populations<br>(Click legend to toggle)', geo = g)

如何将这两种类型的地图合并为一张地图?
1个回答

8

好问题!这里有一个简单的例子。注意:

  • 使用add_trace在绘图之上添加另一种图表类型的层。
  • 绘图的layout在所有迹线之间进行共享。layout键描述诸如地图的scope、轴、title等内容。查看更多布局键。

简单气泡图地图

lon = c(-73.9865812, -118.2427266, -87.6244212, -95.3676974)
pop = c(8287238, 3826423, 2705627, 2129784)
df_cities = data.frame(cities, lat, lon, pop)

plot_ly(df_cities, lon=lon, lat=lat, 
        text=paste0(df_cities$cities,'<br>Population: ', df_cities$pop),
        marker= list(size = sqrt(pop/10000) + 1), type="scattergeo",
        filename="stackoverflow/simple-scattergeo") %>%
  layout(geo = list(scope="usa"))

采用R和plotly创建的气泡图地图 交互版本

简单的区域填充图表

state_codes = c("NY", "CA", "IL", "TX")
pop = c(19746227.0, 38802500.0, 12880580.0, 26956958.0)
df_states = data.frame(state_codes, pop)

plot_ly(df_states, z=pop, locations=state_codes, text=paste0(df_states$state_codes, '<br>Population: ', df_states$pop), 
        type="choropleth", locationmode="USA-states", colors = 'Purples', filename="stackoverflow/simple-choropleth") %>%
  layout(geo = list(scope="usa"))

使用R和plotly创建的区域填充图表 交互版本

结合区域填充图和气泡图的图表

plot_ly(df_cities, lon=lon, lat=lat, 
        text=paste0(df_cities$cities,'<br>Population: ', df_cities$pop), 
        marker= list(size = sqrt(pop/10000) + 1), type="scattergeo",
        filename="stackoverflow/choropleth+scattergeo") %>%
  add_trace(z=df_states$pop,
            locations=df_states$state_codes, 
            text=paste0(df_states$state_codes, '<br>Population: ', df_states$pop),
            type="choropleth", 
            colors = 'Purples', 
            locationmode="USA-states") %>%
  layout(geo = list(scope="usa"))

Choropleth map with a bubble chart map 带有悬停文字的交互版

请注意第二个追踪中的zlocations列明确来自df_states数据框。如果它们来自第一个追踪所在的同一数据框(在plot_ly中声明的df_cities),那么我们可以只写z=state_codes而不是z=df_states$state_codes(如第二个示例中所示)。


@kanimbla,这个地图是交互式的吗?我能否制作一个交互式地图,显示相同的变量,但使用其他国家的数据,例如巴西、安哥拉、中国等(使用shapefile文件)? - MAOC
@Vasco,上面的图确实是完全交互式的。您可以通过调整图形选项scopelonaxislataxis来为其他国家制作类似的图表。请参见此示例。问题在于,您想要州级别的细节,而据我所知,Plotly仅提供美国的这种选项(选项locationmode)。此外,我认为Plotly不支持使用shapefiles。对于此问题,您可以尝试使用leaflet - kanimbla

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接