使用地图包基于县数据的分区统计图

问题描述 投票:0回答:0

我正在尝试根据阿拉巴马州参议院选举等特殊选举的选举数据创建视觉效果。我将以下数据集导入为 R 中的数据框

> dput(jones_moore)
structure(list(County = c("Jefferson", "Madison", "Mobile", "Montgomery", 
"Shelby", "Baldwin", "Tuscaloosa", "Lee", "Morgan", "Etowah", 
"Calhoun", "Limestone", "Houston", "Lauderdale", "St. Clair", 
"Elmore", "Cullman", "Talladega", "Marshall", "Walker", "Colbert", 
"Autauga", "Blount", "Dallas", "DeKalb", "Coffee", "Tallapoosa", 
"Dale", "Jackson", "Russell", "Chilton", "Covington", "Escambia", 
"Lawrence", "Clarke", "Pike", "Chambers", "Marengo", "Geneva", 
"Marion", "Monroe", "Macon", "Barbour", "Pickens", "Franklin", 
"Butler", "Winston", "Cherokee", "Hale", "Bibb", "Washington", 
"Henry", "Randolph", "Lowndes", "Fayette", "Sumter", "Wilcox", 
"Choctaw", "Conecuh", "Perry", "Greene", "Crenshaw", "Lamar", 
"Clay", "Bullock", "Coosa", "Cleburne"), Jones = c(149522, 65664, 
62253, 48186, 27251, 22131, 30858, 19810, 10901, 10518, 11705, 
9606, 9107, 9908, 6203, 7711, 4156, 9967, 5134, 4317, 6865, 5606, 
2405, 10492, 3559, 3715, 4590, 3842, 3328, 6761, 2298, 2102, 
3640, 3028, 4346, 3989, 4247, 4495, 1289, 1311, 3244, 5780, 3680, 
3057, 1770, 2914, 911, 1525, 3894, 1567, 1799, 1896, 1692, 3779, 
1142, 3527, 3344, 2273, 2259, 3138, 3340, 1320, 779, 985, 2712, 
1414, 594), Moore = c(66309, 46313, 46725, 17705, 36424, 38445, 
22064, 14017, 19187, 15693, 14567, 14298, 14796, 12775, 15876, 
14411, 16602, 9698, 13828, 11909, 7762, 8752, 11621, 3485, 10097, 
8052, 7171, 6988, 7316, 3622, 7555, 6825, 4985, 5314, 3984, 4154, 
3308, 2804, 5431, 5268, 3276, 758, 2699, 2961, 4214, 2756, 4680, 
3996, 1691, 3599, 3320, 3014, 3229, 988, 3491, 814, 999, 1949, 
1815, 821, 462, 2347, 2847, 2586, 656, 1867, 2465), WriteIns = c(3710, 
3446, 1539, 743, 1718, 1699, 1007, 672, 668, 617, 399, 515, 285, 
382, 459, 338, 324, 223, 449, 259, 171, 253, 180, 60, 234, 202, 
148, 136, 154, 55, 132, 88, 87, 61, 43, 97, 75, 62, 93, 68, 40, 
20, 41, 46, 48, 41, 67, 110, 32, 66, 48, 38, 23, 13, 50, 18, 
16, 17, 18, 11, 9, 56, 29, 44, 7, 30, 30), Rpt = c("100%", "100%", 
"100%", "100%", "100%", "100%", "100%", "100%", "100%", "100%", 
"100%", "100%", "100%", "100%", "100%", "100%", "100%", "100%", 
"100%", "100%", "100%", "100%", "100%", "100%", "100%", "100%", 
"100%", "100%", "100%", "100%", "100%", "100%", "100%", "100%", 
"100%", "100%", "100%", "100%", "100%", "100%", "100%", "100%", 
"100%", "100%", "100%", "100%", "100%", "100%", "100%", "100%", 
"100%", "100%", "100%", "100%", "100%", "100%", "100%", "100%", 
"100%", "100%", "100%", "100%", "100%", "100%", "100%", "100%", 
"100%"), Total = c(219541, 115423, 110517, 66634, 65393, 62275, 
53929, 34499, 30756, 26828, 26671, 24419, 24188, 23065, 22538, 
22460, 21082, 19888, 19411, 16485, 14798, 14611, 14206, 14037, 
13890, 11969, 11909, 10966, 10798, 10438, 9985, 9015, 8712, 8403, 
8373, 8240, 7630, 7361, 6813, 6647, 6560, 6558, 6420, 6064, 6032, 
5711, 5658, 5631, 5617, 5232, 5167, 4948, 4944, 4780, 4683, 4359, 
4359, 4239, 4092, 3970, 3811, 3723, 3655, 3615, 3375, 3311, 3089
), JonesPct = c(68.1066406730406, 56.8898746350381, 56.3288905779201, 
72.3144340726956, NA, NA, 57.2196777244154, 57.4219542595438, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, 50.1156476267096, NA, NA, 
NA, NA, NA, 74.7453159507017, NA, NA, NA, NA, NA, 64.7729450086223, 
NA, NA, NA, NA, 51.9049325211991, NA, 55.6618610747051, 61.0650726803423, 
NA, NA, NA, 88.1366270204331, 57.3208722741433, 50.4122691292876, 
NA, 51.0243389949221, NA, NA, 69.3252625956917, NA, NA, NA, NA, 
79.0585774058577, NA, 80.9130534526268, 76.7148428538656, 53.6211370606275, 
55.2052785923754, 79.0428211586902, 87.6410390973498, NA, NA, 
NA, 80.3555555555556, NA, NA), MoorePct = c(NA, NA, NA, NA, 55.7001513923509, 
61.7342432757929, NA, NA, 62.384575367408, 58.4948561204711, 
54.6173746766151, 58.5527662885458, 61.170828510005, 55.3869499241275, 
70.4410329221759, 64.1629563668744, 78.7496442462764, NA, 71.237957858946, 
72.2414316044889, 52.45303419381, 59.9000752857436, 81.8034633253555, 
NA, 72.6925845932325, 67.2737906257833, 60.2149634730036, 63.7242385555353, 
67.7532876458604, NA, 75.6634952428643, 75.7071547420965, 57.2199265381084, 
63.2393192907295, NA, 50.4126213592233, NA, NA, 79.7152502568619, 
79.2537987061832, 49.9390243902439, NA, NA, NA, 69.8607427055703, 
NA, 82.7147401908802, 70.964304741609, NA, 68.7882262996942, 
64.2539191019934, 60.9135004042037, 65.3114886731392, NA, 74.5462310484732, 
NA, NA, NA, NA, NA, NA, 63.0405586892291, 77.8932968536252, 71.5352697095436, 
NA, 56.3877982482634, 79.799287795403)), row.names = c(NA, -67L
), class = "data.frame")

我有以下地图,我需要根据 Jones_Pct 和 Moore_Pct 列中的百分比进行着色:

library(maps)
library(RColorBrewer)
library(mapdata)

# Define the latitude and longitude coordinates for each city
huntsville_coords <- c(-86.5861, 34.7304)
birmingham_coords <- c(-86.8025, 33.5207)
tuscaloosa_coords <- c(-87.5692, 33.2098)
montgomery_coords <- c(-86.2999, 32.3792)
mobile_coords <- c(-88.0399, 30.6944)

# Define the city names
huntsville_name <- "Huntsville"
birmingham_name <- "Birmingham"
tuscaloosa_name <- "Tuscaloosa"
montgomery_name <- "Montgomery"
mobile_name <- "Mobile"

# Create the county map for Alabama
alabama_counties <- map("county", "alabama", fill = TRUE, col = "white", lty = 1)

# Add markers for each city to the map
points(huntsville_coords[1], huntsville_coords[2], pch = 20, cex = 2)
points(birmingham_coords[1], birmingham_coords[2], pch = 20, cex = 3)
points(tuscaloosa_coords[1], tuscaloosa_coords[2], pch = 20, cex = 1.5)
points(montgomery_coords[1], montgomery_coords[2], pch = 20, cex = 2.5)
points(mobile_coords[1], mobile_coords[2], pch = 20, cex = 2)

# Add text labels to the points
text(huntsville_coords[1] + 0.05, huntsville_coords[2] + 0.05,
     huntsville_name, pos = 4)
text(birmingham_coords[1] + 0.05, birmingham_coords[2] + 0.05,
     birmingham_name, pos = 4)
text(tuscaloosa_coords[1] + 0.05, tuscaloosa_coords[2] + 0.05,
     tuscaloosa_name, pos = 4)
text(montgomery_coords[1] + 0.05, montgomery_coords[2] + 0.05,
     montgomery_name, pos = 4)
text(mobile_coords[1] + 0.05, mobile_coords[2] + 0.05,
     mobile_name, pos = 4)

# Extract the county names from alabama_counties$names
county_names <- alabama_counties$names
county_names <- strsplit(county_names, ",")
county_names <- do.call(rbind, county_names)
county_names <- as.data.frame(county_names[,2])
names(county_names) <- "County"

我需要根据百分比范围为地图着色:

breaks <- c(45, 50, 55, 60, 65, 70, 75, 80, 83, 86, 90)

最终结果应该是使用

brewer.pal(10 "RdBu")
,其中琼斯的百分比较高是深蓝色调,而摩尔的百分比较高是深红色。

如何将地图数据的名称列(列为“州,县”)与列中仅列出县的数据结合起来,然后相应地为地图着色?

r maps choropleth
© www.soinside.com 2019 - 2024. All rights reserved.