Get startedGet started for free

Saving a tmap plot

Saving tmap plots is easy with the tmap_save() function. The first argument, tm, is the plot to save, and the second, filename, is the file to save it to. If you leave tm unspecified, the last tmap plot printed will be saved.

The extension of the file name specifies the file type, for example .png or .pdf for static plots. One really neat thing about tmap is that you can save an interactive version which leverages the leaflet package. To get an interactive version, use tmap_save() but use the file name extension .html.

This exercise is part of the course

Visualizing Geospatial Data in R

View Course

Exercise instructions

Save your plot from the previous exercise in the following ways. Neither plot will display in your workspace, but you'll be able to take a look at them once you complete the exercise.

  • Save it as a static plot by specifying the filename population.png.
  • Save it as an interactive plot by specifying the filename population.html.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

library(sp)
library(tmap)

# Plot from last exercise
tm_shape(countries_spdf) +
  tm_grid(n.x = 11, n.y = 11, projection = "longlat") +
  tm_fill(col = "population", style = "quantile")  +
  tm_borders(col = "burlywood4")

# Save a static version "population.png"


# Save an interactive version "population.html"
Edit and Run Code