gpt4 book ai didi

r - 如何使用 tmap 中的自定义中断来调整调色板?

转载 作者:行者123 更新时间:2023-12-04 01:36:38 26 4
gpt4 key购买 nike

我想用 tmap 包在密度图中绘制 1950 年的世界人口。
我手动将人口数据分成 22 个类别,并为每个类别填充不同的颜色。
我的代码是:

    library(tmap)
data(World)

map <- tm_shape(World)+
tm_fill("1950",
title = "Population class",
breaks = c(0, 100 ,200, 300, 400, 500, 600, 700, 800, 900,
1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800,
1900, 2000, 3000, 4000),
textNA = "No data",
colorNA = "white",
palette = "topo")+
tm_borders()+
tm_layout("World Density Population Map")
tm_style_classic()
current.mode <- tmap_mode("plot")
map

最后一步“map”发生错误:

Error: Invalid palette



我有一个来自 wpp2015 的公共(public)数据集,它与我的数据集不完全相同,但可以直观地了解我的数据框结构。
     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))   

如果有人可以提供帮助,我将不胜感激。

最佳答案

假设您使用的是 World来自 tmap 的数据集包裹。您的代码存在三个问题。首先,没有名为 1950 的列.表示人口密度的列是pop_est_dens .二、假设pop_est_dens是您要绘制的列,breaks太大了,因为最大值只有 1200 左右。第三,topo不是有效的 palette姓名。

因此,我将您的代码修改如下。

library(tmap)
data(World)

map <- tm_shape(World)+
tm_fill("pop_est_dens",
title = "Population class",
breaks = c(0, 100 ,200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200),
style = "fixed",
textNA = "No data",
colorNA = "white",
palette = "Reds")+
tm_borders() +
tm_layout("World Density Population Map")
map

enter image description here

关于r - 如何使用 tmap 中的自定义中断来调整调色板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49423007/

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