在 R 中的地理位置上生成热图
producing heat map over Geo locations in R
我想使用 r 根据美国地图上的一些 geo-location 数据生成热图。
下面的图是在我使用 geom_polygon()
和 geom_point()
.
的美国地图上创建的
如何将其转换为热图以获得如下或类似的内容?
用于生成上图的r脚本:
library(ggplot2);library(maps);
states <- map_data("state")
ggplot() +
geom_polygon(data=states, aes(x=long, y=lat, group=group), color="grey", fill="white")+
geom_point(data=data, aes(x=long, y=lat, color = X, size = X), alpha = 0.5)+
scale_colour_gradient(low = "red", high = "green")+
scale_x_discrete()+ scale_y_discrete()+ theme_void()+
facet_wrap( ~type, nrow = 2)
我用的数据:
data <- structure(list(X = c(3L, 2L, 6L, 1L, 4L, 4L, 1L, 2L, 5L, 3L,
0L, 4L, 4L, 3L, 3L, 2L, 4L, 3L, 4L, 4L, 5L, 3L, 4L, 4L, 2L, 3L,
5L, 2L, 2L, 0L, 1L, 0L, 3L, 4L, 2L, 3L, 0L, 0L, 1L, 3L, 3L, 1L,
2L, 0L, 0L, 3L, 4L, 3L, 4L, 4L, 3L, 2L, 0L), Y = c(3L, 9L, 7L,
6L, 3L, 3L, 6L, 5L, 3L, 10L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 4L,
5L, 4L, 4L, 4L, 5L, 6L, 5L, 4L, 4L, 4L, 2L, 2L, 0L, 2L, 4L, 2L,
4L, 4L, 3L, 3L, 4L, 7L, 7L, 2L, 3L, 2L, 4L, 3L, 4L, 3L, 3L, 7L,
3L, 4L, 3L), Z = c(35L, 31L, 31L, 32L, 35L, 36L, 33L, 37L, 32L,
30L, 39L, 35L, 33L, 35L, 35L, 35L, 30L, 35L, 33L, 31L, 33L, 35L,
33L, 35L, 35L, 35L, 34L, 36L, 38L, 42L, 43L, 36L, 37L, 36L, 39L,
35L, 38L, 40L, 39L, 33L, 33L, 41L, 38L, 38L, 41L, 39L, 35L, 35L,
35L, 34L, 39L, 39L, 39L), type = structure(c(2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L), .Label = c("BF", "DE", "TS", "ZN"), class = "factor"), lat = c(40.77486,
33.72621, 30.38654, 39.21092, 42.56396, 39.17866, 41.42014, 38.8756,
39.98261, 32.73808, 39.36327, 42.31465, 42.95051, 38.18899, 26.05433,
45.21794, 36.13051, 30.00133, 40.7747, 35.48284, 41.32453, 28.45942,
39.94898, 33.27457, 40.43147, 45.63667, 41.81694, 32.73808, 39.36327,
42.31465, 42.95051, 35.9996, 34.83323, 41.76574, 29.53539, 39.80981,
30.34521, 39.09211, 35.95858, 35.92756, 33.78383, 38.18899, 36.13051,
30.00133, 40.7747, 35.48284, 41.32453, 28.45942, 39.94898, 33.27457,
40.43147, 45.63667, 41.81694), long = c(-75.94306, -85.44905,
-97.78677, -77.6507, -72.23171, -84.7458, -82.23518, -104.89844,
-83.27101, -98.0848, -106.07907, -83.24842, -85.86264, -85.81685,
-80.99071, -94.50328, -87.32164, -90.19677, -74.68918, -97.61928,
-96.59226, -81.96995, -75.87195, -112.45182, -80.05054, -123.21019,
-71.45619, -98.0848, -106.07907, -83.24842, -85.86264, -80.0537,
-82.39784, -72.7151, -96.11084, -86.41153, -81.82319, -94.8559,
-83.99512, -115.53027, -119.30347, -85.81685, -87.32164, -90.19677,
-74.68918, -97.61928, -96.59226, -81.96995, -75.87195, -112.45182,
-80.05054, -123.21019, -71.45619)), class = "data.frame", row.names = c(NA,
-53L))
感谢您的帮助。
您可以使用 stat_density2d
,指定 geom = "polygon"
。从评论来看,您似乎想要一个图,每个 X、Y 和 Z 值都有 4 个方面。因为 stat_density2d
只会计算 X
、Y
的每个实例或 Z
,而不是考虑其大小,我们需要根据每个点的值对每一行进行 n 次复制。
dfX <- data.frame(long = rep(data$long, data$X),
lat = rep(data$lat, data$X),
type = rep(data$type, data$X))
dfY <- data.frame(long = rep(data$long, data$Y),
lat = rep(data$lat, data$Y),
type = rep(data$type, data$Y))
dfZ <- data.frame(long = rep(data$long, data$Z),
lat = rep(data$lat, data$Z),
type = rep(data$type, data$Z))
现在我们可以定义绘图函数了:
plot_mapdata <- function(df)
{
states <- ggplot2::map_data("state")
ggplot2::ggplot(data = df, ggplot2::aes(x = long, y = lat)) +
ggplot2::lims(x = c(-140, 50), y = c(20, 60)) +
ggplot2::coord_cartesian(xlim = c(-130, -60), ylim = c(25, 50)) +
ggplot2::geom_polygon(data = states, ggplot2::aes(x = long, y = lat, group = group),
color = "black", fill = "white") +
ggplot2::stat_density2d(ggplot2::aes(fill = ..level.., alpha = ..level..),
geom = "polygon") +
ggplot2::scale_fill_gradientn(colours = rev(RColorBrewer::brewer.pal(7, "Spectral"))) +
ggplot2::geom_polygon(data = states, ggplot2::aes(x = long, y = lat, group = group),
color = "black", fill = "none") +
ggplot2::facet_wrap( ~type, nrow = 2) +
ggplot2::theme(legend.position = "none")
}
所以我们可以这样做:
plot_mapdata(dfX)
plot_mapdata(dfY)
plot_mapdata(dfZ)
我想使用 r 根据美国地图上的一些 geo-location 数据生成热图。
下面的图是在我使用 geom_polygon()
和 geom_point()
.
如何将其转换为热图以获得如下或类似的内容?
用于生成上图的r脚本:
library(ggplot2);library(maps);
states <- map_data("state")
ggplot() +
geom_polygon(data=states, aes(x=long, y=lat, group=group), color="grey", fill="white")+
geom_point(data=data, aes(x=long, y=lat, color = X, size = X), alpha = 0.5)+
scale_colour_gradient(low = "red", high = "green")+
scale_x_discrete()+ scale_y_discrete()+ theme_void()+
facet_wrap( ~type, nrow = 2)
我用的数据:
data <- structure(list(X = c(3L, 2L, 6L, 1L, 4L, 4L, 1L, 2L, 5L, 3L,
0L, 4L, 4L, 3L, 3L, 2L, 4L, 3L, 4L, 4L, 5L, 3L, 4L, 4L, 2L, 3L,
5L, 2L, 2L, 0L, 1L, 0L, 3L, 4L, 2L, 3L, 0L, 0L, 1L, 3L, 3L, 1L,
2L, 0L, 0L, 3L, 4L, 3L, 4L, 4L, 3L, 2L, 0L), Y = c(3L, 9L, 7L,
6L, 3L, 3L, 6L, 5L, 3L, 10L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 4L,
5L, 4L, 4L, 4L, 5L, 6L, 5L, 4L, 4L, 4L, 2L, 2L, 0L, 2L, 4L, 2L,
4L, 4L, 3L, 3L, 4L, 7L, 7L, 2L, 3L, 2L, 4L, 3L, 4L, 3L, 3L, 7L,
3L, 4L, 3L), Z = c(35L, 31L, 31L, 32L, 35L, 36L, 33L, 37L, 32L,
30L, 39L, 35L, 33L, 35L, 35L, 35L, 30L, 35L, 33L, 31L, 33L, 35L,
33L, 35L, 35L, 35L, 34L, 36L, 38L, 42L, 43L, 36L, 37L, 36L, 39L,
35L, 38L, 40L, 39L, 33L, 33L, 41L, 38L, 38L, 41L, 39L, 35L, 35L,
35L, 34L, 39L, 39L, 39L), type = structure(c(2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L), .Label = c("BF", "DE", "TS", "ZN"), class = "factor"), lat = c(40.77486,
33.72621, 30.38654, 39.21092, 42.56396, 39.17866, 41.42014, 38.8756,
39.98261, 32.73808, 39.36327, 42.31465, 42.95051, 38.18899, 26.05433,
45.21794, 36.13051, 30.00133, 40.7747, 35.48284, 41.32453, 28.45942,
39.94898, 33.27457, 40.43147, 45.63667, 41.81694, 32.73808, 39.36327,
42.31465, 42.95051, 35.9996, 34.83323, 41.76574, 29.53539, 39.80981,
30.34521, 39.09211, 35.95858, 35.92756, 33.78383, 38.18899, 36.13051,
30.00133, 40.7747, 35.48284, 41.32453, 28.45942, 39.94898, 33.27457,
40.43147, 45.63667, 41.81694), long = c(-75.94306, -85.44905,
-97.78677, -77.6507, -72.23171, -84.7458, -82.23518, -104.89844,
-83.27101, -98.0848, -106.07907, -83.24842, -85.86264, -85.81685,
-80.99071, -94.50328, -87.32164, -90.19677, -74.68918, -97.61928,
-96.59226, -81.96995, -75.87195, -112.45182, -80.05054, -123.21019,
-71.45619, -98.0848, -106.07907, -83.24842, -85.86264, -80.0537,
-82.39784, -72.7151, -96.11084, -86.41153, -81.82319, -94.8559,
-83.99512, -115.53027, -119.30347, -85.81685, -87.32164, -90.19677,
-74.68918, -97.61928, -96.59226, -81.96995, -75.87195, -112.45182,
-80.05054, -123.21019, -71.45619)), class = "data.frame", row.names = c(NA,
-53L))
感谢您的帮助。
您可以使用 stat_density2d
,指定 geom = "polygon"
。从评论来看,您似乎想要一个图,每个 X、Y 和 Z 值都有 4 个方面。因为 stat_density2d
只会计算 X
、Y
的每个实例或 Z
,而不是考虑其大小,我们需要根据每个点的值对每一行进行 n 次复制。
dfX <- data.frame(long = rep(data$long, data$X),
lat = rep(data$lat, data$X),
type = rep(data$type, data$X))
dfY <- data.frame(long = rep(data$long, data$Y),
lat = rep(data$lat, data$Y),
type = rep(data$type, data$Y))
dfZ <- data.frame(long = rep(data$long, data$Z),
lat = rep(data$lat, data$Z),
type = rep(data$type, data$Z))
现在我们可以定义绘图函数了:
plot_mapdata <- function(df)
{
states <- ggplot2::map_data("state")
ggplot2::ggplot(data = df, ggplot2::aes(x = long, y = lat)) +
ggplot2::lims(x = c(-140, 50), y = c(20, 60)) +
ggplot2::coord_cartesian(xlim = c(-130, -60), ylim = c(25, 50)) +
ggplot2::geom_polygon(data = states, ggplot2::aes(x = long, y = lat, group = group),
color = "black", fill = "white") +
ggplot2::stat_density2d(ggplot2::aes(fill = ..level.., alpha = ..level..),
geom = "polygon") +
ggplot2::scale_fill_gradientn(colours = rev(RColorBrewer::brewer.pal(7, "Spectral"))) +
ggplot2::geom_polygon(data = states, ggplot2::aes(x = long, y = lat, group = group),
color = "black", fill = "none") +
ggplot2::facet_wrap( ~type, nrow = 2) +
ggplot2::theme(legend.position = "none")
}
所以我们可以这样做:
plot_mapdata(dfX)
plot_mapdata(dfY)
plot_mapdata(dfZ)