简体   繁体   English

带有 scale_fill_manual 的 map 的 geom_polygon 不维持秩序

[英]geom_polygon for map with scale_fill_manual is not maintaining order

I have been trying to draw a map for Ethiopia and here is my code in R script.我一直在尝试为埃塞俄比亚绘制 map,这是我在 R 脚本中的代码。 I am using ggplot and for color filling using scale_fill_manual .我正在使用ggplot并使用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.如果仔细观察 map,您会发现 4-10 区间的贫困率呈现深红色而不是浅黄色。

How to resolve this unordered color mapping?如何解决这种无序的颜色映射? Your kind help is appreciated.感谢您的帮助。

You have to set basepovband as a factor and order its levels:您必须将basepovband设置为一个因素并对其级别进行排序:

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) d1、d2、d3 和 d4 是原始数据的子集 (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. 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM