【问题标题】:R - plotly - combine bubble and chorpleth mapR - plotly - 结合气泡和等值线图
【发布时间】:2015-11-09 03:08:42
【问题描述】:

我想在一张地图中结合两种类型的地图,即气泡图和等值线图。目标是通过将鼠标悬停在地图上来显示国家级(等值线)和城市级(气泡)的人口规模。

等值线图的示例代码如下:

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)

气泡图的示例代码如下:

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)

怎么可能将两种类型的地图合二为一呢?

【问题讨论】:

    标签: r maps plotly


    【解决方案1】:

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

    • 使用add_trace 在绘图顶部添加另一个图表类型图层
    • 绘图的layout 在所有轨迹之间共享。 layout 键描述地图的 scope、轴、title 等内容。See more layout keys.

    简单的气泡图地图

    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"))
    

    Interactive version

    简单的等值线图

    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"))
    

    Interactive version

    组合等值线和气泡图

    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"))
    

    Interactive version with hover text

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

    【讨论】:

    • @kanimbla,这张地图是交互式的吗?我可以制作一张显示相同变量但与巴西、安哥拉、中国等其他国家/地区的互动地图吗? (使用 shapefile?)
    • @Vasco,上面的情节确实是完全互动的。您可以通过使用图形选项scopelonaxislataxis 为不同的国家制作类似的情节。请参阅此example。问题是您希望拥有据我所知仅为美国提供的州级粒度(选项locationmode)。此外,我认为 plotly 不支持使用 shapefile。为此,您可以尝试leaflet
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 2021-11-30
    • 2018-07-25
    • 2017-05-20
    • 2018-03-09
    • 2021-08-11
    • 2011-11-11
    相关资源
    最近更新 更多