下面的代碼可以很好地用於繪製多邊形填充多邊形沒有工作
SP <- SpatialPolygons(spd)
centroids <- coordinates(SP)
x <- centroids[,1]
y <- centroids[,2]
SP1 <- SpatialPolygonsDataFrame(SP, data = data.frame(x = x, y = y, z = z, row.names = row.names(SP)))
ggplot(data = SP1, aes(x = long, y = lat, group = group)) + geom_path()
相反,如果我改變代碼的最後兩行基於某些值向量它不工作,以填補這些多邊形:
數據源:https://www.google.com/maps/d/viewer?mid=1mf9TQY3bocarO4SaIXAtco_C1LE&hl=en_US
penetration <- runif(n = length(SP), min = 1, max = 10)
SP1 <- SpatialPolygonsDataFrame(SP, data = data.frame(x = x, y = y, z = z, value = penetration, row.names = row.names(SP)))
ggplot(data = SP1, aes(x = long, y = lat, group = group)) + geom_polygon(aes(fill=SP1$value))
SPD與從下面的鏈接數據來創建
我再提取座標從KML文件......這是遂成多邊形的列表,以便我可以使用一些繪圖功能繪製...
tkml <- getKMLcoordinates(kmlfile="Pune all Electoral Wards.kml", ignoreAltitude=T)
len <-length(tkml)
del <- NA
for(i in 1:len)
{
if(length(tkml[[i]]) == 2)
del <- c(del, i)
}
del <- del[-1]
tkmlCleaned <- tkml[!tkml %in% tkml[del]]
size <- length(tkmlCleaned)
sr <- vector(mode = "list", length = size)
srs <- vector(mode = "list", length = size)
spd <- list()
for(j in 1:length(sr))
{
sr[[j]] <- Polygon(tkmlCleaned[[j]])
srs[[j]] <- Polygons(list(sr[[j]]), j)
spd <- c(srs[[j]], spd)
}
什麼是'spd'?你可以添加一個數據集來使其具有可重現性嗎? – steveb
因爲我不習慣發佈堆棧溢出,請不要介意格式化錯誤 – Aditya
最好使用'dput'提供數據。所以爲了得到'spd',你可以做下面的'dput(spd)' – steveb