R spplot() 如何更改比例尺的单位类型

R spplot() how to change the unit type of the scale bar

我用spplot()(package sp)画了一张基本图

比例尺图例在 0 到 1 之间的小数位,但实际上我希望它显示百分比。

如果我对简单图形使用 Plot() 函数,我可以通过手动添加它们来更改比例。 在我可以手动定义单位类型的 spplot 中是否有等效的方法来执行此操作?

示例:

library(sp)
library(lattice)
trellis.par.set(sp.theme()) # sets bpy.colors() ramp
data(meuse)
coordinates(meuse) <- ~x+y
spplot(meuse, "ffreq")

该图显示 1,2,3 作为图例。如果假设我想将其显示为 $1、$2、$3 怎么办?

sp::spplot 的帮助页面说这是使用格函数完成的,并进一步指出可以以与 xyplot 中相同的方式将参数传递给 scale 参数。然而,这不太可能影响图例,而是会更改 x 轴和 y 轴的注释。当我 运行 帮助页面中的第一个示例时,我得到一个从 0 到 500 的 scale.bar,只有这两个值作为标签。查看使用的代码可以推断出 'scale.bar' 是使用传递给 sp.layout 的参数构造的,在本例中为 list(l2,l3,l4,l5)。经过几次死胡同的尝试后,我想出了这个关于如何使用 lattice simpleKey 函数来交付图例修改并制作比例尺的建议,如第一个示例所示:

library(sp); library(lattice)
l2 = list("SpatialPolygonsRescale", layout.north.arrow(), offset = c(181300,329800), 
    scale = 400)
l3 = list("SpatialPolygonsRescale", layout.scale.bar(), offset = c(180500,329800), 
    scale = 500, fill=c("transparent","black"))
l4 = list("sp.text", c(180500,329900), "0")
l5 = list("sp.text", c(181000,329900), "500 m")

spplot(meuse, c("ffreq"), sp.layout=list(l2,l3,l4,l5) 
              ,key=simpleKey( text=paste0("$", 1:3) ) )

注意:由于 spplot 是 S4 函数,因此您可以通过以下方式获取代码:

showMethods(spplot, class="SpatialPointsDataFrame", includeDefs=TRUE)

legendEntries 参数 (showcased here) 允许您向 spplot() 图例提供文本标签的字符向量。像这样使用它:

spplot(meuse, "ffreq", legendEntries = paste0("$", levels(meuse$ffreq)))

如果绘制一个因子,只需更改因子的标签可能会更容易:

levels(meuse$ffreq) = c("a", "b", "c")
spplot(meuse, "ffreq")

legendEntries 已记录,但不是 spplot 的命名参数,因此在参数列表中缺失。 SpatialPointsDataFrame 对象的 spplot 方法有一个 colorkey 参数来绘制连续的颜色键;即将发布的 sp 1.0-18 将允许它的列表参数来控制 lattice::levelplot

中的相同参数