r - 将数据框附加到 shapefile 并绘制它

标签 r ggplot2 dataframe shapefile

我一直在努力尝试将数据框与形状文件连接起来并绘制结果。我正在尝试遵循 @jlhoward 对 this question 的回答中提出的方法。 .

我有一个national dataset of vaccination rates by post code 。我正在尝试将其与 ESRI shapefile 合并来自澳大利亚统计局的邮政编码,并根据其他问题按邮政编码绘制结果。

这是我当前尝试的地方:

library(rgdal)
library(maptools) 
library(ggplot2)
library(plyr)
setwd("~/Google Drive/R/PC_Shapes")
vac.data <- read.csv(file = "Postcode2013.csv", header=TRUE, sep=" ", na.string="NA", dec=".", strip.white=TRUE)
postcode <- readOGR("POA06aAUST_region.shp", layer="POA06aAUST_region")
postcode@data$id <- rownames(postcode@data)
postcode.df <- fortify(postcode)
postcode.df <- join(postcode.df, postcode@data, by="id")
postcode.df <- merge(postcode.df, vac.data, all=TRUE)
ggp <- ggplot(data=postcode.df, aes(x=long, y=lat, group=group)) 
ggp <- ggp + geom_polygon(aes(fill=LEVEL))         
ggp <- ggp + geom_path(color="grey", linestyle=2) 
ggp <- ggp + coord_equal() 
ggp <- ggp + scale_fill_gradient(low = "#ffffcc", high = "#ff4444", space = "Lab", na.value = "grey50", guide = "colourbar")
ggp <- ggp + labs(title="Vaccination Rates: Australia")
print(ggp)

我认为我的问题在于以下两行,我知道我需要分配 by.x= 和/或 by.y=: 但我不断收到错误,我不清楚它们源自何处。我不确定我想在这里实现什么......

postcode.df <- join(postcode.df, postcode@data, by="id")
postcode.df <- merge(postcode.df, vac.data, all=TRUE)

此时我的 shapefile 最终包含超过 5,500,000 个观察值,而 R 开始陷入困境。

还值得注意的是,ABS shapefile 中有一些邮政编码我没有数据。我不知道如何排除它们。它们可能是一个问题。在之前的尝试中,我尝试过这种方法:

library("sp","rgdal","plyr")
setwd("~/Google Drive/R/PC_Shapes")
ogrListLayers("POA06aAUST_region.shp")
postcode <- readOGR("POA06aAUST_region.shp", layer="POA06aAUST_region")
vacs <- read.csv("~/Google Drive/R/PC_Shapes/Postcode2013.csv")
PNI <- melt(vacs, id=c("Postcode","Percent.not.fully.immunised"))
postcode$POA_2006 %in% PNI$Postcode
postcode$POA_2006[which(!postcode$POA_2006 %in% PNI$Postcode)] 
levels(postcode$POA_2006[which(!postcode$POA_2006 %in% PNI$Postcode)] )

如果有人知道我在哪里跌倒,我将非常感激任何提示。我是 R 新手,如果这是一个明显的问题,我深表歉意。

最佳答案

这里有很多错误。 read.csv 行... sep=",",而不是“”。 必须确保您在正确的列上合并。使用 head(df) 查看您尝试合并的 df 的前几行,或使用 str(df) 查看有关它的一堆信息。

祝你好运。

library(rgdal)
library(maptools) 
library(ggplot2)
library(plyr)
gpclibPermit()

vac.data <- read.csv(file = "Postcode2013.csv", header=TRUE, sep=",", na.string="NA", dec=".", strip.white=TRUE)
postcode <- readOGR("POA06aAUST_region.shp", layer="POA06aAUST_region")
# took too long to fortify on whole data set
postcode <- postcode[1:50,]
postcode@data$id <- rownames(postcode@data)
pts <- fortify(postcode,region="id")
postcode.df <- merge(pts,postcode,by="id", stringsAsFactors=F)
postcode.df$id <- as.numeric(postcode.df$id)
postcode.df2 <- merge(postcode.df, vac.data, by.x="POA_2006", by.y="PC_2006")
postcode.df2 <- postcode.df2[order(postcode.df2$id,postcode.df2$order),]

ggplot() + geom_polygon(aes(x=long,y=lat, group=group, 
                            fill=Percent.not.fully.immunised),
                        data=postcode.df2)

关于r - 将数据框附加到 shapefile 并绘制它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22759512/

相关文章:

r - 在 mutate 中将参数传递给 pmap

r - 在 R data.table 中连接具有相同(非键控)列名的表

r - 轨迹树

r - R 上的 geom_vline 不适用于在 ggplot 图上添加垂直线

r - ggplot2 中密度图叠加中的警告消息

python - 列出数据框列中存在的所有数据类型

r - 如何在 rvest html_session 中发帖?

r - 如何将单独的路径映射到冲积图?

python - 如何理解 pandas resample 方法中的封闭和标签参数?

python - Python 中检查对象属性是否分配了 DataFrame 的最有效方法?