簡體   English   中英

帶有 scale_fill_manual 的 map 的 geom_polygon 不維持秩序

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

我一直在嘗試為埃塞俄比亞繪制 map,這是我在 R 腳本中的代碼。 我正在使用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()

在此處輸入圖像描述 我正在努力的是貧困率地區的顏色應該是有序的。 如果仔細觀察 map,您會發現 4-10 區間的貧困率呈現深紅色而不是淺黃色。

如何解決這種無序的顏色映射? 感謝您的幫助。

您必須將basepovband設置為一個因素並對其級別進行排序:

eth_map_data$basepovband <- 
  factor(eth_map_data$basepovband, levels = c("4-10", "10-15", "15-20", "20-25", "25-30"))

這是一個例子:

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')

在此處輸入圖像描述

讓我把我修改后的代碼工作得相當好。 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