2017-08-17 60 views
0

我使用貼圖包到地圖的顏色代碼區域:連續大規模像傳說中的顏色編碼圖(地圖包)

palette = colorRampPalette(brewer.pal(n=9, name='Greens'))(nrow(b))  
palette = palette[rank(b)] 
france2<- map(database="france", fill = TRUE, col=palette) 

我想包括在地圖中的傳奇人物,這說明了什麼顏色的意思。由於顏色是綠色的陰影,我想要有一個從淺綠色到深綠色的連續比例。我無法弄清楚如何用這個軟件包做到這一點。

謝謝!

+0

您能否提供更多的數據?爲了分配填充顏色,我們需要知道比例尺基於哪個數據屬性。 – kostr

+0

另外 - 這裏有一個很好的資源來繪製R包: http://eriqande.github.io/rep-res-web/lectures/making-maps-with-R.html – kostr

+0

謝謝你的鏈接。有沒有辦法做到這一點,沒有ggplot,只是與地圖?繪製的數據只是100分制的分數。每個地區(N = 95)都會得到一個分數,最常見的是從20到60。 – Max

回答

0

下面是使用ggplot2dplyr的解決方案。我不知道你的實際數據看起來像是否能夠持續擴展,所以我在代碼中即興創作。

library(ggplot2) 
library(dplyr) 

#loading map data 
france <- map_data("france") 

#creating dummy data for the continuous values that weren't provided in example above 
dummy_data <- france %>% distinct(region) 

#adding sample values 
dummy_data <- dummy_data %>% mutate(Score = sample(x = c(1:100), 
                size = nrow(dummy_data), 
                replace = TRUE)) 

#merging sample values with the map's dataframe 
france <- france %>% left_join(dummy_data) 

#plotting the map 
plot <- ggplot() + geom_map(data = france, 
        map = france, 
        aes(x = long, y = lat,map_id = region, 
         group = group, 
         fill = Score), 
        color = "black") + 
    scale_fill_gradient(low='lightgreen',high='darkgreen')+ 
    theme(plot.subtitle = element_text(vjust = 1), 
     plot.caption = element_text(vjust = 1), 
     axis.line = element_line(colour = NA), 
     axis.ticks = element_line(colour = NA), 
     axis.text = element_text(size = 2, colour = NA), 
     panel.background = element_rect(fill = NA), 
     plot.background = element_rect(colour = NA)) +labs(x = NULL, y = NULL) 

print(plot)