簡體   English   中英

將數據框附加到shapefile並將其繪制

[英]Attaching a dataframe to a shapefile and plotting it

我一直在努力將數據框與shapefile結合在一起並繪制結果。 我正在嘗試遵循@jlhoward 對此問題的答案中建議的方法。

我有一個按郵政編碼分類全國疫苗接種率數據集 我正在嘗試將其與澳大利亞統計局的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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM