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

时间:2014-03-31 10:43:13

标签: r ggplot2 dataframe shapefile

我一直在努力尝试使用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的新人,请道歉。

1 个答案:

答案 0 :(得分:2)

这里有很多错误。 read.csv行... sep =&#34;,&#34;,not&#34; &#34 ;. 要确保你正在合并正确的列。使用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)