view raw Rmd
This tutorial is the third part in a series of three:
After the presentation of the basic map concepts, and the flexibleapproach in layer implemented in ggplot2
, this part illustrates how toachieve complex layouts, for instance with map insets, or several mapscombined. Depending on the visual information that needs to bedisplayed, maps and their corresponding data might need to be arrangedto create easy to read graphical representations. This tutorial willprovide different approaches to arranges maps in the plot, in order tomake the information portrayed more aesthetically appealing, and mostimportantly, convey the information better.
Many R packages are available from CRAN,the Comprehensive R Archive Network, which is the primary repository ofR packages. The full list of packages necessary for this series oftutorials can be installed with:
1 |
|
We start by loading the basic packages necessary for all maps, i.e.ggplot2
and sf
. We also suggest to use the classic dark-on-lighttheme for ggplot2
(theme_bw
), which is more appropriate for maps:
1 |
|
The package rworldmap
provides a map of countries of the entire world;a map with higher resolution is available in the package rworldxtra
.We use the function getMap
to extract the world map (the resolutioncan be set to "low"
, if preferred):
1 |
|
The world map is available as a SpatialPolygonsDataFrame
from thepackage sp
; we thus convert it to a simple feature using st_as_sf
from package sf
:
1 |
|
There are 2 solutions to combine sub-maps:
-
using Grobs (graphic objects, allow plots only in plot region, basedon coordinates), which directly use
ggplot2
-
using
ggdraw
(allows plots anywhere, including outer margins,based on relative position) from packagecowplot
Example illustrating the difference between the two, and their use:
1 |
|
1 |
|
Graphs from ggplot2
can be saved, like any other R object. They canthen be reused later in other functions.
Using grobs, and annotation_custom
:
1 |
|
Using ggdraw
(note: used to build on top of initial plot; could beleft empty to arrange subplots on a grid; plots are “filled” with theirplots, unless the plot itself has a constrained ratio, like a map):
1 |
|
Having a way show in a visualization, a specific area can be veryuseful. Many scientists usually create maps for each specific areaindividually. This is fine, but there are simpler ways to display whatis needed for a report, or publication.
This exmaple is using two maps side by side, including the legend of thefirst one. It illustrates how to use a custom grid, which can be made alot more complex with different elements.
First, simplify REGION
for the legend:
1 |
|
Prepare the subplots, #1 world map:
1 |
|
And #2 Gulf map :
1 |
|
The command ggplotGrob
signals to ggplot
to take each created map,and how to arrange each map. The argument coord_equal
can specify thelength, ylim
, and width, xlim
, for the entire plotting area. Whereas in annotation_custom
, each maps’ xmin
, xmax
, ymin
, and ymax
can be specified to allow for complete customization.
1 |
|
Below is the final map, using the same methodology as the exmaple plotabove. Using ggplot
to arrange maps, allows for easy and quickplotting in one function of R code.
1 |
|
In the second approach, using cowplot::plot_grid
to arrange ggplot
figures, is quite versatile. Any ggplot
figure can be arranged justlike the figure above. There are many commands that allow for the map tohave different placements, such as nrow=1
means that the figure willonly occupy one row and multiple columns, and ncol=1
means the figurewill be plotted on one column and multiple rows. The commandrel_widths
establishes the width of each map, meaning that the firstmap gworld
will have a relative width of 2.3
, and the map ggulf
has the relative width of 1
.
1 |
|
Some other commands can adjust the position of the figures such asadding align=v
to align vertically, and align=h
to alignhoriztonally.
Note also the existence of get_legend
(cowplot
), and that the legendcan be used as any object.
This map can be save using,ggsave
:
1 |
|
For map insets directly on the background map, both solutions are viable(and one might prefer one or the other depending on relative or absolutecoordinates).
Map example using map of the 50 states of the US, including Alaska andHawaii (note: not to scale for the latter), using reference projectionsfor US maps. First map (continental states) use a 10/6 figure:
1 |
|
Alaska map (note: datum = NA
removes graticules and coordinates):
1 |
|
Hawaii map:
1 |
|
Using ggdraw
from cowplot
(tricky to define exact positions; notethe use of the ratios of the inset, combined with the ratio of theplot):
1 |
|
This plot can be saved using ggsave
:
1 |
|
The same kind of plot can be created using grobs, with ggplotGrob
,(note the use of xdiff/ydiff and arbitrary ratios):
1 |
|
This plot can be saved using ggsave
:
1 |
|
The print
command can also be used place multiple maps in one plottingarea.
To specify where each plot is displayed with the print
function, theargument viewport
needs to include the maximum width and height ofeach map, and the minimum x and y coordinates of where the maps arelocated in the plotting area. The argument just
will make a positionon how the secondary maps will be displayed. All maps are defaulted thesame size, until the sizes are adjusted with width
and height
.
1 |
|
Theprint
function uses the previous specifications that were listed ineach plots’ respective viewport
, with vp=
.
1 |
|
To bring about a more lively map arrangement, arrows can be used tobring the viewers eyes to specific areas in the plot. The next examplewill create a map with zoomed in areas, pointed to by arrows.
Firstly, we will create our main map, and then our zoomed in areas.
Site coordinates, same as Tutorial #1:
1 |
|
Mainlaind map of Florida, #1:
1 |
|
A map for site A is created by layering the map and points we createdearlier. ggplot
layers geom_sf
objects and plot them spatially.
1 |
|
A map for site B:
1 |
|
Coordinates of the two arrows will need to be specified before plotting.The argumemnts x1
, and x2
will plot the arrow line from a specificstarting x-axis location,x1
, and ending in a specific x-axis,x2
. Thesame applies for y1
and y2
, with the y-axis respectively:
1 |
|
Final map using (ggplot
only). The argument geom_segment
, will bethe coordinates created in the previous script, to plot line segmentsending with an arrow using arrow=arrow()
:
1 |
|
This plot can be saved using ggsave
:
1 |
|
ggdraw
could also be used for a similar result, with the argumentdraw_plot
:
1 |
|
Related