我用来能够毫无问题地运行这个脚本,但是现在fortify {ggplot2}命令给了我一个错误信息.任何可能是问题的暗示都会很棒!我使用fortify命令能够使用ggplot2对shapefile进行geom_map.
下面是我下载数据的脚本和链接.
#######################################################
#######################################################
rm(list = ls(all = TRUE))#clear workspace
getwd()
#upload packages
library(maps)
library(mapdata)
library(gridExtra)
library(rgdal)
library(rgeos)
library(ggplot2)
library(sp)
library(maptools)
gpclibPermit()
#setwd(".../FAO") FAO data are major fishing area divisions
> FAO<- readOGR(dsn="fao", layer="World_Fao_Zones")
OGR data source with driver: ESRI Shapefile
Source: "fao", layer: "World_Fao_Zones"
with 19 features and 1 fields
Feature type: wkbPolygon with 2 dimensions
> names(FAO);dim(FAO)
[1] "zone"
[1] 19 1
> str(FAO,max.level=2)
Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots
..@ data :'data.frame': 19 obs. …Run Code Online (Sandbox Code Playgroud) 我知道这个问题已在其他地方得到解答.我试图按照@jlhoward的指示,但显然我的技能太有限了.我可以再问你的帮助,R社区吗?
这就是我所拥有的:
瑞士的shapefile:链接
以及相应的CSV文件,用于市政名称及其邮政编码:链接
数据网站:cadastre.ch
关于最后一次流行投票的其他数据:直接链接,excel文件
我通过合并在CSV文件(wow.csv)(我要说明的数据)中添加了一列.该文件现在看起来像这样:
Gemeinden code Ja.Anteil Ortschaft PLZ Zusatzziffer Kantonskürzel E N
1 Aadorf 4551 78.78638 Aawangen 8522 2 TG 710206 263564
2 Aadorf 4551 78.78638 Ettenhausen TG 8356 0 TG 710129 259411
3 Aadorf 4551 78.78638 Aadorf 8355 0 TG 710588 261648
4 Aadorf 4551 78.78638 Guntershausen 8357 0 TG 711741 258934
5 Aadorf 4551 78.78638 Wittenwil 9547 0 TG 712002 262572
Run Code Online (Sandbox Code Playgroud)
之后我试着按照@jlhoward的说明:
muni多边形转换为数据框以进行绘图我试图像这样在圣何塞上放置网格:
您可以使用以下代码直观地制作网格:
ca_cities = tigris::places(state = "CA") #using tigris package to get shape file of all CA cities
sj = ca_cities[ca_cities$NAME == "San Jose",] #specifying to San Jose
UTM_ZONE = "10" #the UTM zone for San Jose, will be used to convert the proj4string of sj into UTM
main_sj = sj@polygons[[1]]@Polygons[[5]] #the portion of the shape file I focus on. This is the boundary of san jose
#converting the main_sj polygon into a spatialpolygondataframe using the sp package
tst_ps …Run Code Online (Sandbox Code Playgroud) 我正在尝试生成生成世界地图的图,其中每个国家/地区的颜色对应于存储在数据框中的特定值.
> aggregated_country_data
country num_responses region
1 AL 1 Albania
2 AM 1 Armenia
3 AR 32 Argentina
...
75 ZW 3 Zimbabwe
Run Code Online (Sandbox Code Playgroud)
这就是我尝试过的
library(rworldmap)
library(ggplot2)
map.world <- map_data(map="world")
gg <- ggplot()
gg <- gg + theme(legend.position="none")
gg <- gg + geom_map(data=map.world, map=map.world, aes(map_id=region, x=long, y=lat), fill="white", colour="black", size=0.25)
gg
Run Code Online (Sandbox Code Playgroud)
这绘制了世界地图就好了,接下来我想为每个国家添加颜色,与aggregated_country_data中的'num_responses'值成比例
gg <- gg + geom_map(data=aggregated_country_data, map=map.world, aes(map_id=region, fill=num_responses), color="white", size=0.25)
gg
Run Code Online (Sandbox Code Playgroud)
但是现在它对每种颜色进行颜色编码,因为它们对应于国家/地区代码,而不是在aggregated_country_data中列num_responses中的值.
很明显,我没有得到关于ggplot2的东西,但我无法弄清楚那是什么.
我很感激任何投入,布拉德
我弄清楚问题是什么,它与ggplot2或我正在做的任何其他事情无关.aggregated_country_data数据框的"region"名称与map.world中的名称不同.我的输入数据(aggregated_country_data)默认使用两个字母的国家代码,我使用国家代码R包转换为国家名称(在数据框中称为"区域"),但它使用的名称的命名约定与map.world.所以这是一个完全不同的问题.