0

I am trying to plot occurrences in different months using the mapview () function

x=data.frame(read.csv(file.choose()))   ## choose the original CSV file downloaded from ebird
jan=x[which(x$Month == "1"),] ## separate out data by each month, to be plotted later as desired
feb=x[which(x$Month == "2"),]
mar=x[which(x$Month == "3"),]
apr=x[which(x$Month == "4"),]
may=x[which(x$Month == "5"),]
jun=x[which(x$Month == "6"),]
jul=x[which(x$Month == "7"),]
aug=x[which(x$Month == "8"),]
sep=x[which(x$Month == "9"),]
oct=x[which(x$Month == "10"),]
nov=x[which(x$Month == "11"),]
dec=x[which(x$Month == "12"),]

mapview(jan, xcol = "Longitude", ycol = "Latitude", crs = 4269, grid = FALSE, col.regions="green") # plot the data for january 


I would similarly want to plot the data for all 12 months, but I can't figure how to keep the x-y bounds for each of those 12 months similar. It assumes a default bound, which varies each month depending on the max extent of the data in each month, which doesn't give me maps that I can compare.

Is there a option to add a header label for the map as well?

Any help would be greatly appreciated.

1 Answer 1

1

You might be able to do this by converting your points to sf objects and then mapview lets you use hide=TRUE when you add map layers. Then you can use all your points as a hidden map layer, and that sets the extent, and add the layer you want to see.

Here's a reproducible example. First make a data frame of points in (0,1) and (1,2) unit squares:

p1 = data.frame(x=runif(10,0,1),y=runif(10,0,1),ID=1)
p2 = data.frame(x=runif(10,1,2),y=runif(10,1,2),ID=2)
p = rbind(p1, p2)

Now convert to a spatial data frame (ideally you'd also set the coordinate system here but everything defaults to lat-long so nm):

library(sf)
ps = st_as_sf(p, coords=c("x","y"))

Now make a map of ID=1:

mapview(ps[ps$ID==1,]) + mapview(ps, hide=TRUE)

And one of ID=2:

mapview(ps[ps$ID==2,]) + mapview(ps, hide=TRUE)

and those maps should have the same extent. This should be applicable to your data.

Alternatively you might want to switch to a different mapping package, like tmap, which may have a bit more flexibility for web mapping. I've only considered mapview for quick views of spatial data, and not for constructing maps that might have use as complex visualisation needing lots of fine control.

1
  • thank you, this is very neat May 25, 2023 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.