RgoogleMaps 上的图例?
我使用库(RgoogleMaps)在地图(点)上绘制测量位置。不同的点上有不同的设备,并且我成功地为每个设备获得了单独的彩色点:
theplot <- PlotOnStaticMap(lat=sitecoord$lat, lon=sitecoord$lon,
cex=.7, pch=20,
col=sitecoord$equipmentType,
MyMap=Map, NEWMAP=FALSE)
如何在生成的地图图中添加图例以查看哪些设备由蓝色点代表,哪些由红色点代表,等等?
更新:
使用@Rguy 的非常好的建议。我设法将图例引入。为了其他人的利益,这是我的测试代码(不,我不是在冰岛测量,只是将其用作示例):
library(RgoogleMaps)
library(RColorBrewer)
Equipment <- c("AA","AA","BB","CC")
lat <- c(63.90,66.20,64.80,64.50)
lon <- c(-22.40,-14.20,-18.60,-15.00)
tblDataPoints <- data.frame(Equipment,lat,lon)
My.Pal <- brewer.pal(3, "Reds")
tblDataPoints$colorz <- My.Pal[tblDataPoints$Equipment]
plot.new()
bb <- qbbox(lat=range(tblDataPoints$lat), lon=range(tblDataPoints$lon))
m <- c(mean(tblDataPoints$lat), mean(tblDataPoints$lon))
zoom <- min(MaxZoom(latrange=bb$latR,lonrange=bb$lonR))
Map <- GetMap.bbox(bb$lonR, bb$latR, zoom=zoom, maptype="roadmap", NEWMAP=TRUE)
tmp <- PlotOnStaticMap(lat=lat, lon=lon, cex=.7, pch=20, col=tblDataPoints$colorz, MyMap=Map, NEWMAP=FALSE)
tblLgd <- unique(tblDataPoints[,c("Equipment","colorz")])
row.names(tblLgd) <- NULL
legend("topright", legend = tblLgd$Equipment, fill = tblLgd$colorz, bg = "white")
I use library(RgoogleMaps) to plot measurement-positions on a map (points). There is different equipment on different points, and I successfully get separately colored points per equipment:
theplot <- PlotOnStaticMap(lat=sitecoord$lat, lon=sitecoord$lon,
cex=.7, pch=20,
col=sitecoord$equipmentType,
MyMap=Map, NEWMAP=FALSE)
How can I add a legend to the resulting map-plot to see which equipment is represented by blue points, which by red, and so on?
Update:
Using the very good recommendations of @Rguy. I managed to get the legend in. For the benefit of others, here is my test-code (no, I'm not measuring in Iceland, just used it as example):
library(RgoogleMaps)
library(RColorBrewer)
Equipment <- c("AA","AA","BB","CC")
lat <- c(63.90,66.20,64.80,64.50)
lon <- c(-22.40,-14.20,-18.60,-15.00)
tblDataPoints <- data.frame(Equipment,lat,lon)
My.Pal <- brewer.pal(3, "Reds")
tblDataPoints$colorz <- My.Pal[tblDataPoints$Equipment]
plot.new()
bb <- qbbox(lat=range(tblDataPoints$lat), lon=range(tblDataPoints$lon))
m <- c(mean(tblDataPoints$lat), mean(tblDataPoints$lon))
zoom <- min(MaxZoom(latrange=bb$latR,lonrange=bb$lonR))
Map <- GetMap.bbox(bb$lonR, bb$latR, zoom=zoom, maptype="roadmap", NEWMAP=TRUE)
tmp <- PlotOnStaticMap(lat=lat, lon=lon, cex=.7, pch=20, col=tblDataPoints$colorz, MyMap=Map, NEWMAP=FALSE)
tblLgd <- unique(tblDataPoints[,c("Equipment","colorz")])
row.names(tblLgd) <- NULL
legend("topright", legend = tblLgd$Equipment, fill = tblLgd$colorz, bg = "white")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我以前做过这个。如果您使用
legend
函数制作了一个可重现的问题示例,我们可以讨论它。在那之前,这里有一个模糊的解释。1. 使用 RColorBrewer 创建口味。例如:
2. 以某种方式为每个点分配一种颜色。就我而言,我有一个
WT
列和一个 bin 向量,因此我通过对权重进行分箱来生成每个点的颜色,并在my.pal
为该点的颜色。请注意,在此示例中,我的binz
向量中的 bin 数量少于 9 个,因为我的味觉只有 9 种红色阴影。3. 在地图上绘制,传递颜色参数。
4. 最后,创建图例,并将其添加到地图中。
希望您能弄清楚一切!
I've done this before. If you had made a reproducible example of the problem you're having with the
legend
function, we could discuss it. Until then, here is a vague explanation.1. Create a palate using RColorBrewer. For example:
2. Assign each of your points a color, in some way. In my case, I had a
WT
column and a vector of bins, and so I generated the colors per point by binning the weights, and taking the cooresponding entry inmy.pal
to be that point's color. Note that in this example, there are fewer than 9 bins in mybinz
vector, since my palate has only 9 shades of red.3. Plot on the map, passing the colors argument.
4. Finally, create the legend, and add it to the map.
Hope you get it all figured out!