gpt4 book ai didi

r - R中的加拿大人口普查 map 划分

转载 作者:行者123 更新时间:2023-12-03 07:55:33 27 4
gpt4 key购买 nike

我对 R 和映射非常陌生,我想创建某些数据的映射。我有一组名为“D.Montreal”的数据,它显示了 2010 年访问蒙特利尔的加拿大人口普查部门的访客。我想使用这些数据创建一张 map ,以显示有多少人来自不同的地区,也许通过着色 map 根据人数不同颜色。

有没有可以用来划定加拿大人口普查区的软件包?

到目前为止,我已经制作了加拿大的轮廓:
map("worldHires","Canada", xlim=c(-141,-53), ylim=c(40,85), col="grey90", fill=TRUE)
非常感谢!

最佳答案

绝对是一个广泛的问题,通过空间任务 View (如 Roman 建议的那样)是一个很好的起点。在映射方面,我更喜欢使用 ggplot,尽管它需要一些麻烦。在您遇到一些空间任务主题之前,下面的一些内容不会真正有意义,但它本质上是从 Statistics Canada 中获取一个 shapefile。 ,简化它(因此多边形不会永远加载/绘制),然后按区域着色:

library(rgeos)
library(rgdal)
library(maptools)
library(sp)
library(ggplot2)

# decent, uncluttered map theme (needs devtools package tho)
devtools::source_gist("https://gist.github.com/hrbrmstr/33baa3a79c5cfef0f6df")

# grab the file from "Statistics Canada"
download.file("http://www12.statcan.gc.ca/census-recensement/2011/geo/bound-limit/files-fichiers/gcd_000b11a_e.zip",
destfile="gcd_000b11a_e.zip")

unzip("gcd_000b11a_e.zip")

# this simplifies the polygons so they load/plot faster
system("ogr2ogr canada.shp gcd_000b11a_e.shp -simplify 0.01")

# what layers do we have? you can use this to check
# ogrListLayers("gcd_000b11a_e/canada.shp")
# but there are none, so the shapefile is the layer

canada <- readOGR("gcd_000b11a_e/","canada")

# do this to see what's available from an "identifier" standpoint
# "CDNAME" seems to be the census district name
# "PRNAME" seems to be the province name
# str(canada@data)

# rig up some data
# make a data frame of census division areas
# you can assign as many value columns as you like
# they get merged in later and can be used as the fill level
# we'll use the area as the fill level
map_areas <- data.frame(id=canada@data$CDNAME,
area=sapply(slot(canada, "polygons"), slot, "area") )

# this takes a while, but it makes a data frame for use with
# ggplot and lets us use the census division name for doing things
# like applying colors
canada_map <- fortify(canada, region="CDNAME")

# merge in areas
canada_map <- merge(canada_map, map_areas, by="id")

gg <- ggplot()
gg <- gg + geom_map(data=canada_map, map=canada_map,
aes(map_id=id, x=long, y=lat, group=group, fill=log1p(area)),
color="white", size=0.1)
gg <- gg + coord_map() # can choose other projections
gg <- gg + theme_map()
gg

enter image description here

处理人口数据之类的东西非常相似。我找到了一些省份人口数据,以下根据人口为那些(可能过于简化,但我的目标是使人口普查区更容易绘制)进行了一个等值线。
province_pop <- data.frame(
id=c("Newfoundland and Labrador / Terre-Neuve-et-Labrador",
"Prince Edward Island / Île-du-Prince-Édouard",
"Nova Scotia / Nouvelle-Écosse", "New Brunswick / Nouveau-Brunswick",
"Quebec / Québec", "Ontario", "Manitoba", "Saskatchewan",
"Alberta", "British Columbia / Colombie-Britannique", "Yukon",
"Northwest Territories / Territoires du Nord-Ouest", "Nunavut"),
population=c(526977.0, 146283.0, 942668.0, 753914.0, 8214672.0, 13678740.0,
1282043.0, 1125410.0, 4121692.0, 4631302.0, 36510.0, 43623.0, 36585.0))

canada_map <- fortify(canada, region="PRNAME")
canada_map <- merge(canada_map, province_pop, by="id")

gg <- ggplot()
gg <- gg + geom_map(data=canada_map, map=canada_map,
aes(map_id=id, x=long, y=lat, group=group, fill=population),
color="white", size=0.5)
gg <- gg + scale_fill_continuous(low="#ccebc5", high="#084081")
gg <- gg + coord_map()
gg <- gg + theme_map()
gg

enter image description here

你肯定想看看 canada@data 中的列的值。因为,正如您在省名中看到的那样,它们可能同时包含法语和英语版本。

我可以伪造一些人口普查区的人口数据来表明它是相同的方法:
fake_census_pop <- data.frame(
id=unique(as.character(canada@data$CDNAME)),
fake_pop=sample(500000:30000000, length(unique(as.character(canada@data$CDNAME)))))

canada_map <- fortify(canada, region="CDNAME")
canada_map <- merge(canada_map, fake_census_pop, by="id")

gg <- ggplot()
gg <- gg + geom_map(data=canada_map, map=canada_map,
aes(map_id=id, x=long, y=lat, group=group, fill=fake_pop),
color="white", size=0.1)
gg <- gg + scale_fill_continuous(low="#ccebc5", high="#084081")
gg <- gg + coord_map()
gg <- gg + theme_map()
gg

enter image description here

关于r - R中的加拿大人口普查 map 划分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26078193/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com