如何在绘图中添加长度/距离刻度

时间:2014-07-15 13:01:45

标签: r plot

我使用plot()函数创建了一个散点图。这个图表代表某个地区的多个地区,但我想了解这些地区的规模。

这是一个大型数据集,我也使用points()来帮助将所有信息包含在一个图中。请参阅随附的代码。

plot(c.aus1.1FG$x.position,c.aus1.1FG$y.position,ylim=c(80,130),xlim=c(140,240),xaxt='n',yaxt= 'n',pch=18,col="yellow",xlab="", ylab="",cex=0.75)
points(c.aus1.2FG$x.position,c.aus1.2FG$y.position,pch=18, col="yellow",cex=0.75)
points(c.aus1.3FG$x.position,c.aus1.3FG$y.position,pch=18, col="yellow", cex=0.75)
points(c.aus2.1FG$x.position,c.aus2.1FG$y.position,pch=18, col="blue", cex=0.75)
points(c.aus2.2FG$x.position,c.aus2.2FG$y.position,pch=18, col="blue", cex=0.75)
points(c.aus3.1FG$x.position,c.aus3.1FG$y.position,pch=18, col="dark red", cex=0.75)
points(c.aus3.2FG$x.position,c.aus3.2FG$y.position,pch=18, col="dark red", cex=0.75)
points(c.aus3.3FG$x.position,c.aus3.3FG$y.position,pch=18, col="dark red", cex=0.75)
points(c.aus4.1FG$x.position,c.aus4.1FG$y.position,pch=18, col="orange", cex=0.75)
points(c.aus4.2FG$x.position,c.aus4.2FG$y.position,pch=18, col="orange", cex=0.75)
points(c.aus4.3FG$x.position,c.aus4.3FG$y.position,pch=18, col="orange", cex=0.75)
points(c.aus4.4FG$x.position,c.aus4.4FG$y.position,pch=18, col="orange", cex=0.75)
points(c.aus5.1FG$x.position,c.aus5.1FG$y.position,pch=18, col="dark green", cex=0.75)
points(c.aus5.2FG$x.position,c.aus5.2FG$y.position,pch=18, col="dark green", cex=0.75)
points(c.aus5.3FG$x.position,c.aus5.3FG$y.position,pch=18, col="dark green", cex=0.75)
points(c.aus6.1FG$x.position,c.aus6.1FG$y.position,pch=18, col="purple", cex=0.75)
points(c.aus6.2FG$x.position,c.aus6.2FG$y.position,pch=18, col="purple", cex=0.75)
points(c.aus6.3FG$x.position,c.aus6.3FG$y.position,pch=18, col="purple", cex=0.75)
points(c.aus6.4FG$x.position,c.aus6.4FG$y.position,pch=18, col="purple", cex=0.75)
points(c.aus7.1FG$x.position,c.aus7.1FG$y.position,pch=18, cex=0.75)
points(c.aus7.2FG$x.position,c.aus7.2FG$y.position,pch=18, cex=0.75)
points(c.aus7.3FG$x.position,c.aus7.3FG$y.position,pch=18, cex=0.75)
points(c.aus8.1FG$x.position,c.aus8.1FG$y.position,pch=18,col="red", cex=0.75)
points(c.aus8.2FG$x.position,c.aus8.2FG$y.position,pch=18,col="red", cex=0.75)
points(c.aus8.3FG$x.position,c.aus8.3FG$y.position,pch=18,col="red", cex=0.75)
points(c.aus8.4FG$x.position,c.aus8.4FG$y.position,pch=18,col="red", cex=0.75)
points(c.aus9.1FG$x.position,c.aus9.1FG$y.position,pch=18, col="grey", cex=0.75)
points(c.aus9.2FG$x.position,c.aus9.2FG$y.position,pch=18, col="grey", cex=0.75)
points(c.aus9.3FG$x.position,c.aus9.3FG$y.position,pch=18, col="grey", cex=0.75)

这提供了以下图表。如何在右上角/右下角的情节中添加距离刻度? enter image description here

2 个答案:

答案 0 :(得分:0)

sp包的解决方案,可能吗?

library(sp)

# dumb data
data<-data.frame(x=runif(10),y=runif(10),col=c(rep('a',5),rep('b',5)))
# coordinates (see also proj4string!)
coordinates(data)<-~x+y

summary(data)

# set scale and text
scale = list("SpatialPolygonsRescale", layout.scale.bar(), 
             offset = c(0.2,0.03), scale = 0.2, fill=c("transparent","black"))

start = list("sp.text", c(0.2,0.01), "0")
end= list("sp.text", c(0.4,0.01), "0.2")

# plot result with scales, key, 
map<-spplot(data,scales = list(draw = TRUE),
       key.space=list(x=0.1,y=0.93,corner=c(0,1)),
       sp.layout=list(scale,start,end),
       main='simple map'
       )

print(map)

另见:sp graphic examples

答案 1 :(得分:0)

在绘制空间数据时,您也可以使用coordinates包中的scalebarraster

library(raster)

# generate x and y coordinate and grouping variable P
set.seed(1)
x = runif(min = 0, max = 100, n = 20)
y = runif(min = 200, max = 400, n = 20)
P = rep(1:4, each = 5, len = 20)    # first 4 only.

# generate a data frame
my_points = data.frame(x,y,P)

# define x and y as coordinates, creates a SpatialPointsDataFrame
coordinates(my_points)<-~x+y

# plot the point data
plot(my_points, col=my_points$P, pch=3, axes=T)

# add scale bar
scalebar(d = 50, xy = c(100,350), type = 'bar', divs = 2, below = 'm')

如果您不想使用raster,可以手动创建一个栏,可能是这样的:

# or manual, without raster package
plot(x,y, col=P, pch=3)

# add a line for scale, covering distance between x0 and x1
segments(x0 = 80, x1 = 100, y0 = 350, y1 = 350, lwd=3, lend=2)

# alternative with an arrow
arrows(x0 = 80, x1 = 100, y0 = 350, y1 = 350, lwd=3, lend=2, code = 3, angle = 90, len=0.1)

# add text label to line
text(x = 90, y =  360, labels = '20 m')