RgoogleMaps 上的图例?

发布于 2024-12-10 01:46:40 字数 1365 浏览 0 评论 0原文

我使用库(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不甘平庸 2024-12-17 01:46:40

我以前做过这个。如果您使用 legend 函数制作了一个可重现的问题示例,我们可以讨论它。在那之前,这里有一个模糊的解释。

1. 使用 RColorBrewer 创建口味。例如:

库(RColorBrewer)
My.pal <- Brewer.pal(9, "reds")

2. 以某种方式为每个点分配一种颜色。就我而言,我有一个 WT 列和一个 bin 向量,因此我通过对权重进行分箱来生成每个点的颜色,并在 my.pal 为该点的颜色。请注意,在此示例中,我的 binz 向量中的 bin 数量少于 9 个,因为我的味觉只有 9 种红色阴影。

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

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

PlotOnStaticMap(MyMap, lat = 数据$LAT, lon = 数据$LON, col =
颜色z)

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

legend("bottomleft", legend = legend.txt, fill = My.pal,
title =“我是”,bg =“白色”)

希望您能弄清楚一切!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文