如何在R中添加比例尺?

时间:2014-05-21 13:39:53

标签: r plot scale

让我们说我想要一个情节并丢失R中的方框。但是我仍然需要一个比例尺,以便人们可以理解缩放。我没有找到解决方案。

plot(1,1, type="n", xlim=c(0,5), ylim=c(0,5))

当我使用光栅包中的比例尺功能时,缩放不正确:

require(raster)
scalebar(1)

添加的比例尺太短,无法在x轴上表示1。 我试图找到别的东西,但大多数比例尺功能都与地图有关。

编辑: 所以我想要的是这样的:

plot(1,1, type="n", xlim=c(0,5), ylim=c(0,5)
             , yaxt="n",
             xaxt="n", frame.plot=F, ann=F
    # adding a blank plot without the axes
     )

#adding some simple function
x=c(1:5)
y=x*x
lines(x=x, y=y)

#defining where the scale bar should appear
lines(x=c(4,5), y=c(5,5))

#placing the text right under the line
text(x=4.5, y=5, pos=1, label="1 km")

有没有更简单的方法来做这样的事情?

2 个答案:

答案 0 :(得分:3)

可能有一个功能可以满足您的需求,但您也可以创建自己的功能,希望能够提供足够的功能。请参阅下面的一种可能性。您当然可以调整功能设置以获得所需的定位。特别是,我已将yadj作为参数包含在函数中,默认值为1.5。如果比例尺标签未在比例线下正确定位,则可以更改此设置。

如果x轴的范围大于下面使用的值,则需要调整刻度线的x坐标,使其跨越10,100等x单位,视情况而定是。如果你想得到想象,你可以让函数本身根据图的x范围确定要跨越多少个x单位,然后在单位标签中使用该跨度的大小。

# Function to add a scalebar to a base-graphics plot
myScalebar = function(units_label, yadj=1.5) {

  # Get plot coordinates
  pc = par("usr") 

  # Position scale line between last two major x-axis tick marks
  # and 1/10th of the total y-range above the lower y-axis coordinate
  lines(c(floor(pc[2]-1),floor(pc[2])),     
        rep(pc[3] + 0.1*(pc[4] - pc[3]), 2))

  # Place the units label at the midpoint of and just below the scale line
  text(x=mean(c(floor(pc[2]-1), floor(pc[2]))), 
       y=pc[3] + 0.1*(pc[4] - pc[3]),
       label=units_label, adj=c(0.5, yadj))
}

# Now redo your plot
# Start with blank plot
plot(1,1, type="n", xlim=c(0,5), ylim=c(0,5), 
     yaxt="n", xaxt="n", frame.plot=F, ann=F)

# Add a simple function
x=c(1:5)
y=x*x
lines(x=x, y=y)

# Add scalebar
myScalebar("1 km")

enter image description here

答案 1 :(得分:1)

我通常使用这种功能,可以在绘图中提供很大的灵活性。我已经扩展了变量名称以帮助调试。请注意:此设计适用于仅转换为utms的栅格(不要使用地理投影)。

ScaleBar <- function(reference_raster_utm, round_to_nearest_km, width_percent, y_percent_from_bottom, x_percent_from_left, y_text_percent_from_bottom, ...) {
    # Round by max to nearest... e.g. 5 km 
    mround <- function(x,base){ 
        base*round(x/base) 
    }   
    # scale bar size adjustment to avoid decimals
        scale_size <- ((xmax(reference_raster_utm)-xmin(reference_raster_utm))*width_percent)/1000
        scale_size_adj <- mround(scale_size, round_to_nearest_km)
        scale_size_adj_plot <- (scale_size_adj*1000)/2
    # Horizontal percent position (x) for scale bar
        x_position <- ((xmax(reference_raster_utm)-xmin(reference_raster_utm))*x_percent_from_left)+xmin(reference_raster_utm)
    # Vertical percent position y for scale bar
        y_position <- ((ymax(reference_raster_utm)-ymin(reference_raster_utm))*y_percent_from_bottom)+ymin(reference_raster_utm)
        y_position_text <- ((ymax(reference_raster_utm)-ymin(reference_raster_utm))*y_text_percent_from_bottom)+ymin(reference_raster_utm)
    # Draw line on plot
        library(sp)
        x_ends <- c((x_position-scale_size_adj_plot), (x_position+scale_size_adj_plot))
        y_ends <- c((y_position), (y_position))
        scale_bar_line <- SpatialLines(list(Lines(Line(cbind(x_ends, y_ends)), ID="length")))
        projection(scale_bar_line) <- projection(reference_raster_utm)
        plot(scale_bar_line, add=TRUE, ...)
        text(x_position, y_position_text, paste0(scale_size_adj, "km"))
}

<强>参数

  • reference_raster_utm:来自。
  • 的源范围/投影的个人光栅文件之一
  • round_to_nearest_km:舍入到最近的公里,例如最大2km,5km等。
  • width_percent:比例尺应覆盖的绘图宽度的百分比(例如,大50%小10%)。
  • y_percent_from_bottom:从底部开始的垂直位置。底部为0%,顶部为100%,中间为50%。
  • x_percent_from_left:左起水平位置。左边0%,右边100%,中间50%。
  • y_text_percent_from_bottom:与y_percent_from_bottom相同,但适用于文字。

示例

plot(my_raster)

ScaleBar(reference_raster_utm=my_raster, round_to_nearest_km=5, width_percent=0.25, y_percent_from_bottom=0.10, x_percent_from_left=0.50, y_text_percent_from_bottom=0.07, lwd=2)

enter image description here