我在R中使用tigris包来获取某些地理编码的块组:
我的数据框:
> head(MyData)
ï..AddressId StreetAddressLine1 City ZipCode StateAbbreviation Latitude
1 60350 119 Summit Ave Winthrop 2152 MA 42.38445
2 6955669 1705 Chaise Ct Carson City 89703 NV 39.17730
3 6956266 13 Phillips Ave Shrewsbury 1545 MA 42.28119
4 6997538 38003 Wheeler Rd Dexter 97431 OR 43.94063
5 6956697 3919 Birkdale Ln Se Olympia 98501 WA 46.99440
6 6955879 117 Highland Ave Staten Island 10301 NY 40.61681
Longitude
1 -70.97491
2 -119.77846
3 -71.75263
4 -122.85481
5 -122.83683
6 -74.09748 …Run Code Online (Sandbox Code Playgroud) 我想知道在 R 中使用传单包绘制大量标记和多边形时是否有其他人遇到过类似的问题。这通常应该是这样的:

但是,当我放大/缩小地图时,多边形和标记显然不合适(或者您可以说底图没有正确调整)。下面包含一个示例:

当我绘制较小的区域或很少的标记时,我不会遇到这个问题。我想知道是否有办法提高性能。非常感谢您的帮助!
我的代码示例如下:
map1 <- leaflet() %>%
addProviderTiles("CartoDB.Positron") %>%
addPolygons(data = data_merged, group="Default",
fillColor = ~pal(minority_population), color = "orange",
fillOpacity = 0.7,weight = 1, smoothFactor = 0.2, popup = popup) %>%
addMarkers(data = branches_temp, ~long, ~lat,
popup=~name_branch, group="Branch Locations",
icon=icons(iconUrl = "./data/bank_blue_marker.png",iconWidth=20, iconHeight=20))
Run Code Online (Sandbox Code Playgroud) library(tidyverse)
library(tigris)
library(sf)
santacruz <- tracts("CA", "Santa Cruz")
coords_sf <- locations %>% st_as_sf(coords = c("Longitude", "Latitude"), crs=4269)
Run Code Online (Sandbox Code Playgroud)
这应该有相同的 CRS,但是当我尝试时
st_intersects(coords_sf, santacruz)
Run Code Online (Sandbox Code Playgroud)
我明白了
错误:st_crs(x) == st_crs(y) 不为 TRUE
然后我尝试了
st_set_crs(santacruz, 4269)
st_set_crs(coords_sf, 4269)
st_transform(santacruz, 4269)
st_transform(coords_sf, 4269)
Run Code Online (Sandbox Code Playgroud)
但它不起作用。我也尝试过
st_transform(santacruz, crs = "+proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs")
st_transform(coords_sf, crs = "+proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs")
Run Code Online (Sandbox Code Playgroud)
无论我尝试设置 CRS 并在尝试时对其进行转换
st_intersects(coords_sf, santacruz)
Run Code Online (Sandbox Code Playgroud)
我明白了
错误:st_crs(x) == st_crs(y) 不为 TRUE
此时我无法判断是否是设置 CRS 或转换或st_intersects功能有问题。谢谢,
I am using the Tigris package to download shape files with the code,
options(tigris_class = "SF")
Run Code Online (Sandbox Code Playgroud)
While I can easily map polygons
ggplot(data=zctas) +
geom_sf(aes())
Run Code Online (Sandbox Code Playgroud)
I am struggling to create a marker/point instead of a filled polygon (for example, a shape -- like a circle inside a zctas) as the shape file I've pulled down from tigris does not have a lat/long for the x and y AES mapping. The shape file has a "geometry" column, and I am wondering …
我正在尝试获取两个 shapefile(落在某些大都市区边界内的人口普查区)的交集。我能够成功获得相交特征,但是当我尝试将 sf_intersect 的输出转换为 SpatialPolygonsDataframe 时,出现错误:
“as_Spatial(from) 中的错误:不支持从特征类型 sfc_GEOMETRY 到 sp 的转换”
这是我的代码:
library(sf)
library(dplyr)
library(tigris)
library(sp)
#download shapefiles corresponding to metro areas
metro_shapefiles<-core_based_statistical_areas(cb = FALSE, year = 2016)
#convert to sf and filter
metro_shapefiles<-st_as_sf(metro_shapefiles)%>%filter(GEOID==31080 )
#Data for California
census_tracts_california<-tracts(state="CA",year=2016)
census_tracts_california<-st_as_sf(census_tracts_california)
#INTERSECT AND CONVERT BACK TO SP
census_tracts_intersected1<-st_intersection(census_tracts_california,
metro_shapefiles)
#back to spatial
census_tracts_intersected1<-as(census_tracts_intersected1,"Spatial")
Run Code Online (Sandbox Code Playgroud) 我有相对大量的坐标,我想获取人口普查区域(除了 FIPS 代码)。我知道我可以使用call_geolocator_latlon(如这里所做的那样)查找单个纬度/经度对,但这对我的目的来说似乎不切实际,因为该函数对人口普查局的 API 发出一次调用,我想这需要很长时间在我的约 200,000 双上运行。
有没有更快的方法来做到这一点,也许是通过使用block_groups函数下载每个州的 shapefile并从那里映射到经纬度到人口普查区?
有没有办法并排绘制两个县(不改变实际比例)来比较它们的大小。
我希望并排绘制圣地亚哥和圣克拉拉,以展示它们的实际大小。
谢谢
library(tigris)
library(ggplot2)
san_diego <- county_subdivisions(state = "CA", county = "San Diego County", cb = TRUE, year = NULL)
santa_clara <- county_subdivisions(state = "CA", county = "Santa Clara County", cb = TRUE, year = NULL)
gg_san_diego <- ggplot() +
geom_sf(data = san_diego,
color="black",
fill="white",
size=0.25)
gg_santa_clara <- ggplot() +
geom_sf(data = santa_clara,
color="black",
fill="white",
size=0.25)
gg_san_diego
gg_santa_clara
Run Code Online (Sandbox Code Playgroud) 我运行以下命令:
library(dplyr)
library(sf)
library(tigris)
library(tmap)
options(tigris_class = 'sf')
options(tigris_use_cache = TRUE)
nj = tigris::states(cb = T, year = 2015) %>%
filter(STUSPS == 'NJ')
nj_msas = tigris::core_based_statistical_areas(cb = T, year = 2015) %>%
filter(grepl('NJ', NAME)) %>%
sf::st_intersection(nj)
tmap_mode('plot')
nj_msas %>%
tm_shape() +
tm_polygons()
Run Code Online (Sandbox Code Playgroud)
最后一个块给出了错误:
vapply(g2, st_is_empty, logical(1)) 中的错误:值长度必须为 1,但 FUN(X[[3]]) 结果长度为 148
当我删除 时st_intersection,最后一个块没有错误。我无法通过谷歌搜索在任何地方找到此错误消息。有谁知道发生了什么?
另外,如果我运行除最后一个块之外的所有上述内容,并且我ggplot2::geom_sf用来创建地图而不是tmap函数,我会得到我想要的地图。没有错误。
我正在使用 Ubuntu 18.04.1。RStudio v1.1.463。R 3.5.2。底格里斯河 0.7。地图 2.2。平方英尺 0.7-2。