![]() ![]() ![]() One advantage to sf data is immediately apparent, we can easily see the overall structure of the data: Australia is comprised of six states and some territories. This output shows some of the metadata associated with the data (discussed momentarily), and tells us that the data is essentially a tibble with 9 rows and 2 columns. To illustrate what an sf data set looks like, we import a data set depicting the borders of Australian states and territories: To introduce these functions, we rely on the ozmaps package by Michael Sumner which provides maps for Australian state boundaries, local government areas, electoral boundaries, and so on ( Sumner 2019). The sf package ( Pebesma 2018) developed by Edzer Pebesma provides an excellent toolset for working with such data, and the geom_sf() and coord_sf() functions in ggplot2 are designed to work together with the sf package. Vector data for maps are typically encoded using the “simple features” standard produced by the Open Geospatial Consortium. ![]() There are a few limitations to the approach outlined above, not least of which is the fact that the simple “longitude-latitude” data format is not typically used in real world mapping. ![]() For this reason, ggplot2 provides geom_sf() and coord_sf() to handle spatial data specified in simple features format. Chapter 15 discusses coordinate systems in ggplot2 in more general terms, but as we’ll see below, geospatial data often require a more exacting approach. In both plots we use coord_quickmap() to adjust the axes to ensure that longitude and latitude are rendered on the same scale. This is illustrated in the right panel below. To turn this scatterplot into a map, we use geom_polygon() instead, which draws each county as a distinct polygon. In this plot, each row in the data frame is plotted as a single point, producing a scatterplot that shows the corners of every county. To get a better sense of what the data contains, we can plot mi_counties using geom_point(), as shown in the left panel below. In this data set we have four variables: lat and long specify the latitude and longitude of a vertex (i.e. a corner of the polygon), id specifies the name of a region, and group provides a unique identifier for contiguous areas within a region (e.g. if a region consisted of multiple islands). Here’s a data set specifying the county boundaries for Michigan: The maps package isn’t particularly accurate or up-to-date, but it’s built into R so it’s an easy place to start. For this example we take data from the maps package using ggplot2::map_data(). Perhaps the simplest approach to drawing maps is to use geom_polygon() to draw boundaries for different regions. Finally, Section 6.5 discusses how to draw maps based on raster data. Next, Section 6.3 and Section 6.4 discuss how to work with map projections and the underlying sf data structure. We’ve structured the chapter in the following way: Section 6.1 outlines a simple way to draw maps using geom_polygon(), which is followed in Section 6.2 by a modern “simple features” (sf) approach using geom_sf(). This chapter will help you tackle both problems. Typically, the problem can be decomposed into two problems: using one data source to draw a map, and adding metadata from another information source to the map. Plotting geospatial data is a common visualisation task, and one that requires specialised tools. This chapter should be readable but is currently undergoing final polishing. You are reading the work-in-progress third edition of the ggplot2 book. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |