I have three buffer radius sizes


that I have placed around an array of points (two example points are given here in the file SourcePlace).

What I'd like to do is visualise the three buffer sizes, in different colours, on the same plot. I attach the file with the point information, then

PlaceCoord <- SpatialPointsDataFrame(data.frame(UTM.Easting,UTM.Northing),data=data.frame(PlaceID,Name,lngYear,IDgroup),proj4string=CRS("+init=epsg:32717"))
ptbufN <- list()
for (i in 1:3) {
  ptbufN[i] <- gBuffer(PlaceCoord, width = radius[i], quadsegs = 10)
plot(ptbufN[[1]], density=NA, col=1)
plot(ptbufN[[2]], density=NA, col=2)
plot(ptbufN[[3]], density=NA, col=3)
points(PlaceCoord, pch = 20, col = 'red')

What I can't seem to fix is that my polygons are drawn on new plots each time. Maybe there's code to add polygons to existing plots somewhere in ggplot2?


I am sure more elegant ways to perform this task, but I have come to a solution. This requires sp, ggplot2, and rgeos.

As before, I attached the file with the point information.

 PlaceCoord <- SpatialPointsDataFrame(data.frame(UTM.Easting,UTM.Northing),data=data.frame(PlaceID,Name,lngYear,IDgroup),proj4string=CRS("+init=epsg:32717"))

#Establish the three radii of interest 

#Create each of the buffers
ptbuf <- list()
for (i in 1:3) {
  ptbuf[i] <- gBuffer(PlaceCoord, width = radius[i], quadsegs = 10)

    ggplot() +
  geom_polygon(data = all_buffers[[1]], aes(x=long, y=lat, group = group), colour="black", fill="black", alpha=0.45)  + # adds the first plot. colour is for the outline, fill is obvious and alpha sets the transparency (1 = opaque) of the polygon
  geom_polygon(data = all_buffers[[2]], aes(x=long, y=lat, group = group), colour="red", fill="red", alpha=0.45) + # adds the second plot
  geom_polygon(data = all_buffers[[3]], aes(x=long, y=lat, group = group), colour="green", fill="green", alpha=0.45) + # adds the third plo
  geom_point(data=SourcePlace,aes(x = UTM.Easting, y = UTM.Northing))+
  coord_equal() +
  theme(legend.position = "none", title = element_blank(),
        axis.text = element_blank())

The group parameter under aes helped me eliminate unwanted lines across my polygons, as discussed here. The output plot, with my entire point array included, looks like this:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.