2

私は R とマッピングが初めてで、特定のデータのマップを作成したいと考えています。2010 年にモントリオールを訪れたカナダの国勢調査部門の訪問者を示す "D.Montreal" という名前のデータ セットがあります。人数に応じて地図の色を変えます。

カナダの国勢調査部門の境界を設定するために使用できるパッケージはありますか?

ここまでで、カナダの概要を説明しました。

map("worldHires","Canada", xlim=c(-141,-53), ylim=c(40,85), col="grey90", fill=TRUE)

どうもありがとう!

4

1 に答える 1

12

確かに幅広い質問であり、(Roman が提案したように) 空間タスク ビューを使用することは、優れた出発点です。マッピングに関しては ggplot を使用することを好みますが、多少の作業は必要です。次のいくつかは、いくつかの空間タスクのトピックに到達するまで実際には意味がありませんが、基本的にはStatistics Canadaからシェープファイルを取得し、単純化して (ポリゴンの読み込み/プロットに永遠にかかることはありません)、色を付けます。エリア別:

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

ここに画像の説明を入力

人口データのようなものを扱うことは非常に似ています。州の人口のデータをいくつか見つけました。以下は、人口に基づいてそれらのコロプレスを作成します(単純化しすぎている可能性がありますが、国勢調査の地区をプロットしやすくすることを目指していました)。

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

ここに画像の説明を入力

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

ここに画像の説明を入力

于 2014-09-27T20:25:37.757 に答える