나는 몇 달 동안 R을 벗어 났기 때문에 어떤 결과가 있었을 수도 있습니다.
이 데이터 세트를 인터넷에서 찾았습니다. 나는 그것을 약간 다루었으므로 dput()
여기에서 그럴 것이지만 원래 https://ourworldindata.org/terrorism 에서 왔습니다 .
> dput(ter)
structure(list(region = c("Afghanistan", "Albania", "Algeria",
"Angola", "Argentina", "Australasia & Oceania", "Australia",
"Austria", "Azerbaijan", "Bahrain", "Bangladesh", "Belgium",
"Brazil", "Burkina Faso", "Burundi", "Cameroon", "Canada", "Central African Republic",
"Central America & Caribbean", "Central Asia", "Chad", "Chile",
"China", "Colombia", "Cote d'Ivoire", "Czech Republic", "Democratic Republic of the Congo",
"Djibouti", "Dominican Republic", "East Asia", "Eastern Europe",
"Ecuador", "Egypt", "Ethiopia", "Finland", "France", "Gabon",
"Georgia", "Germany", "Greece", "Honduras", "India", "Indonesia",
"Iran", "Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Jordan",
"Kenya", "Kosovo", "Kyrgyzstan", "Laos", "Latvia", "Lebanon",
"Liberia", "Libya", "Malawi", "Malaysia", "Maldives", "Mali",
"Malta", "Mexico", "Middle East & North Africa", "Mozambique",
"Myanmar", "Nepal", "Netherlands", "Niger", "Nigeria", "North America",
"Macedonia", "Norway", "Pakistan", "Palestine", "Papua New Guinea",
"Paraguay", "Peru", "Philippines", "Poland", "Russia", "Rwanda",
"Saudi Arabia", "Serbia", "Sierra Leone", "Somalia", "South Africa",
"South America", "South Asia", "South Sudan", "Southeast Asia",
"Spain", "Sri Lanka", "Sub-Saharan Africa", "Sudan", "Sweden",
"Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "Tunisia",
"Turkey", "Uganda", "Ukraine", "UK", "USA", "Venezuela", "Vietnam",
"Western Europe", "World", "Yemen", "Zambia", "Zimbabwe"), Code = c("AFG",
"ALB", "DZA", "AGO", "ARG", NA, "AUS", "AUT", "AZE", "BHR", "BGD",
"BEL", "BRA", "BFA", "BDI", "CMR", "CAN", "CAF", NA, NA, "TCD",
"CHL", "CHN", "COL", "CIV", "CZE", "COD", "DJI", "DOM", NA, NA,
"ECU", "EGY", "ETH", "FIN", "FRA", "GAB", "GEO", "DEU", "GRC",
"HND", "IND", "IDN", "IRN", "IRQ", "IRL", "ISR", "ITA", "JAM",
"JOR", "KEN", "OWID_KOS", "KGZ", "LAO", "LVA", "LBN", "LBR",
"LBY", "MWI", "MYS", "MDV", "MLI", "MLT", "MEX", NA, "MOZ", "MMR",
"NPL", "NLD", "NER", "NGA", NA, "MKD", "NOR", "PAK", "PSE", "PNG",
"PRY", "PER", "PHL", "POL", "RUS", "RWA", "SAU", "SRB", "SLE",
"SOM", "ZAF", NA, NA, "SSD", NA, "ESP", "LKA", NA, "SDN", "SWE",
"SYR", "TWN", "TJK", "TZA", "THA", "TUN", "TUR", "UGA", "UKR",
"GBR", "USA", "VEN", "VNM", NA, "OWID_WRL", "YEM", "ZMB", "ZWE"
), Year = c(2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017,
2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017,
2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017,
2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017,
2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017,
2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017,
2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017,
2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017,
2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017,
2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017,
2017, 2017, 2017, 2017, 2017, 2017, 2017), `Terrorism fatalities (GTD, 2018)` = c(6092,
0, 12, 7, 0, 4, 4, 2, 5, 6, 25, 2, 0, 53, 20, 228, 6, 601, 4,
6, 62, 0, 16, 84, 3, 0, 596, 0, 2, 16, 101, 0, 877, 67, 2, 7,
0, 0, 1, 0, 2, 465, 20, 39, 6476, 0, 3, 0, 0, 4, 126, 0, 0, 1,
0, 17, 0, 289, 0, 4, 1, 361, 1, 23, 10819, 22, 218, 4, 0, 148,
1805, 124, 0, 0, 1076, 50, 0, 4, 8, 496, 0, 61, 2, 31, 0, 0,
1912, 21, 101, 7664, 581, 811, 21, 1, 6712, 82, 5, 2026, 0, 1,
8, 72, 5, 222, 7, 40, 42, 95, 5, 0, 83, 26445, 762, 0, 0)), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -115L), spec = structure(list(
cols = list(Entity = structure(list(), class = c("collector_character",
"collector")), Code = structure(list(), class = c("collector_character",
"collector")), Year = structure(list(), class = c("collector_double",
"collector")), `Terrorism fatalities (GTD, 2018)` = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1), class = "col_spec"))
나는 항상 중요한 코드를 축소하면서 많은 것을 시도했습니다. 결국, 나는 vignette("forcats")
(내가 tidyverse
로드 한) 예제 코드를 사용했고 , 다른 데이터 셋으로 시도해 보았다.
starwars %>%
mutate(skin_color = fct_lump(skin_color, n = 5)) %>%
count(skin_color, sort = TRUE)
ter %>%
mutate(hair = fct_lump(region, n = 5)) %>%
count(hair, sort = TRUE)
gss_cat %>%
mutate(relig = fct_lump(relig, n = 5)) %>%
count(relig, sort = TRUE)
starwars
및 모두에서 예상대로 작동 gss_cat
하지만 ter
(내 데이터) 에서는 작동 하지 않습니다 .
> ter %>%
+ mutate(hair = fct_lump(region, n = 5)) %>%
+ count(hair, sort = TRUE)
# A tibble: 115 x 2
hair n
<fct> <int>
1 Afghanistan 1
2 Albania 1
3 Algeria 1
4 Angola 1
5 Argentina 1
6 Australasia & Oceania 1
7 Australia 1
8 Austria 1
9 Azerbaijan 1
10 Bahrain 1
# … with 105 more rows
왜 이런 일이 발생합니까? fct_lump()
여기서 일 하지 않는 이유는 무엇 입니까?
사망자가 5 명 미만인 지역을 '기타'카테고리로 묶으려는 것 같습니다. 이것은 기본 R에서 간단합니다.
ter$region <- as.character(ter$region)
ter$region[which(ter$`Terrorism fatalities (GTD, 2018)` < 5)] <- "Other"
ter$region <- factor(ter$region)
원하는 forcats
경우을 사용 하여 사망 수준에 따라 레벨을 조정할 수 있습니다.
ter$region <- fct_reorder(ter$region, ter$`Terrorism fatalities (GTD, 2018)`)
ggplot(ter, aes(region, `Terrorism fatalities (GTD, 2018)`)) +
geom_col() +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
또는 위의 코드를 실행했지만 치사율이 500 미만인 모든 지역을 하나로 묶으면 다음과 같은 결과가 나타납니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다