3

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")
hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
Chris
  • 2,256
  • 1
  • 19
  • 41
  • Can you use the "legend" function for this? See help(legend). – Spacedman Oct 15 '11 at 15:55
  • Yeah, that's probably the direction to go, but there is something in the logic of how PlotOnStaticMap chooses the colors that I can't figure out (I'm not so experienced in this, it might be trivial). – Chris Oct 15 '11 at 16:46
  • If you pre-assign the colors to the points and maintain a record of them, you will be able to match the colors with what they correspond to. Allowing PlotOnStaticMap to decide which colors to assign each point, based on some factor, leaves the information on what color was assigned to where somewhere in the function. – Rguy Oct 17 '11 at 19:37

1 Answers1

1

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!

Rguy
  • 1,622
  • 1
  • 15
  • 20