Help with polygons

32 views
Skip to first unread message

Joe

unread,
May 14, 2010, 12:29:58 PM5/14/10
to ggplot2
Hi All,

My goal is to create a Voronoi Tessellation which is bounded by the
borders of the United States and Canada, then export the polygons to a
dataframe for plotting in ggplot2. After much futzing around with
spatstat and sp, I've given up on figuring out how to do this the
right way. I got this far:

cities <- structure(list(CitState = c("SiouxFalls SD", "Rockford IL",
"Kenosha WI",
"Duluth MN", "SiouxCity IA", "CedarRapids IA", "NewYork NY"),
Latitude = c(43.54599, 42.265973, 42.577791, 46.779135,
42.489678,
41.976662, 40.756054), Longitude = c(-96.731291, -89.086667,
-87.822644, -92.108243, -96.404948, -91.673155, -73.986951
)), .Names = c("CitState", "Latitude", "Longitude"), row.names =
c(1L,
2L, 3L, 4L, 5L, 6L, 267L), class = "data.frame")

library(maps)
library(spatstat)
us <- data.frame(map("usa", regions = "main",plot = F)[c("x","y")])
manhattan <- data.frame(map("usa", regions = "manhattan",plot = F)
[c("x","y")])
spatstat.options(checkpolygons = FALSE)
us.win <- owin(poly = list(list(x = us$x, y = us$y),list(x = manhattan
$x,y = manhattan$y)))
us.ppp <- ppp(cities$Longitude, cities$Latitude, window = us.win)
us.d <- dirichlet(us.ppp)

Extracting polygons from the output of dirichlet() seems hopeless.

So, I'm wondering if I can fake it. I can create a voronoi
tessellation from my data with deldir(), and get that into the right
shape for plotting. I'm wondering if there is some way to plot the
inverse of the us as a polygon, plotted over the voronoi tessellation,
making it appear to be bounded by the borders of the us.

us$ID <- 1
ggplot(us, aes(x,y)) + geom_polygon(aes(group = ID))

-Joe

--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Andrie de Vries

unread,
May 14, 2010, 6:03:03 PM5/14/10
to ggplot2
Joe

Is the function unionSpatialPolygons() in the maptools package of any
use?

http://bm2.genes.nig.ac.jp/RGM2/R_current/library/maptools/man/unionSpatialPolygons.html

There is a worked example of using this function here:

http://www.nceas.ucsb.edu/files/scicomp/GISSeminar/UseCases/PolygonDissolve/PolygonDissolve.html

Andrie

Joe

unread,
May 15, 2010, 7:40:16 PM5/15/10
to ggplot2
Thanks for those links, but I ended up going in a different direction
using the gpclib library.

cities <- structure(list(CitState = c("SiouxFalls SD", "Rockford IL",
"Kenosha WI",
"Duluth MN", "SiouxCity IA", "CedarRapids IA", "NewYork NY"),
Latitude = c(43.54599, 42.265973, 42.577791, 46.779135,
42.489678,
41.976662, 40.756054), Longitude = c(-96.731291, -89.086667,
-87.822644, -92.108243, -96.404948, -91.673155, -73.986951
)), .Names = c("CitState", "Latitude", "Longitude"), row.names =
c(1L,
2L, 3L, 4L, 5L, 6L, 267L), class = "data.frame")

library(maps)
library(deldir)
library(gpclib)

us <- data.frame(map("usa", regions = "main",plot = F)[c("x","y")])
manhattan <- data.frame(map("usa", regions = "manhattan",plot = F)
[c("x","y")])
longisland <- data.frame(map("usa", regions = "long island",plot = F)
[c("x","y")])

cities.tess <- deldir(cities$Longitude, cities$Latitude)

clip.border <- function(deldir.obj, border){
require(gpclib)
bord <- as(matrix(ncol = 2), "gpc.poly")
for(i in seq(along = border)){
b <- as(border[[i]], "gpc.poly")
bord <- union(b, bord)
}

tiles <- tile.list(deldir.obj)
out <- {}
group <- 1
for(i in seq(along = tiles)){
poly <- cbind(tiles[[i]]$x, tiles[[i]]$y)
poly <- as(poly, "gpc.poly")

poly <- intersect(poly, bord)
if(length(poly@pts)>0){
for(j in seq(along=poly@pts)){
poly.df <- data.frame(x = poly@pts[[j]]$x, y =poly@pts[[j]]$y)
poly.df$ID <- i
poly.df$group <- group
group <- group + 1
out <- rbind(out, poly.df)
}
}
}
return(out)
}

tess.df <- clip.border(cities.tess, list(us, manhattan, longisland))
tess.df$CitState <- cities[tess.df$ID,]$CitState

ggplot(tess.df, aes(x,y)) + geom_polygon(aes(group = group,fill =
CitState))+coord_map()



On May 14, 6:03 pm, Andrie de Vries <apdevr...@gmail.com> wrote:
> Joe
>
> Is the function unionSpatialPolygons() in the maptools package of any
> use?
>
> http://bm2.genes.nig.ac.jp/RGM2/R_current/library/maptools/man/unionS...
>
> There is a worked example of using this function here:
>
> http://www.nceas.ucsb.edu/files/scicomp/GISSeminar/UseCases/PolygonDi...
Reply all
Reply to author
Forward
0 new messages