The data for this code is divided into nine compartments, but I just want to be divided into five compartments like my screenshot, what should I do

library(ggplot2)
library(maps)
library(dplyr)

加载入数据和预处理

data <- read.csv("D:\Users\gbd数据新\19902021dalys204国家.csv", header = TRUE)

PC <- data %>%
filter(metric_name == 'Rate') %>%
filter(year == '1990') %>%
filter(measure_name == 'DALYs (Disability-Adjusted Life Years)') %>%
filter(age_name == '55+ years')

PC_cal <- PC %>% mutate(val = as.numeric(val),
lower = as.numeric(lower),
upper = as.numeric(upper)) %>%
mutate(val = round(val,2),
lower = round(lower,2),
upper = round(upper,2))

PC_cal <- PC_cal[, c(4, 6, 8, 14, 15, 16)]

names(PC_cal) <- c('location', 'sex', 'age', 'PC', 'LCI', 'UCI')

PC_cal <- PC_cal %>% mutate(PC = as.numeric(PC),
LCI = as.numeric(LCI),
UCI = as.numeric(UCI)) %>%
mutate(PC = round(PC,2),
LCI = round(LCI,2),
UCI = round(UCI,2))
PC_cal <- PC_cal %>% mutate(PC_CI = paste(PC, LCI,sep = '\n(')) %>%
mutate(PC_CI = paste(PC_CI, UCI,sep = ' to ')) %>% mutate(PC_CI = paste0(PC_CI, ')'))

head(PC_cal)

地图数据

worldData <- map_data('world')
country_asr <- PC_cal
country_asr$location <- as.character(country_asr$location)

统一国家名称

country_asr$location[country_asr$location == 'United States of America'] = 'USA'
country_asr$location[country_asr$location == 'Russian Federation'] = 'Russia'
country_asr$location[country_asr$location == 'United Kingdom'] = 'UK'
country_asr$location[country_asr$location == 'Congo'] = 'Republic of Congo'
country_asr$location[country_asr$location == "Iran (Islamic Republic of)"] = 'Iran'
country_asr$location[country_asr$location == "Democratic People's Republic of Korea"] = 'North Korea'
country_asr$location[country_asr$location == "Taiwan (Province of China)"] = 'Taiwan'
country_asr$location[country_asr$location == "Republic of Korea"] = 'South Korea'
country_asr$location[country_asr$location == "United Republic of Tanzania"] = 'Tanzania'
country_asr$location[country_asr$location == "C?te d'Ivoire"] = 'Saint Helena'
country_asr$location[country_asr$location == "Bolivia (Plurinational State of)"] = 'Bolivia'
country_asr$location[country_asr$location == "Venezuela (Bolivarian Republic of)"] = 'Venezuela'
country_asr$location[country_asr$location == "Czechia"] = 'Czech Republic'
country_asr$location[country_asr$location == "Republic of Moldova"] = 'Moldova'
country_asr$location[country_asr$location == "Viet Nam"] = 'Vietnam'
country_asr$location[country_asr$location == "Lao People's Democratic Republic"] = 'Laos'
country_asr$location[country_asr$location == "Syrian Arab Republic"] = 'Syria'
country_asr$location[country_asr$location == "North Macedonia"] = 'Macedonia'
country_asr$location[country_asr$location == "Micronesia (Federated States of)"] = 'Micronesia'
country_asr$location[country_asr$location == "Macedonia"] = 'North Macedonia'
country_asr$location[country_asr$location == "Trinidad and Tobago"] = 'Trinidad'
country_asr <- rbind(country_asr,country_asr[country_asr$location == "Trinidad",])
country_asr$location[country_asr$location == "Trinidad"] = 'Tobago'
country_asr$location[country_asr$location == "Cabo Verde"] = 'Cape Verde'
country_asr$location[country_asr$location == "United States Virgin Islands"] = 'Virgin Islands'
country_asr$location[country_asr$location == "Antigua and Barbuda"] = 'Antigu'
country_asr <- rbind(country_asr,country_asr[country_asr$location == "Antigu",])
country_asr$location[country_asr$location == "Antigu"] = 'Barbuda'
country_asr$location[country_asr$location == "Saint Kitts and Nevis"] = 'Saint Kitts'
country_asr <- rbind(country_asr,country_asr[country_asr$location == "Saint Kitts",])
country_asr$location[country_asr$location == "Saint Kitts"] = 'Nevis'
country_asr$location[country_asr$location == "Côte d'Ivoire"] = 'Ivory Coast'
country_asr$location[country_asr$location == "Saint Vincent and the Grenadines"] = 'Saint Vincent'
country_asr <- rbind(country_asr,country_asr[country_asr$location == "Saint Vincent",])
country_asr$location[country_asr$location == "Saint Vincent"] = 'Grenadines'
country_asr$location[country_asr$location == "Eswatini"] = 'Swaziland'
country_asr$location[country_asr$location == "Brunei Darussalam"] = 'Brunei'

计算PC的分位数,这里我们手动选择了分为9个区间

PC_quantile <- quantile(PC_cal$PC, probs = seq(0, 1, length.out = 10))

创建整数标签

PC_group <- vector(mode = "character", length = length(PC_quantile) - 1)

for (z in 1:(length(PC_quantile) - 1)) {
if (z == 1) {
PC_group[z] <- paste0("<", round(PC_quantile[z + 1]))
} else if (z == length(PC_quantile) - 1) {
PC_group[z] <- paste(round(PC_quantile[z]), " and above")
} else {
PC_group[z] <- paste(round(PC_quantile[z]), " to <", round(PC_quantile[z + 1]))
}
}

打印分组标签,检查是否符合要求

print(PC_group)

###将worldData和包含特定国家信息的数据(country_asr)进行全连接操作
worldData <- map_data('world')

total <- full_join(worldData,country_asr,by = c('region'='location'))
p <- ggplot()
total <- total %>% mutate(val2 = cut(PC, breaks = PC_quantile,
labels = PC_group, ### breaks需要根据自己的实际结果来调整
include.lowest = T,right = T))

切分并生成新的标签列

total <- total %>%
mutate(val2 = cut(PC, breaks = PC_quantile,
labels = PC_group, # 使用整数标签
include.lowest = TRUE, right = TRUE))

打印修改后的数据

head(total)

绘制地图 修改后的代码

p2 <- ggplot(data = total) +
geom_polygon(aes(x = long, y = lat, group = group, fill = val2), colour = "black", size = 0.2) +
scale_fill_manual(values = c("#FFECE8", "#FDCDC5", "#FBACA3", "#F98981", "#F76560",
"#F53F3F", "#CB272D", "#A1151E", "#770813")) +
theme_void() +
labs(x = "", y = "") +
theme(legend.position = "bottom left") +
guides(fill = guide_legend(title = 'DALY rate per
100 000 population')) +
theme(legend.position.inside = c(0.15, 0.25)) +
theme(legend.position = c(0.15, 0.40)) +
theme(panel.background = element_rect(fill = "white", colour = NA))

打印地图

print(p2)

dput(PC_cal)
structure(list(location_name = c("Bolivia (Plurinational State of)",
"Venezuela (Bolivarian Republic of)", "Uruguay", "Luxembourg",
"Myanmar", "Austria", "Dominican Republic", "Samoa", "Philippines",
"Peru", "Tajikistan", "Brazil", "Libya", "Democratic Republic of the Congo",
"Poland", "Montenegro", "Ukraine", "Solomon Islands", "Sri Lanka",
"Tonga", "Greenland", "South Africa", "Turkmenistan", "Mauritius",
"American Samoa", "Thailand", "Kuwait", "China", "Russian Federation",
"Romania", "Andorra", "Italy", "Timor-Leste", "Uzbekistan", "Chile",
"Puerto Rico", "Central African Republic", "Yemen", "Brunei Darussalam",
"Dominica", "Belgium", "Lesotho", "Slovakia", "Madagascar", "Ecuador",
"Tokelau", "Vanuatu", "Guinea", "Democratic People's Republic of Korea",
"Ghana", "Cyprus", "Gambia", "Canada", "Malta", "Grenada", "Viet Nam",
"Bosnia and Herzegovina", "Togo", "Saint Kitts and Nevis", "Serbia",
"Cook Islands", "Bermuda", "Lebanon", "Colombia", "Paraguay",
"Albania", "Slovenia", "Spain", "Cambodia", "Indonesia", "Republic of Korea",
"Denmark", "Congo", "Guyana", "Malawi", "San Marino", "Namibia",
"Taiwan (Province of China)", "France", "Netherlands", "Saudi Arabia",
"United States of America", "Gabon", "Saint Lucia", "Costa Rica",
"Afghanistan", "United States Virgin Islands", "Rwanda", "Marshall Islands",
"Zimbabwe", "Palestine", "Haiti", "Bulgaria", "Norway", "Algeria",
"Liberia", "Oman", "Guatemala", "Armenia", "Bahrain", "Azerbaijan",
"Kiribati", "Bangladesh", "Burkina Faso", "Equatorial Guinea",
"Morocco", "Comoros", "Belarus", "Fiji", "Japan", "Portugal",
"Croatia", "Eswatini", "Seychelles", "Guam", "Mozambique", "Finland",
"Jamaica", "Singapore", "Sudan", "Bhutan", "Qatar", "Somalia",
"Burundi", "Papua New Guinea", "Kazakhstan", "Tunisia", "El Salvador",
"Bahamas", "Antigua and Barbuda", "Georgia", "Mauritania", "Tuvalu",
"Latvia", "Czechia", "Eritrea", "Iran (Islamic Republic of)",
"Cabo Verde", "Mali", "Estonia", "Greece", "Nepal", "Australia",
"Benin", "Nicaragua", "North Macedonia", "Monaco", "Iceland",
"Honduras", "Saint Vincent and the Grenadines", "Mongolia", "Mexico",
"Guinea-Bissau", "Suriname", "Sweden", "India", "Germany", "Kyrgyzstan",
"Argentina", "Belize", "Micronesia (Federated States of)", "United Republic of Tanzania",
"Lao People's Democratic Republic", "Niger", "New Zealand", "Lithuania",
"Hungary", "Ireland", "Malaysia", "Cuba", "Republic of Moldova",
"Jordan", "Barbados", "Israel", "Pakistan", "Panama", "Switzerland",
"Zambia", "Northern Mariana Islands", "United Arab Emirates",
"Egypt", "Trinidad and Tobago", "South Sudan", "Nauru", "Niue",
"Syrian Arab Republic", "Maldives", "Iraq", "Djibouti", "Turkey",
"Chad", "Cameroon", "Botswana", "Sao Tome and Principe", "Kenya",
"Ethiopia", "Uganda", "United Kingdom", "Sierra Leone", "Palau",
"Nigeria", "Côte d'Ivoire", "Angola", "Senegal"), sex_name = c("Both",
"Both", "Both", "Both", "Both", "Both", "Both", "Both", "Both",
"Both", "Both", "Both", "Both", "Both", "Both", "Both", "Both",
"Both", "Both", "Both", "Both", "Both", "Both", "Both", "Both",
"Both", "Both", "Both", "Both", "Both", "Both", "Both", "Both",
"Both", "Both", "Both", "Both", "Both", "Both", "Both", "Both",
"Both", "Both", "Both", "Both", "Both", "Both", "Both", "Both",
"Both", "Both", "Both", "Both", "Both", "Both", "Both", "Both",
"Both", "Both", "Both", "Both", "Both", "Both", "Both", "Both",
"Both", "Both", "Both", "Both", "Both", "Both", "Both", "Both",
"Both", "Both", "Both", "Both", "Both", "Both", "Both", "Both",
"Both", "Both", "Both", "Both", "Both", "Both", "Both", "Both",
"Both", "Both", "Both", "Both", "Both", "Both", "Both", "Both",
"Both", "Both", "Both", "Both", "Both", "Both", "Both", "Both",
"Both", "Both", "Both", "Both", "Both", "Both", "Both", "Both",
"Both", "Both", "Both", "Both", "Both", "Both", "Both", "Both",
"Both", "Both", "Both", "Both", "Both", "Both", "Both", "Both",
"Both", "Both", "Both", "Both", "Both", "Both", "Both", "Both",
"Both", "Both", "Both", "Both", "Both", "Both", "Both", "Both",
"Both", "Both", "Both", "Both", "Both", "Both", "Both", "Both",
"Both", "Both", "Both", "Both", "Both", "Both", "Both", "Both",
"Both", "Both", "Both", "Both", "Both", "Both", "Both", "Both",
"Both", "Both", "Both", "Both", "Both", "Both", "Both", "Both",
"Both", "Both", "Both", "Both", "Both", "Both", "Both", "Both",
"Both", "Both", "Both", "Both", "Both", "Both", "Both", "Both",
"Both", "Both", "Both", "Both", "Both", "Both", "Both", "Both",
"Both", "Both", "Both"), age_name = c("55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years", "55+ years", "55+ years", "55+ years",
"55+ years", "55+ years"), val = c(51.28, 29.39, 48.52, 77.62,
33.43, 46.87, 25.03, 39.32, 29.19, 43.01, 54.44, 37.81, 88.42,
34.8, 38.95, 28.19, 168.05, 37.33, 75.12, 37.59, 139.45, 147.54,
65.24, 50.23, 39.48, 39.35, 53.83, 192.63, 195.19, 52.1, 33.04,
46.24, 31.92, 51.28, 43.81, 48.75, 37.47, 75.08, 97.82, 30.81,
49.02, 84.38, 44.11, 35.05, 35.06, 40.46, 35.96, 27.19, 83.11,
28, 50.23, 32.21, 135.61, 48.73, 30.53, 59.79, 24.11, 27.64,
29.26, 39.82, 35.85, 46.11, 58.36, 41.01, 25.39, 42.1, 48.4,
53.03, 32.2, 20.48, 47.26, 88.56, 39.99, 26.59, 34.51, 38.71,
100.43, 93.04, 57.6, 39.53, 37.75, 99, 42.58, 28.47, 36.03, 119.34,
60.99, 34.07, 40.7, 129.16, 36.46, 32.07, 36.65, 65.92, 73.88,
30.89, 47.51, 88.43, 60.51, 49.1, 57.74, 122.49, 61.87, 30.06,
37.44, 79.1, 32.51, 124.98, 36.76, 35.41, 38.15, 62.1, 98.17,
43.74, 45.19, 26.43, 110.27, 32.46, 40.14, 74.84, 62.35, 51.52,
32.79, 35.69, 33.46, 110.45, 74.77, 40.04, 33.12, 28.31, 61.83,
31.75, 38.94, 113.72, 39.41, 30.46, 197.39, 27.65, 28.84, 160.39,
40.16, 61.04, 75.31, 28.68, 31.16, 38.19, 38.85, 100.49, 68.03,
26.97, 56.77, 39.71, 29.91, 29.91, 35.82, 82.31, 45.22, 102.19,
47.04, 28.08, 42.5, 40.62, 31.63, 28.93, 53.03, 100.95, 32.33,
65.83, 35.25, 32.8, 90.41, 43.78, 26.89, 72.36, 72.25, 36.02,
77.35, 43.68, 44.3, 82.32, 40.51, 29.1, 36.32, 42.09, 39.86,
68.21, 34.34, 81.05, 31.02, 52.37, 27.97, 33.65, 93.31, 36.67,
37.51, 28.76, 36.34, 72.23, 31.13, 33.83, 27.41, 30.93, 37.97,
32.44), upper = c(65.49, 40.05, 64.44, 93.18, 44.7, 58.74, 34.93,
53.18, 37.99, 53.8, 73.83, 49.65, 115.51, 48.66, 46.74, 39.83,
203.09, 51.27, 90.25, 49.73, 172.24, 179.38, 87.73, 70.84, 52.28,
54.62, 71.6, 224.41, 235.45, 63.44, 44.93, 57.25, 43.24, 67.86,
58.73, 61.13, 51.3, 99.75, 122.39, 40.48, 61.77, 104.08, 55.52,
46.86, 45.95, 53.9, 49.42, 37.36, 105.84, 40.98, 62.74, 43.96,
170.74, 63.02, 41.05, 75.31, 33.46, 37.85, 39.74, 49.5, 49.45,
58.92, 76.48, 53.93, 35.25, 53.48, 59.68, 68.09, 42.82, 26.29,
62.79, 105.17, 53.17, 37.53, 47.55, 51.82, 127.82, 108.91, 70.62,
51.3, 54.03, 120.88, 56.36, 38.43, 48, 154.39, 78.02, 46.11,
53.93, 159.48, 52.64, 43.09, 46.9, 79.39, 94.93, 43.03, 63.63,
103.4, 84.83, 65.92, 79.07, 152.96, 79.73, 40.35, 50.37, 101.59,
45.21, 160.97, 50.45, 47.18, 49.51, 76.45, 124.26, 55.54, 59.85,
37.13, 131.26, 43.77, 54.14, 95.38, 82.19, 74.28, 44.41, 48.4,
46.95, 139.27, 95.34, 50.12, 45.56, 37.79, 84.02, 42.23, 53.03,
140.58, 47.92, 42.82, 231.23, 37.53, 40.69, 193.4, 51.44, 78.91,
91.12, 38.32, 42.25, 49.57, 52.22, 119.72, 86.52, 37.53, 77,
50.2, 40.16, 41.14, 44.32, 98.65, 56.52, 127.76, 64.08, 38.85,
56.28, 55.14, 41.39, 40.21, 67.88, 122.43, 42.24, 79.63, 45.56,
43.6, 115.06, 60.11, 37.17, 89.45, 89.28, 45.93, 91.98, 58.4,
60.02, 108.38, 56.86, 39.59, 48.99, 56.07, 52.23, 85.53, 45.98,
100.06, 42.65, 71.37, 38.76, 45.16, 120.51, 48.98, 47.27, 38.5,
49.61, 88.84, 42.38, 46.57, 35.79, 42.15, 52.04, 43.37), lower = c(38.92,
20.62, 33.68, 61.61, 24.06, 35.86, 16.37, 27.61, 21.91, 33.36,
38.12, 26.7, 64.37, 23.59, 31.53, 19.45, 135.87, 25.87, 60.62,
26.15, 105.71, 115.6, 46.89, 32.83, 28.1, 27.23, 39.37, 161.3,
159.39, 41.41, 21.84, 35.39, 22.33, 35.33, 29.72, 37.51, 25.22,
55.11, 74.73, 21.42, 37.75, 65.04, 33.27, 23.09, 25.83, 29.8,
24.86, 19.07, 64.2, 17.43, 39.11, 22.97, 101.65, 35.65, 21.29,
46.1, 16.28, 19.27, 20.68, 30.32, 24.07, 35.03, 43.91, 30.2,
16.49, 32.02, 39.09, 39.28, 22.93, 15.66, 33.66, 73.94, 26.98,
17.45, 23.19, 26.24, 78.3, 78.25, 45.61, 28.57, 24.02, 77.35,
29.34, 19.2, 26.76, 90.99, 46.59, 24.08, 28.54, 100.24, 23.29,
22.48, 27.61, 52.51, 53.34, 22.22, 34.43, 74.34, 40.93, 34.44,
39.25, 95.34, 45.12, 21.14, 24.38, 58.96, 22.53, 95.83, 26.09,
24.9, 27.6, 49.17, 75.56, 33.69, 32.49, 18.12, 90.49, 22.62,
28.02, 57.17, 44.74, 34.89, 22.27, 25.52, 22.79, 87.28, 57.17,
30.71, 22.08, 19.66, 43.15, 22.03, 27.88, 89.18, 31.65, 20.54,
166.37, 18.95, 19.55, 129.65, 30.48, 46.57, 61.25, 19.77, 21.57,
27.59, 26.34, 80.47, 52.12, 18.02, 39.47, 29.5, 20.8, 21.02,
28.35, 68.22, 34.53, 78.2, 32.07, 18.37, 30.77, 28.7, 23.02,
19.56, 39.11, 80.98, 23.77, 53.38, 25.46, 22.51, 69.69, 29.04,
17.91, 55.28, 56.91, 26.77, 63.68, 31.21, 30.97, 60.33, 26.93,
19.59, 24.3, 30.02, 29.12, 53.63, 24.17, 63.75, 21.41, 37.96,
18.85, 22.68, 72, 26.8, 27.84, 20.72, 24.21, 55.15, 21.7, 22.54,
20.27, 21.27, 25.34, 22.19)), class = "data.frame", row.names = c(NA,
-204L))
now:

i hope be :