I'm trying to make a map of Kenya with a fill that constitutes of values '0-3' and '5'. However, ggplot is plotting the counties with a '0' fill as blank and shifting the values on the scale, because there is no value '4'. How can I fix this? In the way that the zeros are plotted and the scale stays correct. Below is the code I used to make the plot. And also the code that I used to make the dataframe for the plot.
Let me know if you want me to add something and thanks in advance!
These are the first 10 rows of my dataframe:
structure(list(long = c(35.78634226, 35.79574167, 35.8581196,
35.86048445, 35.88332712, 35.88631784, 35.90456735, 35.9095112,
35.91591989, 35.93612253), lat = c(1.65376487, 1.63856712, 1.54176536,
1.53814079, 1.50313011, 1.49812523, 1.46913353, 1.46174827, 1.4513723,
1.41957298), order = 1:10, hole = c(FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE), piece = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("1", "2", "3",
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15",
"16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26",
"27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37",
"38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48",
"49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59",
"60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70",
"71", "72"), class = "factor"), id = c("Baringo", "Baringo",
"Baringo", "Baringo", "Baringo", "Baringo", "Baringo", "Baringo",
"Baringo", "Baringo"), group = structure(c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L), .Label = c("Baringo.1", "Bomet.1", "Bungoma.1",
"Busia.1", "Elgeyo-Marakwet.1", "Embu.1", "Garissa.1", "Homa Bay.1",
"Isiolo.1", "Kajiado.1", "Kakamega.1", "Kericho.1", "Kiambu.1",
"Kilifi.1", "Kilifi.2", "Kilifi.3", "Kilifi.4", "Kilifi.5", "Kilifi.6",
"Kilifi.7", "Kilifi.8", "Kilifi.9", "Kilifi.10", "Kilifi.11",
"Kilifi.12", "Kilifi.13", "Kilifi.14", "Kilifi.15", "Kilifi.16",
"Kilifi.17", "Kilifi.18", "Kilifi.19", "Kilifi.20", "Kilifi.21",
"Kilifi.22", "Kilifi.23", "Kilifi.24", "Kilifi.25", "Kilifi.26",
"Kilifi.27", "Kirinyaga.1", "Kisii.1", "Kisumu.1", "Kitui.1",
"Kwale.1", "Kwale.2", "Kwale.3", "Kwale.4", "Laikipia.1", "Lamu.1",
"Lamu.2", "Lamu.3", "Lamu.4", "Lamu.5", "Lamu.6", "Lamu.7", "Lamu.8",
"Lamu.9", "Lamu.10", "Lamu.11", "Lamu.12", "Lamu.13", "Lamu.14",
"Lamu.15", "Lamu.16", "Lamu.17", "Lamu.18", "Lamu.19", "Lamu.20",
"Lamu.21", "Lamu.22", "Lamu.23", "Lamu.24", "Lamu.25", "Lamu.26",
"Lamu.27", "Lamu.28", "Lamu.29", "Lamu.30", "Lamu.31", "Lamu.32",
"Lamu.33", "Lamu.34", "Lamu.35", "Lamu.36", "Lamu.37", "Lamu.38",
"Lamu.39", "Lamu.40", "Lamu.41", "Lamu.42", "Lamu.43", "Lamu.44",
"Lamu.45", "Lamu.46", "Lamu.47", "Lamu.48", "Lamu.49", "Lamu.50",
"Lamu.51", "Lamu.52", "Lamu.53", "Lamu.54", "Lamu.55", "Lamu.56",
"Lamu.57", "Lamu.58", "Lamu.59", "Lamu.60", "Lamu.61", "Lamu.62",
"Lamu.63", "Lamu.64", "Lamu.65", "Lamu.66", "Lamu.67", "Lamu.68",
"Lamu.69", "Lamu.70", "Lamu.71", "Lamu.72", "Machakos.1", "Makueni.1",
"Mandera.1", "Marsabit.1", "Meru.1", "Migori.1", "Mombasa.1",
"Mombasa.2", "Mombasa.3", "Mombasa.4", "Mombasa.5", "Murang'a.1",
"Nairobi.1", "Nakuru.1", "Nandi.1", "Narok.1", "Nyamira.1", "Nyandarua.1",
"Nyeri.1", "Samburu.1", "Siaya.1", "Taita Taveta.1", "Tana River.1",
"Tharaka Nithi.1", "Trans Nzoia.1", "Turkana.1", "Uasin Gishu.1",
"Vihiga.1", "Wajir.1", "West Pokot.1"), class = "factor"), SUB_crisis_trans_count = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L)), row.names = c(NA, 10L), class = "data.frame")
SUB_count_cristrans_KE <- long.SUB_dfCSKE %>% mutate(crisis = ifelse(`IPC class` %in% 3:5, 1, 0)) %>%
arrange(`County`, `Period of measurement Kenya`) %>%
group_by(`County`) %>%
summarize(SUB_crisis_trans_count = sum(diff(crisis) > 0))
### Join the dataframe with the counts with the County polygon
SUB_cristrans_KE_plot <- SUB_KE_fortified %>% left_join(SUB_count_cristrans_KE, by = c("id" = "County"))
### Make labels for the map
minVal_cristrans_KE <- min(SUB_cristrans_KE_plot$SUB_crisis_trans_count, na.rm = T)
maxVal_cristrans_KE <- max(SUB_cristrans_KE_plot$SUB_crisis_trans_count, na.rm = T)
### Plot the crisis transitions per county of Kenya
SUB_Kenya_cristrans_plot <- ggplot() +
geom_polygon(data = SUB_cristrans_KE_plot, aes(fill = factor(SUB_crisis_trans_count),
x = long,
y = lat,
group = id)) +
geom_path(data = SUB_cristrans_KE_plot, aes(x = long,
y = lat,
group = group),
color = "black", size = 0.1) +
scale_fill_brewer(name="No of crisis\ntransitions",
labels=c("0","1", "2", "3", "4", "5"),
palette=("Blues")) +
coord_equal() +
#theme_map() + ## switching this one off means you have to delete the longitude & latitude
theme(legend.position = "bottom") +
labs(x = "Longitude",
y = "Latitude",
title = "Crisis transitions per li \n zone of Kenya",
caption = "Shapefile: SUB_KE, Fill: No Crisis transitions; Data: FEWSNET, 2019")
plot(SUB_Kenya_cristrans_plot)