R 子集形状文件数据
我有一个包含500个MSA(城市或城镇)的shapefile,我想将一些MSA子集,但我的R代码不起作用。如果您能帮助我或给我一些建议,我将不胜感激 这是指向shapefile的链接 这是我的R代码:R 子集形状文件数据,r,subset,shapefile,R,Subset,Shapefile,我有一个包含500个MSA(城市或城镇)的shapefile,我想将一些MSA子集,但我的R代码不起作用。如果您能帮助我或给我一些建议,我将不胜感激 这是指向shapefile的链接 这是我的R代码: # generating plot of bangladesh district map # the shape file is downloaded from the link # https://catalog.data.gov/dataset/tiger-line-shapefile-201
# generating plot of bangladesh district map
# the shape file is downloaded from the link
# https://catalog.data.gov/dataset/tiger-line-shapefile-2014-state-nebraska-current-place-state-based-shapefiles/resource/f6c6e766-d785-4da3-9141-7c0ea144d0bf
msa <- readOGR(dsn = "tl_2014_31_place.shp", layer="tl_2014_31_place")
msa <- spTransform(msa, CRS("+proj=longlat +datum=WGS84"))
msa <- fortify(msa) # converts shape file into ggplot-able data frame
ggplot(msa, aes(x=long, y=lat, group=group)) +
geom_polygon(fill="grey65", colour = alpha("white", 1/2), size = 0.2) + theme_bw() +
theme(legend.position = "none", text = element_blank(), line = element_blank()) +
coord_map("polyconic")
#孟加拉国地区地图生成图
#形状文件是从链接下载的
# https://catalog.data.gov/dataset/tiger-line-shapefile-2014-state-nebraska-current-place-state-based-shapefiles/resource/f6c6e766-d785-4da3-9141-7c0ea144d0bf
msa你没有把名字放在向量中
s您需要做更多的工作,才能在相同的数据框中绘制名称
、纬度
、长
library(rgdal)
library(tidyverse)
msa <- readOGR(dsn = paste0("tl_2014_31_place.shp"), layer="tl_2014_31_place")
msa <- spTransform(msa, CRS("+proj=longlat +datum=WGS84"))
msa@data$id = rownames(msa@data)
msa.points = fortify(msa, region = "id")
msa.df = merge(msa.points, msa@data, by = "id")
selectedCounties <- c("Alvo", "Avoca", "Cedar Creek", "Eagle", "Elmwood")
df <- msa.df %>%
filter(NAME %in% selectedCounties )
ggplot(df, aes(x=long, y=lat, group=group)) +
geom_polygon(fill="grey65", colour = alpha("white", 1/2), size = 0.2) + theme_bw() +
theme(legend.position = "none", text = element_blank(), line = element_blank()) +
coord_map("polyconic")
库(rgdal)
图书馆(tidyverse)
msa谢谢你。它起作用了。但是数据也不见了。{msa子集(msa,名称%在%c(“阿尔沃”,“阿沃卡”,“雪松溪”,“鹰”,“榆木”))或msa[msa$NAME%在%c(“阿尔沃”,“阿沃卡”,“雪松溪”,“鹰”,“榆木”),]当然,我想这样做,但我怎么能接受你的答案,我在哪里可以这样做?对不起,我是这个网站的新手。
library(rgdal)
library(tidyverse)
msa <- readOGR(dsn = paste0("tl_2014_31_place.shp"), layer="tl_2014_31_place")
msa <- spTransform(msa, CRS("+proj=longlat +datum=WGS84"))
msa@data$id = rownames(msa@data)
msa.points = fortify(msa, region = "id")
msa.df = merge(msa.points, msa@data, by = "id")
selectedCounties <- c("Alvo", "Avoca", "Cedar Creek", "Eagle", "Elmwood")
df <- msa.df %>%
filter(NAME %in% selectedCounties )
ggplot(df, aes(x=long, y=lat, group=group)) +
geom_polygon(fill="grey65", colour = alpha("white", 1/2), size = 0.2) + theme_bw() +
theme(legend.position = "none", text = element_blank(), line = element_blank()) +
coord_map("polyconic")