我有一个数据集,其中包括以下信息:
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基本上是一个主题编号列,我想在Map上为每个主题编号标绘点,而不是为每个主题设置子集数据,我决定使用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,仅此而已。我该如何解决这个问题?
1条答案
按热度按时间f2uvfpb91#
您的代码没有问题,尽管有一个点超出了这个Map的边界。
问题在于stat_density2d中Map到
alpha = ..level..
的每个面的点密度较低。要改善可视化,只需增加bin的数量。