3

I am plotting the territories of several different fish via the plot function. How do you insert a border around the plot to highlight this territory? The function I am currently using is:

  plot(c.tri1.1FG$x.position,c.tri1.1FG$y.position,ylim=c(80,120),xlim=c(110,180),xlab="X position", ylab="Y position", main=expression(paste("Mapped territories of different ", italic("C. trifascialis"), " individuals at the Far Gardens coral reef site")))
points(c.tri1.2FG$x.position,c.tri1.2FG$y.position)
points(c.tri1.3FG$x.position,c.tri1.3FG$y.position)
points(c.tri1.4FG$x.position,c.tri1.4FG$y.position)
points(c.tri1.4FG$x.position,c.tri1.5FG$y.position)

enter image description here

Any help would be appreciated

user3170629
  • 489
  • 3
  • 12
  • 21
  • I think this might answer your question: http://stackoverflow.com/questions/12603006/placing-a-boundary-around-a-series-of-points-in-ggplot2 – Abhilash Jun 01 '14 at 11:40

2 Answers2

2

If I understand correctly i think you're looking for a convex hull chull:

x <- rnorm(100,0,.2)
y <- runif(100,0,2)
pts <- cbind(x,y)
ch <- chull(pts) #Convex hull
plot(pts, asp=1)

enter image description here

polygon(pts[ch,])

enter image description here

Edit: how to adapt this solution to what seems to be your data

plot(c.tri1.1FG$x.position,c.tri1.1FG$y.position,ylim=c(80,120),xlim=c(110,180),xlab="X position", ylab="Y position", main=expression(paste("Mapped territories of different ", italic("C. trifascialis"), " individuals at the Far Gardens coral reef site")))
points(c.tri1.2FG$x.position,c.tri1.2FG$y.position)
points(c.tri1.3FG$x.position,c.tri1.3FG$y.position)
points(c.tri1.4FG$x.position,c.tri1.4FG$y.position)
points(c.tri1.5FG$x.position,c.tri1.5FG$y.position)

x <- c(c.tri1.1FG$x.position,c.tri1.2FG$x.position,c.tri1.3FG$x.position, c.tri1.4FG$x.position, c.tri1.5FG$x.position)
y <- c(c.tri1.1FG$y.position,c.tri1.2FG$y.position,c.tri1.3FG$y.position, c.tri1.4FG$y.position, c.tri1.5FG$y.position)
pts <- cbind(x, y)
ch <- chull(pts)
polygon(pts[ch,])
plannapus
  • 18,529
  • 4
  • 72
  • 94
  • That is brilliant. Thank you. Is it possible to use this function on a series of data that are using the points(), with the external part of this points being the 'hull'? plot(1.1FG$x,1.1$y,xlab="X position", ylab="Y position",ylim=c(80,130),xlim=c(70,250)) points(1.2FG$x,1.2$y) points(1.3FG$y,1.3$y) – user3170629 Jun 07 '14 at 09:44
  • You might want to show what your data look like (by pasting the result of `dput` on your dataset in your question for instance) if you want a more tailored answer. – plannapus Jun 07 '14 at 09:50
  • I have updated the graph to highlight the addition of the points () function. Again any help would be greatly appreciated – user3170629 Jun 07 '14 at 15:24
1

Does polygon do what you need? eg here i'm plotting two squares (but is just an example) with transparent filling:

vecAx <- c(0,0.5,0.5,0)
vecAy <- c(0,0,0.5,0.5)
vecBx <- vecAx + 0.25
vecBy <- vecAy + 0.25

col2hex <- function(my.col, alpha = 0.3) {
    rgb(t(col2rgb(my.col)), maxColorValue = 255, alpha = alpha*255)
}

colA <- col2hex("red", alpha = 0.4)
colB <- col2hex("blue", alpha = 0.4)
## other in colors()                                                            

plot(NA, NA, xlim=c(0,1), ylim=c(0,1))
polygon(vecAx, vecAy, col = colA)
polygon(vecBx, vecBy, col = colB)

Choose your colors wisely (eg not red and blue) and/or decrease alpha.

HTH

Community
  • 1
  • 1
Luca Braglia
  • 3,133
  • 1
  • 16
  • 21
  • Good idea, but the polygon doesn't work too well on this data. The paths of the territories cross over and so it results in a bit of a mess. I need more of an external boundary over the plot – user3170629 Jun 01 '14 at 16:12