Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 无法在点和点密度上映射多边形_R_Ggplot2_Sf - Fatal编程技术网

R 无法在点和点密度上映射多边形

R 无法在点和点密度上映射多边形,r,ggplot2,sf,R,Ggplot2,Sf,这个玩具的例子: library(dplyr) library(sf) library(ggplot2) # create random points p <- runif(50, 0, 11) %>% cbind(runif(50, 0, 11)) %>% st_multipoint %>% st_sfc %>% st_cast("POINT") %>% st_sf # append coordinates for ggplot p &

这个玩具的例子:

library(dplyr)
library(sf)
library(ggplot2)

# create random points
p <- runif(50, 0, 11) %>% cbind(runif(50, 0, 11)) %>% st_multipoint %>% st_sfc %>% st_cast("POINT") %>% st_sf
# append coordinates for ggplot
p <- cbind(p, st_coordinates(p))

# plot points, point density
ggplot(p, aes(x=X, y=Y)) + geom_point() + 
  stat_density2d(geom="tile", aes(fill=..density..), contour=F, alpha=.5, data=p)

我做错了什么?

问题是
geom\u sf
层继承了全局aes
x
y
。但是在
s
中没有变量
X
Y
。要防止这种情况,只需在调用
geom_sf
时设置
inherit.aes=FALSE

库(dplyr)
图书馆(GG2)
#创建随机点
p%cbind(runif(50,0,11))%%>%st\U多点%>%st\U sfc%%>%st\U cast(“点”)%%>%st\U sf
#为ggplot添加坐标
p%
st_多边形%>%
st_证监会%>%
圣西弗
#打印点、点密度和多边形
ggplot(p,aes(x=x,y=y))+geom_点()
统计密度2D(geom=“tile”,aes(填充=密度),轮廓=F,α=0.5,数据=p)+
geom_sf(数据=s,填充=NA,继承.aes=FALSE)

# create polygon
s <- rbind(c(1, 1), c(10, 1), c(10, 10), c(1, 10), c(1, 1)) %>% list %>% st_polygon %>% st_sfc %>% st_sf

# plot points, point density and polygon
ggplot(p, aes(x=X, y=Y)) + geom_point() + 
  stat_density2d(geom="tile", aes(fill=..density..), contour=F, alpha=.5, data=p) + 
  geom_sf(data=s, fill=NA)
Error in FUN(X[[i]], ...) : object 'X' not found