1

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

多边形 - 省边界(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(多边形)的图例。我怎样才能将它添加到点特征呢?

4

2 回答 2

1

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

legend("bottomright", legend=c("a","b","c","d"), pch=18, col=c("yellow","green","blue","red"))
于 2015-03-05T11:08:32.223 回答
-1

Try legend.grid() function from the package grid. It worked for me and thank you for putting this up.

于 2015-04-17T11:35:02.313 回答