The following code works well for drawing polygons
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()
Instead if I change the last two lines of the code to fill these polygons based on some value vector it does not work:
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 is created with data from the following link:
data source: https://www.google.com/maps/d/viewer?mid=1mf9TQY3bocarO4SaIXAtco_C1LE&hl=en_US
I then extracted coordinates from the kml file... which is then into a list of Polygons so that I could draw it using some plotting function...
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)
}