x=c(0,1.8,1.8,1,1,3,3,2.2,2.2,4,4,6,6,14,14,6,6,4,4,0,0)
y=c(0,0,-2,-2,-10,-10,-2,-2,0,0,1.8,1.8,1,1,3,3,2.2,2.2,4,4,0)
xy=data.frame(x,y)
xy.sp=SpatialPolygons(list(Polygons(list(Polygon(xy)), ID="test")))
labels=c("Hi!", "A very long text string", "N
a
r
r
o
w")
plot(xy.sp, col="khaki")
for( label in labels) {
xy=calc.labpt.strings(xy.sp, label)
text(xy[1], xy[2], label)
}
plot(xy.sp, col="khaki")
# example with multiple polygons & text labels
x1=c(0,4,4,0,0)
y1=c(0,0,4,4,0)
x2=c(1,1,3,3,1)
y2=c(-2,-10,-10,-2,-2)
x3=c(6,14,14,6,6)
y3=c(1,1,3,3,1)
xy.sp=SpatialPolygons(list(
Polygons(list(Polygon(cbind(x1,y1))), ID="test1"), # box
Polygons(list(Polygon(cbind(x3,y3))), ID="test3"), # wide
Polygons(list(Polygon(cbind(x2,y2))), ID="test2") # high
))
plot(xy.sp, col=bpy.colors(3))
text(polygonsLabel(xy.sp, labels), labels, col = c('white', 'black', 'black'))Run the code above in your browser using DataLab