在“ggplot”和“ggmap”中使用“facet_wrap”来显示多个图形的问题

Posted

技术标签:

【中文标题】在“ggplot”和“ggmap”中使用“facet_wrap”来显示多个图形的问题【英文标题】:problem using `facet_wrap` in `ggplot` and `ggmap` to show multiple graph 【发布时间】:2021-07-14 03:50:00 【问题描述】:

我有一个包含以下信息的数据集:

  Postal.Code   top LATITUDE LONGITUDE
   <chr>       <int>    <dbl>     <dbl>
 1 V6Z 3E6         1     49.27369     -123.130006

structure(list(Postal.Code = c("V6Z 3E6", "V6H 0A7", "V6G 1X5", 
"V6J 2A4", "V6B 2L3", "V5L 4N3", "V6Z 2Y2", "V5V 3N2", "V6P 6Z1", 
"V6E 4R7", "V6B 1S3", "V6Z 3G2", "V6J 0B4", "V5K 0C5", "V5N 5P9", 
"V5L 3S4", "V6Z 2E9", "V6B 1R1", "V6E 1G2", "V6A 1B5", "V6E 4L8", 
"V6E 4L8", "V6E 4L8", "V6B 6A7", "V6B 4K8", "V6E 0B1", "V6Z 3E1", 
"V6E 0B1", "V6Z 1C2", "V6E 0B1", "V6Z 3A4", "V6E 1H1", "V6A 4K7", 
"V6Z 2Y4", "V5T 3G2", "V6E 4R7", "V6E 0B1", "V6Z 3B8", "V6B 1B7", 
"V6B 6M2"), top = c(1L, 4L, 5L, 5L, 3L, 5L, 1L, 5L, 4L, 3L, 4L, 
1L, 3L, 5L, 5L, 3L, 3L, 1L, 5L, 4L, 2L, 3L, 4L, 1L, 1L, 4L, 1L, 
5L, 2L, 4L, 1L, 5L, 1L, 1L, 4L, 2L, 4L, 1L, 5L, 5L), LATITUDE = c(49.27369, 
49.2632107, 49.282456, 49.261313, 49.281036, 49.283819, 49.273184, 
49.254803, 49.2030784, 49.2785751, 49.276925, 49.273926, 49.263131, 
48.004134, 49.284064, 49.272736, 49.279232, 49.280055, 49.2868418, 
49.283923, 49.2786587, 49.2786587, 49.2786587, 49.274144, 49.2750068, 
49.281987, 49.278667, 49.281987, 49.276812, 49.281987, 49.273718, 
49.283478, 49.276, 49.273557, 49.25984, 49.2785751, 49.281987, 
49.273347, 49.2754447, 49.2789398), LONGITUDE = c(-123.130006, 
-123.128055, -123.138561, -123.068251, -123.10882, -123.059851, 
-123.119397, -123.10099, -123.1348227, -123.1302609, -123.118491, 
-123.127924, -123.150686, -113.331951, -123.070319, -123.073661, 
-123.1269454, -123.10569, -123.1301548, -123.101285, -123.1303935, 
-123.1303935, -123.1303935, -123.124925, -123.1257866, -123.125272, 
-123.130037, -123.125272, -123.131891, -123.125272, -123.117243, 
-123.127271, -123.101171, -123.119595, -123.100871, -123.1302609, 
-123.125272, -123.127828, -123.1249486, -123.1158462)), row.names = c(NA, 
-40L), class = c("tbl_df", "tbl", "data.frame"))

top 列基本上是一个主题编号列,我想在地图上为每个主题编号绘制点。我决定使用facet_wrap,而不是对每个主题的数据进行子集化,如下所示:

van <- get_map(location = "vancouver", zoom = 12)
HoustonMap <- ggmap(van, base_layer = ggplot(aes(x = LONGITUDE, y = LATITUDE),
                                             data = data))
HoustonMap +
  stat_density2d(aes(x = LONGITUDE, y = LATITUDE, fill = stat(level), alpha = ..level..),
                 bins = 10, geom = "polygon",
                 data = data) +
  scale_fill_gradient(low = "black", high = "red") + facet_wrap(~ top)

但是,这并不能正确显示结果。该图如下所示:

它只是为主题 2 显示一个红点,仅此而已。我该如何解决这个问题?

【问题讨论】:

facet_wrap(.~top)吗? @DavidZ 当我添加. 时出现此错误:Error: At least one layer must contain all faceting variables: .top. * Plot is missing .top` * 缺少第 1 层 .top * 缺少第 2 层 .top * 层缺少 3 .top * 缺少第 4 层 .top ` 你是否包含了~? @Elle 是的,我包括了〜但没用 @NicolásVelásquez,我以这种形式定义了vanvan &lt;- get_map(location = "vancouver", zoom = 12)。我还在statdensity2d 部分尝试了geom = "polygon"。我应该在哪里进行更改? 【参考方案1】:

您的代码很好,尽管您有一个点超出了此地图的范围。

问题在于 stat_density2d 中映射到alpha = ..level.. 的每个方面的点密度较低。要提高可视化效果,只需增加 bin 的数量即可。

library(tidyverse)
library(g
df_points <- 
  tibble(
    Postal.Code = c("V6Z 3E6", "V6H 0A7", "V6G 1X5", "V6J 2A4", "V6B 2L3", "V5L 4N3", "V6Z 2Y2", "V5V 3N2", "V6P 6Z1", "V6E 4R7", "V6B 1S3", "V6Z 3G2", "V6J 0B4", "V5K 0C5", "V5N 5P9", "V5L 3S4", "V6Z 2E9", "V6B 1R1", "V6E 1G2", "V6A 1B5", "V6E 4L8", "V6E 4L8", "V6E 4L8", "V6B 6A7", "V6B 4K8", "V6E 0B1", "V6Z 3E1", "V6E 0B1", "V6Z 1C2", "V6E 0B1", "V6Z 3A4", "V6E 1H1", "V6A 4K7", "V6Z 2Y4", "V5T 3G2", "V6E 4R7", "V6E 0B1", "V6Z 3B8", "V6B 1B7", "V6B 6M2"), 
    top = c(1L, 4L, 5L, 5L, 3L, 5L, 1L, 5L, 4L, 3L, 4L, 1L, 3L, 5L, 5L, 3L, 3L, 1L, 5L, 4L, 2L, 3L, 4L, 1L, 1L, 4L, 1L, 5L, 2L, 4L, 1L, 5L, 1L, 1L, 4L, 2L, 4L, 1L, 5L, 5L), 
    LATITUDE = c(49.27369, 49.2632107, 49.282456, 49.261313, 49.281036, 49.283819, 49.273184, 49.254803, 49.2030784, 49.2785751, 49.276925, 49.273926, 49.263131, 48.004134, 49.284064, 49.272736, 49.279232, 49.280055, 49.2868418, 49.283923, 49.2786587, 49.2786587, 49.2786587, 49.274144, 49.2750068, 49.281987, 49.278667, 49.281987, 49.276812, 49.281987, 49.273718, 49.283478, 49.276, 49.273557, 49.25984, 49.2785751, 49.281987, 49.273347, 49.2754447, 49.2789398), 
    LONGITUDE = c(-123.130006, -123.128055, -123.138561, -123.068251, -123.10882, -123.059851, -123.119397, -123.10099, -123.1348227, -123.1302609, -123.118491, -123.127924, -123.150686, -113.331951, -123.070319, -123.073661, -123.1269454, -123.10569, -123.1301548, -123.101285, -123.1303935, -123.1303935, -123.1303935, -123.124925, -123.1257866, -123.125272, -123.130037, -123.125272, -123.131891, -123.125272, -123.117243, -123.127271, -123.101171, -123.119595, -123.100871, -123.1302609, -123.125272, -123.127828, -123.1249486, -123.1158462))

vancouver <- get_map(location = "vancouver", zoom = 12)

ggmap(ggmap = vancouver,
      maprange = FALSE, 
      base_layer = ggplot(data = df_points,
                          aes(x = LONGITUDE, y = LATITUDE))) +
  stat_density2d(aes(x = LONGITUDE, y = LATITUDE, fill = ..level.., alpha = ..level..),
                 bins = 1000, # Modify this number to tweak the density areas.
                 geom = "polygon") +
  scale_fill_gradient(low = "red", high = "black") +
  facet_wrap(~top)

【讨论】:

感谢您的澄清。帮助很大

以上是关于在“ggplot”和“ggmap”中使用“facet_wrap”来显示多个图形的问题的主要内容,如果未能解决你的问题,请参考以下文章

在 ggplot/ggmap 中绘制多个数据框并创建统一图例的问题

在 ggmap 和 ggplot2 中为英国绘制地图

查找比例 x 或 y 连续的限制或避免删除 ggplot2 和 ggmap 中的行

在 R ggmap 中绘制自定义边框

图例标题对齐 (ggmap)

GIS | R 数据可视化:ggmap包