1

I have three buffer radius sizes

radius<-c(1000,700,500)

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

install.packages("sp")
library(sp)
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?

1

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 
radius<-c(1000,700,500)

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



 #plot
    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.