Table of Contents
Making a Map
To make a map, we use the command MakeMap(). In its most basic form, you can just give it latitude and longitude values, and it will plot them on a map
MakeMap(labike$latitude, labike$longitude)
The plot will appear in the Plots tab.
If you want to change the color, you can do that with the parameter col (either by giving it the name of a color, or a hex value)
MakeMap(labike$latitude, labike$longitude, col = "red")
Using the col argument, we can also color our plotted points based on the different levels of a factor. For example, if you want to look at the different types of bike paths in our data use
levels(labike$type)
Now we'll color our plotted points based on each points type level
MakeMap(labike$latitude, labike$longitude, col = labike$type)
By default, every time you run the function MakeMap()
it will make a new map in the Plots tab. But sometimes you want to plot a couple of different things on the same map. To do that, use the parameter add in the second plot you want to add to the first. Here’s an example of how to do that
MakeMap(NotManyBikes$latitude, NotManyBikes$longitude, col = "blue") MakeMap(LotsOfBikes$latitude, LotsOfBikes$longitude, col = "red", add = TRUE)
Zooming
Again, because R is a programming language, it’s often hard to do dynamic things like zooming. But, it’s possible. To begin, make a map,
MakeMap(labike$latitude, labike$longitude)
Then, you’re going to get the opportunity to define a box that you want to zoom to. In order to do this, run the command
e = drawExtent()
You can name your zoom box anything, but I used e here because it’s short and easy to remember. In the plot viewer pane, you will notice that your mouse has changed to a ’+’ that you get to click twice to define the box you want to zoom in to. After you’ve clicked twice, you should see a red box.
Then, you need to re-run your map command, giving the additional parameter of your box. Again, I named mine e
MakeMap(labike$latitude, labike$longitude, e)
Spatial subsetting
If you want to know which points were inside the extent (the zoom box) that you defined above, you can use the function SpatialSubset()
. This function will also return the indices of the points within the spatial subset you defined.
SpatialSubset(labike$latitude, labike$longitude, e) ## [1] 1 2 3 4 6 7 8 10 11 12 13 14 15 16 17 19 21 23 25 26 27 28 29 ## [24] 31 32 33 34 35 36 38
This is the list of indices of the points within the labike
dataset that are inside the extent you defined. As always, they will print in the console if you do not name them. If you want to save these to use later, I recommend the following code,
whichsubset = SpatialSubset(labike$latitude, labike$longitude, e) spatialsubset = labike[whichsubset, ] head(spatialsubset)