在线图周围插入边框

时间:2014-06-01 10:45:45

标签: r plot border polygon

我正在通过情节函数绘制几种不同鱼类的领地。如何在情节周围插入边框以突出显示此区域?我目前使用的功能是:

  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

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:2)

如果我理解正确,我认为你正在寻找一个凸包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

编辑:如何使此解决方案适应您的数据

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

答案 1 :(得分:1)

polygon能做你需要的吗?例如,我在这里用透明填充绘制两个正方形(但只是一个例子):

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)

明智地选择颜色(例如,不是红色和蓝色)和/或减少alpha

HTH