Get startedGet started for free

Useful get_map() and ggmap() options

1. Useful get_map() and ggmap() options

Now you've seen ggmap in action, let's talk more about what it is doing and how you can alter its output.

2. Changing the map image

Here's the code you used to grab a map of Corvallis. You probably noticed when you ran get_map() it printed some messages to the console. These messages give you a idea of what get_map() is doing. It's querying the Google Maps API and asking for a terrain map.

3. Other map image sources

To see the available options for the maptype and source arguments, you can take a look at the help for get_map(). In the usage section you'll find all the possibilities. The terrain, terrain-background, satellite, roadmap and hybrid maptypes come from Google. Stamen maps also has a terrain type in addition to watercolor, terrain-labels, toner and toner variations. The source osm refers to Open Street Maps and only has one maptype so it doesn't need to be specified. As an example, we could instead ask for the toner-2010 type from Stamen maps.

4. Specifying default data and aesthetics

The ggmap() function also has some useful arguments. In particular the base_layer argument, which is best explained by example. Recall our original plot of sales using ggplot2. We specified the data, and the x and y position aesthetics in the ggplot2 call.

5. Specifying default data and aesthetics

To translate this plot to use ggmap, we moved the data and aesthetics to the geom_point call.

6. Specifying default data and aesthetics

This allowed us to swap out the ggplot2 call with a ggmap call and have our points sit on top of our map. The downside of moving the data and aesthetics is any subsequent layers also need to specify the data and aesthetics and some things, like faceting, just won't work.

7. Specifying default data and aesthetics

The base_layer argument is designed to solve this problem. We can specify our original ggplot2 call as the base_layer argument, and ggmap will use this to specify the default data and aesthetics for all subsequent layers. We get the benefit of having a map background, and the advantage of our usual ggplot2 specification, which for example, means we can now facet on a variable in our sales data without any problems.

8. Changing the way the map is plotted

The extent and maprange arguments can also be useful when the map and data don't quite share the same range. Extent controls how much of the plotting area the map takes up, and maprange whether the map or the data should control the plotting limits.

9. Let's practice!

You'll see an example of these in action in the final section of the chapter.

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.