【问题标题】:Multiple legends in same spplot同一 spplot 中的多个图例
【发布时间】:2015-03-05 10:38:52
【问题描述】:

我想绘制一个有两个变量的地图。一个是多边形,另一个是点。

多边形 - 省份边界(name1、name2、name3、name4),

点 - 类(a、b、c、d)

因为我必须将它们绘制在一起,所以我需要两个图例。下面是我得到多边形特征图例的代码。请建议如何为点特征添加图例。

  #importing shp files
 states = readOGR ( dsn = "C:/.../shp", layer = "states")
 river = readOGR ( dsn = "C:/.../shp", layer = "Province")
 stations = readOGR ( dsn = "C:/.../shp", layer = "ganga_stations_prj")

r <-list("sp.lines", river, pch = 10, cex= 2, col = "blue" ) # line feature



map = function(x, ...)
{

 scale = list("SpatialPolygonsRescale", layout.scale.bar(), 
 offset = c(800000, 2480000), scale = 300000, fill=c("transparent","black"))
 text1 = list("sp.text", c(800000,2520000), "0")
 text2 = list("sp.text", c(1100000, 2520000), "200 km")
 arrow = list("SpatialPolygonsRescale", layout.north.arrow(), 
 offset = c(1800000,3300000), scale = 100000)

spplot(states, "NAME_1",  sp.layout=list(scale,text1,text2, arrow, r, x),main = "Human Use Classification")

}



stations@data$Colour[stations@data$Class=="a"]="yellow"
stations@data$Colour[stations@data$Class=="b"]="green"
stations@data$Colour[stations@data$Class=="c"]="blue"
stations@data$Colour[stations@data$Class=="d"]="red"

p <-list("sp.points", stations,pch=18, cex=1.5, col=stations@data$Colour)
map(p)

我得到的地图:

如您所见,只有 PROVINCE(多边形)的图例。我怎样才能将它添加到点特征呢?

【问题讨论】:

    标签: r plot legend sp


    【解决方案1】:

    我不熟悉spplot,但通常你可以使用图例:

    legend("bottomright", legend=c("a","b","c","d"), pch=18, col=c("yellow","green","blue","red"))
    

    【讨论】:

    • legend() 是否与 spplot() 一起使用。它给出了错误:plot.new 还没有被调用
    • legend() 用于基本图形; spplot() 使用基于gridlattice-package
    • @Rbeginner 尝试从“plot.new()”开始,然后绘制地图,然后调用 legend 命令。 (我经常使用 ggplot,也是基于网格的,它可以工作)。
    【解决方案2】:

    尝试grid 包中的legend.grid() 函数。它对我有用,谢谢你提出这个。

    【讨论】:

      猜你喜欢
      • 2023-01-12
      • 2015-06-03
      • 2014-10-06
      • 1970-01-01
      • 2019-12-23
      • 1970-01-01
      • 2016-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多