连续比例作为彩色编码地图(地图包)的图例

时间:2017-08-17 16:29:53

标签: r maps geocoding

我使用地图包来为地图的代码区域着色:

Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Private Declare Function apiGetComputerName Lib "kernel32" Alias _
"GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long

我想在地图中包含一个图例,它解释了颜色的含义。由于颜色是绿色的阴影,我希望有一个从浅绿色到深绿色的连续刻度。我无法弄清楚如何使用这个包。

谢谢!

1 个答案:

答案 0 :(得分: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)
相关问题