我用tmap
包制作了人口密度图
然而,在将我自己的数据合并到tmap中的World @ data之后,我发现我的地图中的USA丢失了,而越南的数据在美国的地方错位了。卢旺达代替俄罗斯。检查合并后的数据,似乎没有出错。我的代码:
df$iso3 <- countrycode(df$country, 'name', 'iso3c')
#Remove the NAs in orginal datasetm, there are three NAs in World@data
df <- na.omit(df)
data(World)
str(World, max.level = 2)
World@data <- merge(World@data, df, by.x = "iso_a3", by.y = "iso3", all.x = TRUE)
map <- tm_shape(World)+
tm_polygons("1950",
title = "Pop Class",
palette = "Blues",
contrast = 0.7,
border.col = "gray30",
id = "name")+
tm_text("iso_a3", size = "AREA", col = "gray30", root =3)+
tm_format_World()+
tm_style_gray()
current.mode <- tmap_mode("plot")
map111_6
我有来自wpp2015的公共数据集,它与我的数据集不完全相同,但会提供我的数据框架结构的直觉。
dput(df) <- structure(list(name = structure(c(1L, 3L, 4L, 5L, 6L, 14L, 7L, 11L, 13L, 15L, 16L, 17L, 8L, 18L, 20L, 23L, 24L, 25L, 26L, 27L, 21L, 190L, 28L, 29L, 146L, 31L, 19L, 33L, 34L, 35L, 32L, 37L, 201L, 40L, 42L, 43L, 46L, 47L, 48L, 134L, 49L, 58L, 50L, 52L, 53L, 55L, 56L, 22L, 59L, 61L, 65L, 67L, 68L, 71L, 69L, 70L, 73L, 74L, 75L, 76L, 77L, 60L, 78L, 80L, 79L, 203L, 81L, 82L, 108L, 83L, 84L, 85L, 86L, 87L, 88L, 90L, 91L, 93L, 44L, 94L, 95L, 96L, 97L, 98L, 99L, 100L, 101L, 102L, 51L, 103L, 104L, 106L, 105L, 107L, 57L, 173L, 109L, 110L, 111L, 115L, 116L, 113L, 119L, 120L, 121L, 124L, 45L, 125L, 126L, 127L, 128L, 129L, 130L, 131L, 132L, 133L, 136L, 141L, 174L, 142L, 144L, 145L, 160L, 147L, 148L, 149L, 54L, 9L, 150L, 231L, 151L, 152L, 153L, 154L, 158L, 138L, 162L, 163L, 164L, 165L, 166L, 167L, 168L, 170L, 89L, 214L, 171L, 172L, 175L, 176L, 177L, 178L, 179L, 202L, 181L, 182L, 183L, 184L, 185L, 186L, 187L, 188L, 233L, 189L, 191L, 194L, 241L, 200L, 196L, 205L, 237L, 206L, 207L, 208L, 209L, 210L, 211L, 213L, 215L, 216L, 217L, 223L, 218L, 219L, 220L, 221L, 222L, 212L, 66L, 224L, 41L, 225L, 226L, 227L, 30L, 229L, 230L, 232L, 180L, 239L, 240L, 238L, 143L, 117L, 2L, 112L, 156L, 63L, 72L, 159L, 62L, 140L, 155L, 197L, 234L, 36L, 38L, 193L, 192L, 235L, 64L, 157L, 199L, 236L, 12L, 135L, 195L, 161L, 10L, 114L, 204L, 118L, 137L, 169L, 122L, 123L, 228L, 92L, 139L, 39L, 198L), .Label = c("Afghanistan", "Africa", "Albania", "Algeria", "Angola", "Antigua and Barbuda", "Argentina", "Armenia", "Aruba", "Asia", "Australia", "Australia/New Zealand", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bhutan", "Bolivia (Plurinational State of)", "Bosnia and Herzegovina", "Botswana", "Brazil", "Brunei Darussalam", "Bulgaria", "Burkina Faso", "Burundi", "Cabo Verde", "Cambodia", "Cameroon", "Canada", "Caribbean", "Central African Republic", "Central America", "Central Asia", "Chad", "Channel Islands", "Chile", "China", "China, Hong Kong SAR", "China, Macao SAR", "China, Taiwan Province of China", "Colombia", "Comoros", "Congo", "Costa Rica", "Cote d'Ivoire", "Croatia", "Cuba", "Curacao", "Cyprus", "Czech Republic", "Dem. People's Rep. of Korea", "Dem. Republic of the Congo", "Denmark", "Djibouti", "Dominican Republic", "Eastern Africa", "Eastern Asia", "Eastern Europe", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Europe", "Fiji", "Finland", "France", "French Guiana", "French Polynesia", "Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Greece", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "High-income countries", "Honduras", "Hungary", "Iceland", "India", "Indonesia", "Iran (Islamic Republic of)", "Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Kuwait", "Kyrgyzstan", "Lao People's Dem. Republic", "Latin America and the Caribbean", "Latvia", "Least developed countries", "Lebanon", "Lesotho", "Less developed regions", "Less developed regions, excluding China", "Liberia", "Libya", "Lithuania", "Low-income countries", "Lower-middle-income countries", "Luxembourg", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Martinique", "Mauritania", "Mauritius", "Mayotte", "Melanesia", "Mexico", "Micronesia", "Micronesia (Fed. States of)", "Middle-income countries", "Middle Africa", "Mongolia", "Montenegro", "More developed regions", "Morocco", "Mozambique", "Myanmar", "Namibia", "Nepal", "Netherlands", "New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Northern Africa", "Northern America", "Northern Europe", "Norway", "Oceania", "Oman", "Other less developed countries", "Pakistan", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Poland", "Polynesia", "Portugal", "Puerto Rico", "Qatar", "Republic of Korea", "Republic of Moldova", "Reunion", "Romania", "Russian Federation", "Rwanda", "Saint Lucia", "Samoa", "Sao Tome and Principe", "Saudi Arabia", "Senegal", "Serbia", "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands", "Somalia", "South-Central Asia", "South-Eastern Asia", "South Africa", "South America", "South Sudan", "Southern Africa", "Southern Asia", "Southern Europe", "Spain", "Sri Lanka", "St. Vincent and the Grenadines", "State of Palestine", "Sub-Saharan Africa", "Sudan", "Suriname", "Swaziland", "Sweden", "Switzerland", "Syrian Arab Republic", "Tajikistan", "TFYR Macedonia", "Thailand", "Timor-Leste", "Togo", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United Republic of Tanzania", "United States of America", "United States Virgin Islands", "Upper-middle-income countries", "Uruguay", "Uzbekistan", "Vanuatu", "Venezuela (Bolivarian Republic of)", "Viet Nam", "Western Africa", "Western Asia", "Western Europe", "Western Sahara", "World", "Yemen", "Zambia", "Zimbabwe"), class = "factor"), `1950` = c(7752.118, 1263.171, 8872.247, 4354.882, 46.301, 2895.997, 17150.335, 8177.344, 6936.445, 79.088, 115.614, 37894.68, 1353.506, 210.995, 8628.489, 176.795, 3089.649, 2661.293, 412.533, 53974.726, 68.918, 89.793, 48.001, 7250.999, 17527.243, 2308.923, 7745.003, 4432.716, 4466.498, 13736.997, 178.066, 1326.653, 8075.81, 2502.314, 6142.899, 544112.923, 7561.863, 12340.899, 156.334, 15.141, 807.726, 12183.661, 959.489, 3850.295, 5919.997, 494.014, 8902.619, 2255.221, 4268.27, 2364.65, 3470.162, 2199.897, 225.536, 18128.034, 1142.15, 1100.998, 288.993, 4008.299, 41879.607, 25.479, 60.268, 62.001, 473.3, 3527.004, 271.372, 931.926, 69786.246, 4980.878, 33.05, 7566.002, 76.676, 209.999, 59.65, 3146.073, 3093.651, 406.562, 3221.277, 1487.235, 1973.998, 9337.723, 142.656, 376325.205, 69543.319, 17119.263, 5719.191, 2913.093, 1257.971, 46598.602, 2630.131, 1402.896, 82199.47, 6702.996, 448.861, 6076.757, 10549.469, 19211.386, 152.25, 1740, 1682.916, 1334.618, 733.942, 1949, 930.026, 1113.382, 2567.402, 296.001, 196.482, 4083.554, 2953.871, 6109.907, 73.715, 4708.425, 311.997, 222.001, 660.491, 493.254, 28012.558, 780.2, 2341.003, 394.738, 8985.99, 6313.29, 456.418, 485.274, 8483.321, 10027.047, 100.184, 38.066, 64.824, 47.695, 1908.001, 1294.993, 2559.703, 37859.745, 3265.278, 32, 37542.38, 859.66, 1708.192, 1473.245, 7727.735, 18580.487, 24824.013, 8416.969, 535.429, 433.398, 2218, 24.999, 248.111, 16236.292, 102798.657, 2186.187, 82.783, 67, 60, 3121.336, 2476.638, 6732.256, 36.322, 1944.001, 1022.098, 3436.574, 24809.903, 1473.094, 2264.081, 13683.162, 2746.854, 28069.737, 2582.929, 5733.944, 13.766, 214.999, 273, 7009.913, 4668.088, 3413.329, 1531.502, 20710.356, 1395.458, 47.22, 645.628, 69.59, 3605.31, 21238.496, 1211, 5158.193, 37297.652, 1254.444, 20897.237, 50616.012, 102.235, 7649.766, 157813.04, 26.795, 4284.457, 2238.506, 6945.397, 5481.977, 82.102, 4402.32, 2316.95, 2525149.312, 812988.79, 1712160.522, 228901.723, 168843.911, 171614.868, 666585.791, 549089.107, 12681.946, 66922.702, 26400.57, 49221.876, 15587.911, 70768.664, 17075.654, 38028.823, 164900.344, 511574.182, 50957.44, 220170.535, 78029.913, 108632.979, 142255.68, 10085.345, 2199.497, 113739.434, 1516435.967, 1394017.757, 195724.555, 179679.847, 1158315.256, 155.093, 242.011, 130103.438, 768893.01, 824937.314, 800383.367, 1593830.324, 18130.895, 493443.287)), .Names = c("name", "1950"), class = "data.frame", row.names = c(NA, -241L))
任何人都可以帮助查看我的脚本吗?非常感谢。
答案 0 :(得分:1)
World
对象是一个特定的,相当复杂的对象,所以用merge
直接修改它的数据可能不是一个好主意...更好地使用append_data
函数tmaptools
包...
df <- read.table(header = T, stringsAsFactors = F, text = "
country pop_density
Afghanistan 1
Angola 2
Albania 3
Argentina 5
Armenia 6
France 7
Australia 8
")
# your origin value should be `'country.name'`, not `'name'`
library(countrycode)
df$iso3 <- countrycode(df$country, 'country.name', 'iso3c')
library(tmap)
data(World)
# the `World` object is a specific, rather complex object, so it's probably not
# a great idea to modify it's data directly with `merge`... better to use the
# `append_data` function in the `tmaptools` package...
library(tmaptools)
World <- append_data(World, df, key.shp = "iso_a3", key.data = "iso3",
ignore.na = T)
tm_shape(World) +
tm_polygons("pop_density", title = "Pop Class", palette = "Blues",
contrast = 0.7, border.col = "gray30", id = "name") +
tm_text("iso_a3", size = "AREA", col = "gray30", root = 3)