R 使用stat“U density”2d(geom=“polygon”时不进行渲染

R 使用stat“U density”2d(geom=“polygon”时不进行渲染,r,ggplot2,sf,R,Ggplot2,Sf,由于某种原因,在指定geom=“polygon”时,stat\u density\u 2d()似乎不适合我,我完全被难住了。这是我的密码 library(sf) library(tidyverse) library(RANN2) library(hexbin) library(mapproj) options(stringsAsFactors = FALSE) raleigh_police <- rgdal::readOGR("https://opendata.arcgis.com/d

由于某种原因,在指定
geom=“polygon”
时,stat\u density\u 2d()似乎不适合我,我完全被难住了。这是我的密码

library(sf)
library(tidyverse)
library(RANN2)
library(hexbin)
library(mapproj)

options(stringsAsFactors = FALSE)

raleigh_police <- rgdal::readOGR("https://opendata.arcgis.com/datasets/24c0b37fa9bb4e16ba8bcaa7e806c615_0.geojson", "OGRGeoJSON")

raleigh_police_sf <- raleigh_police %>%
  st_as_sf()

raleigh_police_sf %>%
    filter(crime_description == "Burglary/Residential") %>%
    st_coordinates() %>%
    as_tibble() %>%
    ggplot() +
    stat_density_2d(aes(X, Y, fill = stat(level)), geom = "polygon")
我只是得到一个空白的图,上面什么都没有。完全被难住了。我做错了什么

更新(2018-11-18)
原来这里的主要问题是
选项(stringsAsFactors=FALSE)
。如果您对此进行注释并运行原始代码,那么实际上一切都正常。我发现了这个,这就是我尝试的原因。此问题的答案中提供了更有效的代码解决方案,并且他们还确保不使用
选项(stringsAsFactors=FALSE)

如上所述,这只是点级数据,他们提供的CSV可以很好地替代:

library(tidyverse)

rp_csv_url <- "https://opendata.arcgis.com/datasets/24c0b37fa9bb4e16ba8bcaa7e806c615_0.csv"

httr::GET(
  url = rp_csv_url,
  httr::write_disk(basename(rp_csv_url)), # won't overwrite if it exists unless explicitly told to so you get caching for free
  httr::progress() # I suspect this is a big file so it's nice to see a progress bar
)

raleigh_police <- read_csv(basename(rp_csv_url))

mutate(
  raleigh_police, 
  longitude = as.numeric(longitude), # they come in wonky, still
  latitude = as.numeric(latitude) # they come in wonky, still
) -> raleigh_police

raleigh_police %>%
  filter(crime_description == "Burglary/Residential") %>% 
  ggplot() +
  stat_density_2d(
    aes(longitude, latitude, fill = stat(level)), 
    color = "#2b2b2b", size=0.125, geom = "polygon"
  ) +
  viridis::scale_fill_viridis(direction=-1, option="magma") +
  hrbrthemes::theme_ipsum_rc()
然后:

数据帧(
ct=sapply(sp::over(续,sp::geometry(rp_br),returnList=TRUE),长度),
id=1:长度(ct),
lvl=sapply(cl,功能(x)x$级别)
) %>% 
计数(lvl,wt=ct)%>%
变异(
pct=n/nrow(rp\u br),
pct_lab=sprintf(“有%s个点属于此级别”,比例::百分比(pct))
)
###tibble:10 x 4
##lvl n pct pct_实验室
##                                              
##  1   10.  7302 0.927 92.7%的分数属于该水平
##  2   20.  6243 0.792 79.2%的分数属于该水平
##  3   30.  4786 0.607 60.7%的分数属于这一水平
##  4   40.  3204 0.407 40.7%的分数属于这一水平
##  5   50.  1945 0.247 24.7%的分数在这一水平之内
##  6   60.  1277 0.162 16.2%的分数属于该水平
##  7   70.   793 0.101 10.1%的分数属于该水平
##  8   80.   474 0.0601 6.0%的分数属于该水平
##  9   90.   279 0.0354 3.5%的分数属于该水平
## 10  100.    44 0.00558 0.6%的分数属于该水平

除了在读取之前下载文件并将
readOGR
更改为
read\u sf
,它的工作原理与我的一样,为空几何图形导致的几个
NA
点保存警告:

库(tidyverse)
图书馆(sf)
#>链接至GEOS 3.6.1、GDAL 2.1.3、项目4.9.3
路径%
st_坐标()%>%
as_tible()%>%
ggplot()+
统计密度2d(aes(X,Y,fill=stat(level)),geom=“polygon”)
#>警告:已删除包含非有限值的5行(stat_density2d)。

空行:

raleigh\u警察%>%
过滤器(犯罪描述==“入室盗窃/住宅”,
st_是空的()
#>具有5个要素和21个字段的简单要素集合(5个几何图形为空)
#>几何图形类型:点
#>尺寸:XY
#>bbox:xmin:NA-ymin:NA-xmax:NA-ymax:NA
#>epsg(SRID):4326
#>proj4string:+proj=longlat+datum=WGS84+no_defs
#>#tibble:5 x 22
#>目的全球犯罪案件(数字犯罪)(类别犯罪)(代码犯罪)(描述)…
#>                                             
#>1 205318 8057315…P14076062入室盗窃/重置…30B入室盗窃/居住…
#>2417488 70afb27…P15027702入室盗窃/再犯罪…30B入室盗窃/居住…
#>3424718 bdf69fa…P18029113入室盗窃/重置…30B入室盗窃/居住…
#>436550 711c05b…P18044139入室盗窃/再犯罪…30B入室盗窃/居住…
#>5 442091 9d7a008…P18051764入室盗窃/重置…30B入室盗窃/居住…
#>#…还有16个变量:犯罪类型、报告的区块地址、,
#>#城市事件、城市、地区、报告日期、,
#>#报告年、报告月、报告日、,
#>#报告小时,报告日,纬度,
#>经度、机构、更新日期、几何
sessionInfo()
#>R版本3.5.1(2018-07-02)
#>平台:x86_64-apple-darwin15.6.0(64位)
#>正在运行:macOS 10.14.1
#> 
#>矩阵乘积:默认值
#>BLAS:/Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
#>LAPACK:/Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib
#> 
#>区域设置:
#>[1]en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#> 
#>附加基本包:
#>[1]统计图形设备GRUTILS数据集方法库
#> 
#>其他随附包裹:
#>[1]sf_0.7-1适用于猫_0.3.0 stringr_1.3.1
#>[4]dplyr_0.7.99.9000 purrr_0.2.5 readr_1.2.0
#>[7]tidyr_0.8.2 tibble_1.4.99.9005 ggplot2_3.1.0
#>[10]tidyverse_1.2.1
#> 
#>通过命名空间加载(未附加):
#>[1]tidyselect_0.2.5 haven_1.1.2 lattice_0.20-35
#>[4]色彩空间_1.3-2 htmltools_0.3.6 yaml_2.2.0
#>[7]rlang_0.3.0.1 e1071_1.7-0支柱_1.3.0.9001
#>[10]用R_2.1.2 DBI_1.0.0粘合_1.3.0
#>[13]modelr_0.1.2 readxl_1.1.0 plyr_1.8.4
#>[16]芒塞尔0.5.0 gtable 0.2.0 cellranger 1.1.0
#>[19]rvest_0.3.2评估_0.12标签_0.3
#>[22]knitr_1.20 class_7.3-14 broom_0.5.0
#>[25]Rcpp_0.12.19.3比例_1.0.0后端口_1.1.2
#>[28]classInt_0.2-3 jsonlite_1.5 hms_0.4.2.9001
#>[31]摘要0.6.18 stringi\u 1.2.4网格3.5.1
#>[34]rprojroot\u 1.3-2 cli\u 1.0.1工具\u 3.5.1
#>[37]magrittr_1.5 lazyeval_0.2.1蜡笔_1.3.4
#>[40]pkgconfig_2.0.2质量_7.3-51 xml2_1.2.0
#>[43]spData_0.2.9.4 lubridate_1.7.4资产
#>[46]rmarkdown_1.10 httr_1.3.1 R6_2.3.0
#>[49]单元0.6-1 nlme单元3.1-137编译器单元3.5.1

对我有用吗?我使用了
sf::read_sf
来读取它,但是你的代码会逐字逐句地运行。我真的希望你是在本地缓存该文件,而不是在利用别人的带宽和CPU进行快速和随意的操作。使用
readOGR()
还会导致对空几何图形的警告。贾索
library(tidyverse)

rp_csv_url <- "https://opendata.arcgis.com/datasets/24c0b37fa9bb4e16ba8bcaa7e806c615_0.csv"

httr::GET(
  url = rp_csv_url,
  httr::write_disk(basename(rp_csv_url)), # won't overwrite if it exists unless explicitly told to so you get caching for free
  httr::progress() # I suspect this is a big file so it's nice to see a progress bar
)

raleigh_police <- read_csv(basename(rp_csv_url))

mutate(
  raleigh_police, 
  longitude = as.numeric(longitude), # they come in wonky, still
  latitude = as.numeric(latitude) # they come in wonky, still
) -> raleigh_police

raleigh_police %>%
  filter(crime_description == "Burglary/Residential") %>% 
  ggplot() +
  stat_density_2d(
    aes(longitude, latitude, fill = stat(level)), 
    color = "#2b2b2b", size=0.125, geom = "polygon"
  ) +
  viridis::scale_fill_viridis(direction=-1, option="magma") +
  hrbrthemes::theme_ipsum_rc()
h <- c(MASS::bandwidth.nrd(rp_br$longitude), 
       MASS::bandwidth.nrd(rp_br$latitude))

dens <- MASS::kde2d(
  rp_br$longitude, rp_br$latitude, h = h, n = 100
)

breaks <- pretty(range(dens$z), 10)

zdf <- data.frame(expand.grid(x = dens$x, y = dens$y), z = as.vector(dens$z))

z <- tapply(zdf$z, zdf[c("x", "y")], identity)

cl <- grDevices::contourLines(
  x = sort(unique(dens$x)), y = sort(unique(dens$y)), z = dens$z,
  levels = breaks
)

sp::SpatialPolygons(
  lapply(1:length(cl), function(idx) {
    sp::Polygons(
      srl = list(sp::Polygon(
        matrix(c(cl[[idx]]$x, cl[[idx]]$y), nrow=length(cl[[idx]]$x), byrow=FALSE)
      )),
      ID = idx
    )
  })
) -> cont

sp::coordinates(rp_br) <- ~longitude+latitude
data_frame(
  ct = sapply(sp::over(cont, sp::geometry(rp_br), returnList = TRUE), length),
  id = 1:length(ct),
  lvl = sapply(cl, function(x) x$level)
) %>% 
  count(lvl, wt=ct) %>% 
  mutate(
    pct = n/nrow(rp_br),
    pct_lab = sprintf("%s of the points fall within this level", scales::percent(pct))
  )
## # A tibble: 10 x 4
##      lvl     n     pct pct_lab                                   
##    <dbl> <int>   <dbl> <chr>                                     
##  1   10.  7302 0.927   92.7% of the points fall within this level
##  2   20.  6243 0.792   79.2% of the points fall within this level
##  3   30.  4786 0.607   60.7% of the points fall within this level
##  4   40.  3204 0.407   40.7% of the points fall within this level
##  5   50.  1945 0.247   24.7% of the points fall within this level
##  6   60.  1277 0.162   16.2% of the points fall within this level
##  7   70.   793 0.101   10.1% of the points fall within this level
##  8   80.   474 0.0601  6.0% of the points fall within this level 
##  9   90.   279 0.0354  3.5% of the points fall within this level 
## 10  100.    44 0.00558 0.6% of the points fall within this level