【问题标题】:Continuous scale as legend for color coded map (maps package)连续比例尺作为彩色编码地图的图例(地图包)
【发布时间】:2018-01-26 04:32:42
【问题描述】:

我正在使用 maps 包对地图的区域进行颜色编码:

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

我想在地图中加入一个图例,说明颜色的含义。由于颜色是绿色,我想要一个从浅绿色到深绿色的连续刻度。我不知道如何用这个包做到这一点。

谢谢!

【问题讨论】:

  • 您能否提供更多数据?为了分配填充颜色,我们需要知道比例基于数据的哪个属性。
  • 另外,这里有一个很好的 R 包映射资源:eriqande.github.io/rep-res-web/lectures/making-maps-with-R.html
  • 感谢您提供该链接。有没有办法在没有 ggplot 而只使用地图的情况下做到这一点?绘制的数据只是 100 分制的分数。每个区域 (N=95) 都会获得一个分数,通常从 20 到 60 分。

标签: r maps geocoding


【解决方案1】:

这是使用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)

【讨论】:

    猜你喜欢
    • 2019-01-09
    • 1970-01-01
    • 1970-01-01
    • 2016-10-14
    • 2013-06-13
    • 2013-05-17
    • 2017-12-20
    • 2013-07-16
    • 1970-01-01
    相关资源
    最近更新 更多