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:
png(filename = "lifeExp.png", width = 500, height = 350)
plot(world["lifeExp"])
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.
library(tmap)
tmap_obj = tm_shape(world) +
tm_polygons(col = "lifeExp")
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:
library(mapview)
mapview_obj = mapview(world, zcol = "lifeExp", legend = TRUE)
mapshot(mapview_obj, file = "my_interactive_map.html")