<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/drawing-beautiful-maps-programmatically-with-r-sf-and-ggplot2-part-1-basics/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.r-spatial.org//r/2018/10/25/ggplot2-sf.html"> r-spatial</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a rel="nofollow" href="https://raw.githubusercontent.com//r-spatial/r-spatial.org/gh-pages/_rmd/2018-10-02-ggplot2-sf.Rmd" target="_blank">view raw Rmd</a></p>
<h1>Beautiful maps in a beautiful world</h1>
<p>Maps are used in a variety of fields to express data in an appealing and<br>
interpretive way. Data can be expressed into simplified patterns, and<br>
this data interpretation is generally lost if the data is only seen<br>
through a spread sheet. Maps can add vital context by incorporating many<br>
variables into an easy to read and applicable context. Maps are also<br>
very important in the information world because they can quickly allow<br>
the public to gain better insight so that they can stay informed. It’s<br>
critical to have maps be effective, which means creating maps that can<br>
be easily understood by a given audience. For instance, maps that need<br>
to be understood by children would be very different from maps intended<br>
to be shown to geographers.</p>
<p>Knowing what elements are required to enhance your data is key into<br>
making effective maps. Basic elements of a map that should be considered<br>
are polygon, points, lines, and text. Polygons, on a map, are closed<br>
shapes such as country borders. Lines are considered to be linear shapes<br>
that are not filled with any aspect, such as highways, streams, or<br>
roads. Finally, points are used to specify specific positions, such as<br>
city or landmark locations. With that in mind, one need to think about<br>
what elements are required in the map to really make an impact, and<br>
convey the information for the intended audience. Layout and formatting<br>
are the second critical aspect to enhance data visually. The arrangement<br>
of these map elements and how they will be drawn can be adjusted to make<br>
a maximum impact.</p>
<h2>A solution using R and its ecosystem of packages</h2>
<p>Current solutions for creating maps usually involves GIS software, such<br>
as ArcGIS, QGIS, eSpatial, etc., which allow to visually prepare a map,<br>
in the same approach as one would prepare a poster or a document layout.<br>
On the other hand, R, a free and open-source software development<br>
environment (IDE) that is used for computing statistical data and<br>
graphic in a programmable language, has developed advanced spatial<br>
capabilities over the years, and can be used to draw maps<br>
programmatically.</p>
<p>R is a powerful and flexible tool. R can be used from calculating data<br>
sets to creating graphs and maps with the same data set. R is also free,<br>
which makes it easily accessible to anyone. Some other advantages of<br>
using R is that it has an interactive language, data structures,<br>
graphics availability, a developed community, and the advantage of<br>
adding more functionalities through an entire ecosystem of packages. R<br>
is a scriptable language that allows the user to write out a code in<br>
which it will execute the commands specified.</p>
<p>Using R to create maps brings these benefits to mapping. Elements of a<br>
map can be added or removed with ease — R code can be tweaked to make<br>
major enhancements with a stroke of a key. It is also easy to reproduce<br>
the same maps for different data sets. It is important to be able to<br>
script the elements of a map, so that it can be re-used and interpreted<br>
by any user. In essence, comparing typical GIS software and R for<br>
drawing maps is similar to comparing word processing software (e.g.<br>
Microsoft Office or LibreOffice) and a programmatic typesetting system<br>
such as LaTeX, in that typical GIS software implement a WYSIWIG approach<br>
(“What You See Is What You Get”), while R implements a WYSIWYM approach<br>
(“What You See Is What You Mean”).</p>
<p>The package <code>ggplot2</code> implements the grammar of graphics in R, as a way<br>
to create code that make sense to the user: The grammar of graphics is a<br>
term used to breaks up graphs into semantic components, such as<br>
geometries and layers. Practically speaking, it allows (and forces!) the<br>
user to focus on graph elements at a higher level of abstraction, and<br>
how the data must be structured to achieve the expected outcome. While<br>
<code>ggplot2</code> is becoming the de facto standard for R graphs, it does not<br>
handle spatial data specifically. The current state-of-the-art of<br>
spatial objects in R relies on <code>Spatial</code> classes defined in the package<br>
<a rel="nofollow" href="https://cran.r-project.org/package=sp" target="_blank"><code>sp</code></a>, but the new package<br>
<a rel="nofollow" href="https://cran.r-project.org/package=sf" target="_blank"><code>sf</code></a> has recently implemented<br>
the “simple feature” standard, and is steadily taking over <code>sp</code>.<br>
Recently, the package <code>ggplot2</code> has allowed the use of simple features<br>
from the package <code>sf</code> as layers in a graph<sup><a rel="nofollow" href="https://www.r-spatial.org//r/2018/10/25/ggplot2-sf.html#fn:1" target="_blank">1</a></sup>. The combination of<br>
<code>ggplot2</code> and <code>sf</code> therefore enables to programmatically create maps,<br>
using the grammar of graphics, just as informative or visually appealing<br>
as traditional GIS software.</p>
<h2>Drawing beautiful maps programmatically with R, <code>sf</code> and <code>ggplot2</code></h2>
<p>This tutorial is the first part in a series of three:</p>
<ul>
<li>General concepts illustrated with the world Map (this document)</li>
<li><a rel="nofollow" href="https://www.r-spatial.org//r/2018/10/25/ggplot2-sf-2.html" target="_blank">Adding additional layers: an example with points and<br>
polygons</a></li>
<li><a rel="nofollow" href="https://www.r-spatial.org//r/2018/10/25/ggplot2-sf-3.html" target="_blank">Positioning and layout for complex maps</a></li>
</ul>
<p>In this part, we will cover the fundamentals of mapping using <code>ggplot2</code><br>
associated to <code>sf</code>, and presents the basics elements and parameters we<br>
can play with to prepare a map.</p>
<h2>Getting started</h2>
<p>Many R packages are available from <a rel="nofollow" href="https://cran.r-project.org/" target="_blank">CRAN</a>,<br>
the Comprehensive R Archive Network, which is the primary repository of<br>
R packages. The full list of packages necessary for this series of<br>
tutorials can be installed with:</p>
<div>
<div>
<pre><code>install.packages(c(&quot;cowplot&quot;, &quot;googleway&quot;, &quot;ggplot2&quot;, &quot;ggrepel&quot;, 
    &quot;ggspatial&quot;, &quot;libwgeom&quot;, &quot;sf&quot;, &quot;rworldmap&quot;, &quot;rworldxtra&quot;))
</code></pre>
</div>
</div>
<p>We start by loading the basic packages necessary for all maps, i.e.<br>
<code>ggplot2</code> and <code>sf</code>. We also suggest to use the classic dark-on-light<br>
theme for <code>ggplot2</code> (<code>theme_bw</code>), which is appropriate for maps:</p>
<div>
<div>
<pre><code>library(&quot;ggplot2&quot;)
theme_set(theme_bw())
library(&quot;sf&quot;)
</code></pre>
</div>
</div>
<p>The package <code>rworldmap</code> provides a map of countries of the entire world;<br>
a map with higher resolution is available in the package <code>rworldxtra</code>.<br>
We use the function <code>getMap</code> to extract the world map (the resolution<br>
can be set to <code>&quot;low&quot;</code>, if preferred):</p>
<div>
<div>
<pre><code>library(&quot;rworldmap&quot;)
library(&quot;rworldxtra&quot;)
world &lt;- getMap(resolution = &quot;high&quot;)
class(world)

## [1] &quot;SpatialPolygonsDataFrame&quot;
## attr(,&quot;package&quot;)
## [1] &quot;sp&quot;
</code></pre>
</div>
</div>
<p>The world map is available as a <code>SpatialPolygonsDataFrame</code> from the<br>
package <code>sp</code>; we thus convert it to a simple feature using <code>st_as_sf</code><br>
from package <code>sf</code>:</p>
<div>
<div>
<pre><code>world &lt;- st_as_sf(world)
class(world)

## [1] &quot;sf&quot;         &quot;data.frame&quot;
</code></pre>
</div>
</div>
<h1>General concepts illustrated with the world map</h1>
<h2>Data and basic plot (<code>ggplot</code> and <code>geom_sf</code>)</h2>
<p>First, let us start with creating a base map of the world using<br>
<code>ggplot2</code>. This base map will then be extended with different map<br>
elements, as well as zoomed in to an area of interest. We can check that<br>
the world map was properly retrieved and converted into an <code>sf</code> object,<br>
and plot it with <code>ggplot2</code>:</p>
<div>
<div>
<pre><code>ggplot(data = world) +
    geom_sf()
</code></pre>
</div>
</div>
<p><img src="https://i2.wp.com/www.r-spatial.org/images/figure-markdown_mmd/world-plot-1.png?w=90%25&ssl=1" data-recalc-dims="1"></p>
<p>This call nicely introduces the structure of a <code>ggplot</code> call: The first<br>
part <code>ggplot(data = world)</code> initiates the <code>ggplot</code> graph, and indicates<br>
that the main data is stored in the <code>world</code> object. The line ends up<br>
with a <code>+</code> sign, which indicates that the call is not complete yet, and<br>
each subsequent line correspond to another layer or scale. In this case,<br>
we use the <code>geom_sf</code> function, which simply adds a geometry stored in a<br>
<code>sf</code> object. By default, all geometry functions use the main data<br>
defined in <code>ggplot()</code>, but we will see later how to provide additional<br>
data.</p>
<p>Note that layers are added one at a time in a <code>ggplot</code> call, so the<br>
order of each layer is very important. All data will have to be in an<br>
<code>sf</code> format to be used by <code>ggplot2</code>; data in other formats (e.g. classes<br>
from <code>sp</code>) will be manually converted to <code>sf</code> classes if necessary.</p>
<h2>Title, subtitle, and axis labels (<code>ggtitle</code>, <code>xlab</code>, <code>ylab</code>)</h2>
<p>A title and a subtitle can be added to the map using the function<br>
<code>ggtitle</code>, passing any valid character string (e.g. with quotation<br>
marks) as arguments. Axis names are absent by default on a map, but can<br>
be changed to something more suitable (e.g. “Longitude” and “Latitude”),<br>
depending on the map:</p>
<div>
<div>
<pre><code>ggplot(data = world) +
    geom_sf() +
    xlab(&quot;Longitude&quot;) + ylab(&quot;Latitude&quot;) +
    ggtitle(&quot;World map&quot;, subtitle = paste0(&quot;(&quot;, length(unique(world$NAME)), &quot; countries)&quot;))
</code></pre>
</div>
</div>
<p><img src="https://i0.wp.com/www.r-spatial.org/images/figure-markdown_mmd/world-title-1.png?w=90%25&ssl=1" data-recalc-dims="1"></p>
<h2>Map color (<code>geom_sf</code>)</h2>
<p>In many ways, <code>sf</code> geometries are no different than regular geometries,<br>
and can be displayed with the same level of control on their attributes.<br>
Here is an example with the polygons of the countries filled with a<br>
green color (argument <code>fill</code>), using black for the outline of the<br>
countries (argument <code>color</code>):</p>
<div>
<div>
<pre><code>ggplot(data = world) + 
    geom_sf(color = &quot;black&quot;, fill = &quot;lightgreen&quot;)
</code></pre>
</div>
</div>
<p><img src="https://i2.wp.com/www.r-spatial.org/images/figure-markdown_mmd/world-colors-1.png?w=90%25&ssl=1" data-recalc-dims="1"></p>
<p>The package <code>ggplot2</code> allows the use of more complex color schemes, such<br>
as a gradient on one variable of the data. Here is another example that<br>
shows the population of each country. In this example, we use the<br>
“viridis” colorblind-friendly palette for the color gradient (with<br>
<code>option = &quot;plasma&quot;</code> for the plasma variant), using the square root of<br>
the population (which is stored in the variable <code>POP_EST</code> of the <code>world</code><br>
object):</p>
<div>
<div>
<pre><code>ggplot(data = world) +
    geom_sf(aes(fill = POP_EST)) +
    scale_fill_viridis_c(option = &quot;plasma&quot;, trans = &quot;sqrt&quot;)
</code></pre>
</div>
</div>
<p><img src="https://i2.wp.com/www.r-spatial.org/images/figure-markdown_mmd/world-colors-pop-1.png?w=90%25&ssl=1" data-recalc-dims="1"></p>
<h2>Projection and extent (<code>coord_sf</code>)</h2>
<p>The function <code>coord_sf</code> allows to deal with the coordinate system, which<br>
includes both projection and extent of the map. By default, the map will<br>
use the coordinate system of the first layer that defines one (i.e.<br>
scanned in the order provided), or if none, fall back on WGS84<br>
(latitude/longitude, the reference system used in GPS). Using the<br>
argument <code>crs</code>, it is possible to override this setting, and project on<br>
the fly to any projection. This can be achieved using any valid PROJ4<br>
string (here, the European-centric ETRS89 Lambert Azimuthal Equal-Area<br>
projection):</p>
<div>
<div>
<pre><code>ggplot(data = world) +
    geom_sf() +
    coord_sf(crs = &quot;+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs &quot;)
</code></pre>
</div>
</div>
<p><img src="https://i1.wp.com/www.r-spatial.org/images/figure-markdown_mmd/world-coord-1.png?w=65%25&ssl=1" data-recalc-dims="1"></p>
<p>Spatial Reference System Identifier (SRID) or an European Petroleum<br>
Survey Group (EPSG) code are available for the projection of interest,<br>
they can be used directly instead of the full PROJ4 string. The two<br>
following calls are equivalent for the ETRS89 Lambert Azimuthal<br>
Equal-Area projection, which is EPSG code 3035:</p>
<div>
<div>
<pre><code>ggplot(data = world) +
    geom_sf() +
    coord_sf(crs = &quot;+init=epsg:3035&quot;)

ggplot(data = world) +
    geom_sf() +
    coord_sf(crs = st_crs(3035))
</code></pre>
</div>
</div>
<p>The extent of the map can also be set in <code>coord_sf</code>, in practice<br>
allowing to “zoom” in the area of interest, provided by limits on the<br>
x-axis (<code>xlim</code>), and on the y-axis (<code>ylim</code>). Note that the limits are<br>
automatically expanded by a fraction to ensure that data and axes don’t<br>
overlap; it can also be turned off to exactly match the limits provided<br>
with <code>expand = FALSE</code>:</p>
<div>
<div>
<pre><code>ggplot(data = world) +
    geom_sf() +
    coord_sf(xlim = c(-102.15, -74.12), ylim = c(7.65, 33.97), expand = FALSE)
</code></pre>
</div>
</div>
<p><img src="https://i0.wp.com/www.r-spatial.org/images/figure-markdown_mmd/world-zoom-1.png?w=65%25&ssl=1" data-recalc-dims="1"></p>
<h2>Scale bar and North arrow (package <code>ggspatial</code>)</h2>
<p>Several packages are available to create a scale bar on a map (e.g.<br>
<code>prettymapr</code>, <code>vcd</code>, <code>ggsn</code>, or <code>legendMap</code>). We introduce here the<br>
package <code>ggspatial</code>, which provides easy-to-use functions…</p>
<p><code>scale_bar</code> that allows to add simultaneously the north symbol and a<br>
scale bar into the <code>ggplot</code> map. Five arguments need to be set manually:<br>
<code>lon</code>, <code>lat</code>, <code>distance_lon</code>, <code>distance_lat</code>, and <code>distance_legend</code>. The<br>
location of the scale bar has to be specified in longitude/latitude in<br>
the <code>lon</code> and <code>lat</code> arguments. The shaded distance inside the scale bar<br>
is controlled by the <code>distance_lon</code> argument. while its width is<br>
determined by <code>distance_lat</code>. Additionally, it is possible to change the<br>
font size for the legend of the scale bar (argument <code>legend_size</code>, which<br>
defaults to 3). The North arrow behind the “N” north symbol can also be<br>
adjusted for its length (<code>arrow_length</code>), its distance to the scale<br>
(<code>arrow_distance</code>), or the size the N north symbol itself<br>
(<code>arrow_north_size</code>, which defaults to 6). Note that all distances<br>
(<code>distance_lon</code>, <code>distance_lat</code>, <code>distance_legend</code>, <code>arrow_length</code>,<br>
<code>arrow_distance</code>) are set to <code>&quot;km&quot;</code> by default in <code>distance_unit</code>; they<br>
can also be set to nautical miles with “nm”, or miles with “mi”.</p>
<p></p>
<div>
<div>
<pre><code>library(&quot;ggspatial&quot;)
ggplot(data = world) +
    geom_sf() +
    annotation_scale(location = &quot;bl&quot;, width_hint = 0.5) +
    annotation_north_arrow(location = &quot;bl&quot;, which_north = &quot;true&quot;, 
        pad_x = unit(0.75, &quot;in&quot;), pad_y = unit(0.5, &quot;in&quot;),
        style = north_arrow_fancy_orienteering) +
    coord_sf(xlim = c(-102.15, -74.12), ylim = c(7.65, 33.97))

## Scale on map varies by more than 10%, scale bar may be inaccurate
</code></pre>
</div>
</div>
<p><img src="https://i1.wp.com/www.r-spatial.org/images/figure-markdown_mmd/world-scale-north-1.png?w=65%25&ssl=1" data-recalc-dims="1"></p>
<p>Note the warning of the inaccurate scale bar: since the map use<br>
unprojected data in longitude/latitude (WGS84) on an equidistant<br>
cylindrical projection (all meridians being parallel), length in<br>
(kilo)meters on the map directly depends mathematically on the degree of<br>
latitude. Plots of small regions or projected data will often allow for<br>
more accurate scale bars.</p>
<h2>Country names and other names (<code>geom_text</code> and <code>annotate</code>)</h2>
<p>The <code>world</code> data set already contains country names and the coordinates<br>
of the centroid of each country (among more information). We can use<br>
this information to plot country names, using <code>world</code> as a regular<br>
<code>data.frame</code> in <code>ggplot2</code>. We first check the country name information:</p>
<div>
<div>
<pre><code>head(world[, c(&quot;NAME&quot;, &quot;LON&quot;, &quot;LAT&quot;)])

## Simple feature collection with 6 features and 3 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -70.06164 ymin: -18.0314 xmax: 74.89231 ymax: 60.48075
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
##          NAME       LON       LAT                       geometry
## 1       Aruba -69.97345  12.51678 MULTIPOLYGON (((-69.87609 1...
## 2 Afghanistan  66.00845  33.83627 MULTIPOLYGON (((71.02458 38...
## 3      Angola  17.56405 -12.32934 MULTIPOLYGON (((13.98233 -5...
## 4    Anguilla -63.05667  18.22432 MULTIPOLYGON (((-63.0369 18...
## 5     Albania  20.05399  41.14258 MULTIPOLYGON (((20.06496 42...
## 6       Aland  19.94429  60.22851 MULTIPOLYGON (((19.91892 60...
</code></pre>
</div>
</div>
<p>The function <code>geom_text</code> can be used to add a layer of text to a map<br>
using geographic coordinates. The function requires the data needed to<br>
enter the country names, which is the same data as the world map. Again,<br>
we have a very flexible control to adjust the text at will on many<br>
aspects:</p>
<ul>
<li>The size (argument <code>size</code>);</li>
<li>The alignment, which is centered by default on the coordinates<br>
provided. The text can be adjusted horizontally or vertically using<br>
the arguments <code>hjust</code> and <code>vjust</code>, which can either be a number<br>
between 0 (right/bottom) and 1 (top/left) or a character (“left”,<br>
“middle”, “right”, “bottom”, “center”, “top”). The text can also be<br>
offset horizontally or vertically with the argument <code>nudge_x</code> and<br>
<code>nudge_y</code>;</li>
<li>The font of the text, for instance its color (argument <code>color</code>) or<br>
the type of font (<code>fontface</code>);</li>
<li>The overlap of labels, using the argument <code>check_overlap</code>, which<br>
removes overlapping text. Alternatively, when there is a lot of<br>
overlapping labels, the package <code>ggrepel</code> provides a<br>
<code>geom_text_repel</code> function that moves label around so that they do<br>
not overlap.</li>
</ul>
<p>Additionally, the <code>annotate</code> function can be used to add a single<br>
character string at a specific location, as demonstrated here to add the<br>
Gulf of Mexico:</p>
<div>
<div>
<pre><code>ggplot(data = world) +
    geom_sf() +
    geom_text(aes(LON, LAT, label = NAME), size = 4, hjust = &quot;left&quot;, 
        color = &quot;darkblue&quot;, fontface = &quot;bold&quot;, check_overlap = TRUE) +
    annotate(geom = &quot;text&quot;, x = -90, y = 26, label = &quot;Gulf of Mexico&quot;, 
        fontface = &quot;italic&quot;, color = &quot;grey22&quot;, size = 6) +
    coord_sf(xlim = c(-102.15, -74.12), ylim = c(7.65, 33.97), expand = FALSE)
</code></pre>
</div>
</div>
<p><img src="https://i1.wp.com/www.r-spatial.org/images/figure-markdown_mmd/world-country-1.png?w=65%25&ssl=1" data-recalc-dims="1"></p>
<h1>Final map</h1>
<p>Now to make the final touches, the theme of the map can be edited to<br>
make it more appealing. We suggested the use of <code>theme_bw</code> for a<br>
standard theme, but there are many other themes that can be selected<br>
from (see for instance <code>?ggtheme</code> in <code>ggplot2</code>, or the package<br>
<a rel="nofollow" href="https://cran.r-project.org/package=ggthemes" target="_blank"><code>ggthemes</code></a> which provide<br>
several useful themes). Moreover, specific theme elements can be tweaked<br>
to get to the final outcome:</p>
<ul>
<li>Position of the legend: Although not used in this example, the<br>
argument <code>legend.position</code> allows to automatically place the legend<br>
at a specific location (e.g. <code>&quot;topright&quot;</code>, <code>&quot;bottomleft&quot;</code>, etc.);</li>
<li>Grid lines (graticules) on the map: by using <code>panel.grid.major</code> and<br>
<code>panel.grid.minor</code>, grid lines can be adjusted. Here we set them to<br>
a gray color and dashed line type to clearly distinguish them from<br>
country borders lines;</li>
<li>Map background: the argument <code>panel.background</code> can be used to color<br>
the background, which is the ocean essentially, with a light blue;</li>
<li>Many more elements of a theme can be adjusted, which would be too<br>
long to cover here. We refer the reader to the documentation for the<br>
function <code>theme</code>.</li>
</ul>
<p></p>
<div>
<div>
<pre><code>ggplot(data = world) +
    geom_sf(fill = &quot;antiquewhite1&quot;) +
    geom_text(aes(LON, LAT, label = NAME), size = 4, hjust = &quot;left&quot;,
        color = &quot;darkblue&quot;, fontface = &quot;bold&quot;, check_overlap = TRUE) +
    annotate(geom = &quot;text&quot;, x = -90, y = 26, label = &quot;Gulf of Mexico&quot;, 
        fontface = &quot;italic&quot;, color = &quot;grey22&quot;, size = 6) +
    annotation_scale(location = &quot;bl&quot;, width_hint = 0.5) +
    annotation_north_arrow(location = &quot;bl&quot;, which_north = &quot;true&quot;, 
        pad_x = unit(0.75, &quot;in&quot;), pad_y = unit(0.5, &quot;in&quot;),
        style = north_arrow_fancy_orienteering) +
    coord_sf(xlim = c(-102.15, -74.12), ylim = c(7.65, 33.97), expand = FALSE) +
    xlab(&quot;Longitude&quot;) + ylab(&quot;Latitude&quot;) +
    ggtitle(&quot;Map of the Gulf of Mexico and the Caribbean Sea&quot;) +
    theme(panel.grid.major = element_line(color = gray(.5),
        linetype = &quot;dashed&quot;, size = 0.5),
        panel.background = element_rect(fill = &quot;aliceblue&quot;))
</code></pre>
</div>
</div>
<p><img src="https://i2.wp.com/www.r-spatial.org/images/figure-markdown_mmd/world-florida-final-1.png?w=65%25&ssl=1" data-recalc-dims="1"></p>
<h2>Saving the map with <code>ggsave</code></h2>
<p>The final map now ready, it is very easy to save it using <code>ggsave</code>. This<br>
function allows a graphic (typically the last plot displayed) to be<br>
saved in a variety of formats, including the most common PNG (raster<br>
bitmap) and PDF (vector graphics), with control over the size and<br>
resolution of the outcome. For instance here, we save a PDF version of<br>
the map, which keeps the best quality, and a PNG version of it for web<br>
purposes:</p>
<div>
<div>
<pre><code>ggsave(&quot;map.pdf&quot;)
ggsave(&quot;map_web.png&quot;, width = 6, height = 6, dpi = &quot;screen&quot;)
</code></pre>
</div>
</div>
<div>
<ol>
<li>
<p>Note: Support of <code>sf</code> objects is available since version 3.0.0 of<br>
<code>ggplot2</code>, recently released on CRAN. <a rel="nofollow" href="https://www.r-spatial.org//r/2018/10/25/ggplot2-sf.html#fnref:1" target="_blank"><img alt="↩" src="https://s.w.org/images/core/emoji/2.4/72x72/21a9.png" class="wp-smiley"></a></p>
</li>
</ol>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://www.r-spatial.org//r/2018/10/25/ggplot2-sf.html"> r-spatial</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/n7DiModPhCo" height="1"></a></div>