user3170629
user3170629

Reputation: 499

inserting border around a line plot

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

Upvotes: 3

Views: 131

Answers (2)

plannapus
plannapus

Reputation: 18759

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,])

Upvotes: 2

Luca Braglia
Luca Braglia

Reputation: 3243

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

Upvotes: 1

Related Questions