R ggplot2地图为空

R ggplot2地图为空,r,ggplot2,shapes,R,Ggplot2,Shapes,我正在使用类似于代码的其他脚本与来自加拿大统计局的不同形状文件。然而,我无法得到一个简单的脚本来处理省级地图。我认为问题很简单,但我看不出来 setwd("D:\\OneDrive\\lfs_stuff") project_folder<-getwd() data_folder<-project_folder library(tidyverse) #now start the map library(rgeos) library(rgdal) library(maptools) li

我正在使用类似于代码的其他脚本与来自加拿大统计局的不同形状文件。然而,我无法得到一个简单的脚本来处理省级地图。我认为问题很简单,但我看不出来

setwd("D:\\OneDrive\\lfs_stuff")
project_folder<-getwd()
data_folder<-project_folder
library(tidyverse)
#now start the map
library(rgeos)
library(rgdal)
library(maptools)
library(sp)
library(mapproj)
library(ggplot2)
#get test data
mydata<-read_csv("map_data.csv",col_types=list(col_character(),col_double()))
print(mydata)
# shape file came from this link for a digital shape file
# http://www12.statcan.gc.ca/census-recensement/2011/geo/bound-limit/files-fichiers/2016/lpr_000a16a_e.zip
target_url<-"http://www12.statcan.gc.ca/census-recensement/2011/geo/bound-limit/files-fichiers/2016/lpr_000a16a_e.zip"
url_file<-"lpr_000a16a_e.zip"
download_target<-paste0(project_folder,"/",url_file)
    download.file(target_url,download_target,mode="wb",quiet=FALSE)
    unzip(download_target,overwrite=TRUE,exdir=data_folder)
provincial_shape_file<-gsub(".zip",".shp",download_target)
provincial_shp<-readOGR(dsn=provincial_shape_file,layer="lpr_000a16a_e")
#convert it to the reqired data structure. the id vbl will contain the provincial codes
prov_base_map<-fortify(provincial_shp,region="PRUID")
map_data_1<-merge(prov_base_map,as_data_frame(mydata),by="id")
map1<-ggplot()+
geom_map(data=map_data_1,map=map_data_1,stat="identity",
aes(map_id=id,x=long,y=lat,fill=(pch),group=group),
colour="black",size=0.3)+
coord_map()
print(map1)

这里有一种使用
sf
的方法(尽管我认为最终的问题是没有正确识别
id
):

库(sf)
图书馆(httr)
图书馆(tidyverse)
read.csv(文本='“id”,“pch”
"10",0.667259786476859
"11",5.63186813186813
"12",2.12053571428572
"13",-0.563697857948142
"24",0.150669774230772
"35",1.15309092428315
"46",0.479282622139765
"47",1.70242950877815
"48",1.84482533036765
"59",1.96197656978394',
stringsAsFactors=FALSE,
colClasses=c(“字符”、“双精度”)->xdf
#内置缓存的跨平台友好d/l
try(httr::GET)(
url=”http://www12.statcan.gc.ca/census-recensement/2011/geo/bound-limit/files-fichiers/2016/lpr_000a16a_e.zip",
httr::write_disk(“~/Data/lpr_00a16a_e.zip”),
httr::progress()
))->res

我真的很感激你的建议。我使用了gdal程序。我不知道sf图书馆会工作得这么好。你能解释一下为什么没有识别身份证吗。在以前使用其他形状文件运行时,fortify似乎能够正确识别id。
"id","pch"
"10",0.667259786476859
"11",5.63186813186813
"12",2.12053571428572
"13",-0.563697857948142
"24",0.150669774230772
"35",1.15309092428315
"46",0.479282622139765
"47",1.70242950877815
"48",1.84482533036765
"59",1.96197656978394
library(sf)
library(httr)
library(tidyverse)

read.csv(text='"id","pch"
"10",0.667259786476859
"11",5.63186813186813
"12",2.12053571428572
"13",-0.563697857948142
"24",0.150669774230772
"35",1.15309092428315
"46",0.479282622139765
"47",1.70242950877815
"48",1.84482533036765
"59",1.96197656978394',
         stringsAsFactors=FALSE,
         colClasses = c("character", "double")) -> xdf

# cross-platform-friendly d/l with caching built-in
try(httr::GET(
  url = "http://www12.statcan.gc.ca/census-recensement/2011/geo/bound-limit/files-fichiers/2016/lpr_000a16a_e.zip",
  httr::write_disk("~/Data/lpr_00a16a_e.zip"),
  httr::progress()
)) -> res

fils <- unzip("~/Data/lpr_00a16a_e.zip", exdir = "~/Data/lpr")

ca_map <- st_read(grep("shp$", fils, value=TRUE), stringsAsFactors = FALSE)
ca_map <- st_simplify(ca_map, TRUE, 10) # you don't need the coastlines to be that detailed
ca_map <- left_join(ca_map, xdf, by=c("PRUID"="id"))

ggplot(ca_map) +
  geom_sf(aes(fill = pch)) +
  viridis::scale_fill_viridis(direction=-1, option="magma") +
  coord_sf()