RgoogleMaps上的图例? [英] Legend on RgoogleMaps?

查看:162
本文介绍了RgoogleMaps上的图例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用图书馆(RgoogleMaps)绘制地图上的测量位置(点)。在不同的点上有不同的设备,并且我成功地为每个设备分别获得了彩色点:$ b​​
$ b

  theplot<  -  PlotOnStaticMap(lat = sitecoord $ lat,lon = sitecoord $ lon,
cex = .7,pch = 20,
col = sitecoord $ equipmentType,
MyMap = Map,NEWMAP = FALSE)

如何将图例添加到生成的map-plot中,以查看哪些设备由蓝色点表示,等等?






更新:



@Rguy的好建议。为了别人的利益,这里是我的测试代码(不,我不在冰岛测量,只是用它作为例子):

 库(RgoogleMaps)
库(RColorBrewer)

设备<-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< ; - (平均(tblDataPoints $ lat),平均(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以前做过这个。如果您已经对 legend 函数提出了一个可重现的问题示例,我们可以讨论它。在此之前,这是一个模糊的解释。

1。使用RColorBrewer创建一个味觉。例如:


  library(RColorBrewer)
My.pal< - brewer。 pal(9,reds)


2。以某种方式为每个点指定一种颜色。在我的例子中,我有一个 WT 列和一个bin的向量,所以我通过对权重进行分箱产生每个点的颜色,并将相应的条目放在 my.pal 是该点的颜色。请注意,在此示例中,我的 binz 向量中少于9个垃圾箱,因为我的上腭只有9个红色阴影。


colorz< - My.Pal [cut(datas $ WT,labels = FALSE)]




3。在地图上绘制,传递颜色参数。
$ b


PlotOnStaticMap(MyMap,lat = datas $ LAT,lon = datas $ LON,col =
colorz)


4。最后,创建图例并将其添加到地图。


legend(bottomleft,legend = legend.txt,fill = My .bmp,
title =I am,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")

解决方案

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:

library(RColorBrewer)
My.pal <- brewer.pal(9, "reds")

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 in my.pal to be that point's color. Note that in this example, there are fewer than 9 bins in my binz vector, since my palate has only 9 shades of red.

colorz <- My.Pal[cut(datas$WT, labels = FALSE)]

3. Plot on the map, passing the colors argument.

PlotOnStaticMap(MyMap, lat = datas$LAT, lon = datas$LON, col = colorz)

4. Finally, create the legend, and add it to the map.

legend("bottomleft", legend = legend.txt, fill = My.pal, title = "I AM", bg = "white")

Hope you get it all figured out!

这篇关于RgoogleMaps上的图例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆