简体   繁体   English

如何在R中添加比例尺?

[英]How to add a scale bar in R?

Lets say I want to have a plot and lose the box in R. But still I would need a scale bar so one can understand the scaling. 可以说我想绘制一个图并丢失R中的框。但是我仍然需要一个比例尺,以便人们可以理解比例。 I didn't find a solution. 我没有找到解决方案。

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

When I use the scalebar function from the raster package, the scaling is not right: 当我使用光栅包中的比例尺功能时,缩放比例不正确:

require(raster)
scalebar(1)

The added scalebar is too short to represent 1 in the x axis. 添加的比例尺太短,无法在x轴上表示1。 I tried to find something else, but most scalebar functions are related to maps. 我试图找到其他东西,但是大多数比例尺功能都与地图有关。

edit: So what I want is something like this: 编辑:所以我想要的是这样的:

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")

Is there an easier way to do something like this? 有没有更简单的方法可以执行这样的操作?

There might be a function that does what you want, but you can also create your own function that will hopefully serve well enough. 可能有一个功能可以满足您的需求,但是您也可以创建自己的功能,希望该功能可以很好地发挥作用。 See below for one possibility. 请参阅下面的一种可能性。 You can of course adjust the function settings to get the positioning you want. 当然,您可以调整功能设置以获得所需的位置。 In particular, I've included yadj as an argument to the function, with a default value of 1.5. 特别是,我将yadj作为函数的参数包括在内,默认值为1.5。 You can change this if the scalebar label isn't positioned properly under the scale line. 如果比例尺标签未正确放置在比例线下方,则可以更改此选项。

If the x-axis spans a larger range than the values used below, you'll want to adjust the x-coordinates of the scale line so that it spans 10, 100, etc. x-units, as the case may be. 如果x轴的跨度大于下面使用的值,则需要调整比例线的x坐标,以使其跨10、100等x单位(视情况而定)。 If you want to get fancy, you can have the function itself determine how many x-units to span, based on the x-range of the plot and then use the magnitude of that span in the units label. 如果想花哨的话,可以让函数本身根据绘图的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")

在此处输入图片说明

I usually use this sort of function that allows for lots of flexibility across plots. 我通常使用这种功能,可以在各种情节之间提供很大的灵活性。 I have expanded the variables names to help with debugging. 我已经扩展了变量名称以帮助调试。 Please note: this is designed to work with raster converted to utms only (don't use geographic projections). 请注意:此功能仅适用于转换为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"))
}

Arguments : 参数

  • reference_raster_utm: One of your personal raster files to source extent/projection from. reference_raster_utm:您的个人栅格文件之一,可从中获取范围/投影。
  • round_to_nearest_km: round to nearest kilometre eg max out on 2km, 5km ect. round_to_nearest_km:四舍五入到最近的公里,例如最大2km,5km等。
  • width_percent: percent of plot width that the scale bar should cover (eg big 50% small 10%). width_percent:比例尺应覆盖的绘图宽度的百分比(例如,大50%小10%)。
  • y_percent_from_bottom: vertical position from bottom. y_percent_from_bottom:从底部开始的垂直位置。 0% at bottom, 100% at top, 50% in the middle. 底部为0%,顶部为100%,中间为50%。
  • x_percent_from_left: horizontal position from left. x_percent_from_left:从左侧开始的水平位置。 0% at left, 100% at right, 50% in the middle. 左侧为0%,右侧为100%,中间为50%。
  • y_text_percent_from_bottom: same as y_percent_from_bottom but for text. y_text_percent_from_bottom:与y_percent_from_bottom相同,但用于文本。

Example : 范例

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)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM