I have been trying to draw a map for Ethiopia and here is my code in R script. I am using ggplot
and for color filling using scale_fill_manual
.
ggplot(eth_map_data, aes(x=long, y=lat, group=group))+
geom_polygon(aes(fill=basepovband), size=.2)+
coord_equal()+
scale_fill_manual("Poverty rate", values = brewer.pal(5, "YlOrRd"))+
geom_path(color='black')+
theme(axis.text=element_blank(),axis.ticks=element_blank(),
legend.position = "bottom",
panel.spacing = unit(0,"null"),
plot.margin = rep(unit(0,"null"),4),
axis.ticks.length = unit(0,"null"),
axis.text.margin = unit(0,"null")
)+
with(centroids_1, annotate(geom='text', x=long, y=lat, label=ADM1, size=3, color='black'))+
theme_map()
What I am struggling is that color of the regions for poverty rate should be in order. The map if you look closely you will see that poverty rate at 4-10 interval received deep red color instead of light yellow.
How to resolve this unordered color mapping? Your kind help is appreciated.
You have to set basepovband
as a factor and order its levels:
eth_map_data$basepovband <-
factor(eth_map_data$basepovband, levels = c("4-10", "10-15", "15-20", "20-25", "25-30"))
Here is an example:
library(dplyr)
library(ggplot2)
library(RColorBrewer)
# we'll use this dataset
mi_counties <- map_data("county", "michigan") %>%
select(lon = long, lat, group, id = subregion)
mi_counties$basepovband <-
sample(c("4-10", "10-15", "15-20"), size = nrow(mi_counties), replace = TRUE)
mi_counties$basepovband <-
factor(mi_counties$basepovband, levels = c("4-10", "10-15", "15-20"))
# ggplot
ggplot(mi_counties, aes(x = lon, y = lat, group = group)) +
geom_polygon(aes(fill = basepovband), size =.2) +
coord_equal() +
scale_fill_manual("Poverty rate", values = brewer.pal(3, "YlOrRd")) +
geom_path(color = 'black')
Let me put my revised codes that work reasonably well. d1, d2, d3 and d4 are subset of original data (eth_map_data)
geom_polygon(data = d1, aes(x = long, y = lat, group = group, fill = basepovband)) +
geom_polygon(data = d2, aes(x = long, y = lat, group = group, fill = basepovband)) +
geom_polygon(data = d3, aes(x = long, y = lat, group = group, fill = basepovband))+
geom_polygon(data = d4, aes(x = long, y = lat, group = group, fill = basepovband))+
scale_fill_manual('Poverty rate', values=cols.bef)+
geom_path(color='black')+
theme(axis.text=element_blank(),axis.ticks=element_blank(),
legend.position = "bottom",
panel.margin = unit(0,"null"),
plot.margin = rep(unit(0,"null"),4),
axis.ticks.length = unit(0,"null"),
axis.ticks.margin = unit(0,"null")
)+
with(centroids_1, annotate(geom='text', x=long, y=lat, label=ADM1_base, size=3, color='black'))+
theme_map()```
[1]: https://i.stack.imgur.com/fYcAZ.png
Two problems/issues still exist:
- geom_path is not working, so no boundary of regions is showing
- intervals are not in order. If you look at the map, you will see 5-10 is showing last in the legend, even though I have converted the variable into factor with proper levels and labels. I had to hard code with colors.
Any comments are welcome.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.