7.8 Visual outputs

R supports many different static and interactive graphics formats.The most general method to save a static plot is to open a graphic device, create a plot, and close it, for example:

  1. png(filename = "lifeExp.png", width = 500, height = 350)
  2. plot(world["lifeExp"])
  3. dev.off()

Other available graphic devices include pdf(), bmp(), jpeg(), png(), and tiff().You can specify several properties of the output plot, including width, height and resolution.

Additionally, several graphic packages provide their own functions to save a graphical output.For example, the tmap package has the tmap_save() function.You can save a tmap object to different graphic formats by specifying the object name and a file path to a new graphic file.

  1. library(tmap)
  2. tmap_obj = tm_shape(world) +
  3. tm_polygons(col = "lifeExp")
  4. tmap_save(tm = tmap_obj, filename = "lifeExp_tmap.png")

On the other hand, you can save interactive maps created in the mapview package as an HTML file or image using the mapshot() function:

  1. library(mapview)
  2. mapview_obj = mapview(world, zcol = "lifeExp", legend = TRUE)
  3. mapshot(mapview_obj, file = "my_interactive_map.html")