将 ggplot2 geoms 添加到简单的特征图
Adding ggplot2 geoms to simple features plot
与 sp
相比,sf
包似乎是一种更加用户友好的处理空间数据的方法。比如我有一组latitude/longitude坐标,用开发版的ggplot2
:
就可以很方便的画出来
library(sf)
devtools::install_github("tidyverse/ggplot2")
library(ggplot2)
# generate some data
set.seed(123)
y = rnorm(10, mean=40, sd=20)
x = rnorm(10, mean=-100, sd=30)
# collect to data.frame
xy = data.frame(x=x,y=y)
# create sf object
xy.sf = sf::st_as_sf(xy, coords=c("x", "y"), crs=4269)
# plot points
ggplot(data=xy.sf) + geom_sf()
ggplot2::geom_sf
函数知道 xy.sf
对象的几何形状是一组点,因此我不需要调用,例如 ggplot2::geom_point()
.
但是,假设我想根据点集添加另一个几何对象。
例如,如果我想生成一个轮廓层来显示点集中的位置,我会使用 ggplot2::geom_density2d
或 ggplot2::stat_density2d
,如 this answer and this answer.[=24 中所建议的=]
但是下面的代码
ggplot(data=xy.sf) +
geom_sf() +
geom_density2d(data=xy.sf, aes(x=x,y=y,colour=..level..))
生成以下图像
请注意,轮廓线的坐标似乎颠倒了!
我尝试修改上面的代码,但无法正常工作。我意识到 sf
包相当新,但地图非常接近正确!有什么想法吗?
编辑: 忘记添加会话信息
> sessionInfo()
R version 3.4.0 (2017-04-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggplot2_2.2.1.9000 sf_0.4-3
loaded via a namespace (and not attached):
[1] Rcpp_0.12.9 magrittr_1.5 maps_3.1.1 units_0.4-4
[5] MASS_7.3-47 munsell_0.4.3 geosphere_1.5-5 colorspace_1.3-2
[9] lattice_0.20-35 rjson_0.2.15 jpeg_0.1-8 rlang_0.1.1
[13] stringr_1.2.0 udunits2_0.13 plyr_1.8.4 tools_3.4.0
[17] rgdal_1.2-5 grid_3.4.0 gtable_0.2.0 png_0.1-7
[21] DBI_0.5-1 ggthemes_3.3.0 lazyeval_0.2.0 assertthat_0.1
[25] digest_0.6.12 tibble_1.3.1 ggmap_2.6.1 reshape2_1.4.2
[29] mapproj_1.2-4 labeling_0.3 sp_1.2-4 stringi_1.1.2
[33] compiler_3.4.0 RgoogleMaps_1.4.1 scales_0.4.1 proto_1.0.0
为了将来参考,我想我 post 我的发现作为答案:
事实证明,在 geom_sf
之后添加一个图层,例如 geom_density2d
,并不会继承应用于 geom_sf
的贴图美学。
请注意,这按预期工作:
ggplot(data=xy.sf, aes(x=x, y=y)) +
geom_sf() +
geom_density2d(aes(colour=..level..))
但这只是因为 x,y
作为对象存在 与 xy.sf
分开。
因此,以下会引发错误:
rm(x,y)
ggplot(data=xy.sf, aes(x=x, y=y)) +
geom_sf() +
geom_density2d(aes(colour=..level..))
Error in FUN(X[[i]], ...) : object 'x' not found
当然可以在documentation!
中找到
"geom_sf
uses a unique aesthetic: geometry
... Unlike other aesthetics, geometry
will never be inherited from the plot."
所以我找到的解决方法是使用 x,y
对象本身,或者 sf
对象的纯 data.frame
版本;例如,geom_density2d(data=xy, aes(x,y,colour=..level..))
.
与 sp
相比,sf
包似乎是一种更加用户友好的处理空间数据的方法。比如我有一组latitude/longitude坐标,用开发版的ggplot2
:
library(sf)
devtools::install_github("tidyverse/ggplot2")
library(ggplot2)
# generate some data
set.seed(123)
y = rnorm(10, mean=40, sd=20)
x = rnorm(10, mean=-100, sd=30)
# collect to data.frame
xy = data.frame(x=x,y=y)
# create sf object
xy.sf = sf::st_as_sf(xy, coords=c("x", "y"), crs=4269)
# plot points
ggplot(data=xy.sf) + geom_sf()
ggplot2::geom_sf
函数知道 xy.sf
对象的几何形状是一组点,因此我不需要调用,例如 ggplot2::geom_point()
.
但是,假设我想根据点集添加另一个几何对象。
例如,如果我想生成一个轮廓层来显示点集中的位置,我会使用 ggplot2::geom_density2d
或 ggplot2::stat_density2d
,如 this answer and this answer.[=24 中所建议的=]
但是下面的代码
ggplot(data=xy.sf) +
geom_sf() +
geom_density2d(data=xy.sf, aes(x=x,y=y,colour=..level..))
生成以下图像
请注意,轮廓线的坐标似乎颠倒了!
我尝试修改上面的代码,但无法正常工作。我意识到 sf
包相当新,但地图非常接近正确!有什么想法吗?
编辑: 忘记添加会话信息
> sessionInfo()
R version 3.4.0 (2017-04-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggplot2_2.2.1.9000 sf_0.4-3
loaded via a namespace (and not attached):
[1] Rcpp_0.12.9 magrittr_1.5 maps_3.1.1 units_0.4-4
[5] MASS_7.3-47 munsell_0.4.3 geosphere_1.5-5 colorspace_1.3-2
[9] lattice_0.20-35 rjson_0.2.15 jpeg_0.1-8 rlang_0.1.1
[13] stringr_1.2.0 udunits2_0.13 plyr_1.8.4 tools_3.4.0
[17] rgdal_1.2-5 grid_3.4.0 gtable_0.2.0 png_0.1-7
[21] DBI_0.5-1 ggthemes_3.3.0 lazyeval_0.2.0 assertthat_0.1
[25] digest_0.6.12 tibble_1.3.1 ggmap_2.6.1 reshape2_1.4.2
[29] mapproj_1.2-4 labeling_0.3 sp_1.2-4 stringi_1.1.2
[33] compiler_3.4.0 RgoogleMaps_1.4.1 scales_0.4.1 proto_1.0.0
为了将来参考,我想我 post 我的发现作为答案:
事实证明,在 geom_sf
之后添加一个图层,例如 geom_density2d
,并不会继承应用于 geom_sf
的贴图美学。
请注意,这按预期工作:
ggplot(data=xy.sf, aes(x=x, y=y)) +
geom_sf() +
geom_density2d(aes(colour=..level..))
但这只是因为 x,y
作为对象存在 与 xy.sf
分开。
因此,以下会引发错误:
rm(x,y)
ggplot(data=xy.sf, aes(x=x, y=y)) +
geom_sf() +
geom_density2d(aes(colour=..level..))
Error in FUN(X[[i]], ...) : object 'x' not found
当然可以在documentation!
中找到"
geom_sf
uses a unique aesthetic:geometry
... Unlike other aesthetics,geometry
will never be inherited from the plot."
所以我找到的解决方法是使用 x,y
对象本身,或者 sf
对象的纯 data.frame
版本;例如,geom_density2d(data=xy, aes(x,y,colour=..level..))
.