【问题标题】:Using GGPlot to plot spatial data and color based on a column value in R使用 GGPlot 根据 R 中的列值绘制空间数据和颜色
【发布时间】:2020-04-27 21:30:09
【问题描述】:

我正在从一个 shapefile 中读取空间数据,该文件包含一个省内 100 个地区的位置。 在另一个数据框中,我有每个地区的人口数据。

我正在尝试使用 ggplot 创建省份地图(来自 shapefile)并根据人口规模为每个地区着色。这里的问题是我不知道如何填写 由 shapefile 获取的地图,其颜色基于另一个数据框中包含的地区对应的人口值。

请注意,我理解下面的代码不起作用,但它说明了我直观地试图完成的事情

districts_from_shapefile <- readOGR("districts.shp")
districts_with_pop <- read.csv(file = "districts_populations.csv")

ggplot() +  
        geom_polygon(data=districts_from_shapefile, aes(x=long,y=lat,group=group)) +
        geom_polygon(data=districts_with_pop, aes(fill=districts_with_pop$population))

谢谢!

【问题讨论】:

标签: r ggplot2 data-science shapefile sf


【解决方案1】:

确保您的 csv 具有与 shapefile 相同的行数和相同的区域,并且顺序相同。将 csv 中的人口列绑定到 shapefile 属性。然后你只需使用 geom_polygon 一次,fill=population 是一个额外的 aes(除了 x, y, group)。

如果行不匹配,您可以使用 dplyr::join 来获取人口列。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-04
    • 2014-03-24
    • 1970-01-01
    相关资源
    最近更新 更多