Graphic Devices


Simply put, it’s the output medium of your graphic oriented code, when you make a graph in R it has to be sent somewhere. It’s either a screen device: window on a computer, or a file device such as

  • PDF

  • PNG

  • JPEG

  • SVG

  • TIFF…

You can learn more about it by typing ?Devices in R.

Plotting to a screen was explained earlier in other sections.

with(faithful, plot(eruptions,waiting, main="Old Faithful Geyser data"))

or we can use the command title()

with(faithful, plot(eruptions,waiting))
title(main="Old Faithful Geyser data")

Current device

To show the current graphic device use dev.cur()

dev.cur()
png 
  2 

New device

To launch a new device, let’s say we want to save the output to a pdf file, we have to launch the new device and create the file.

Change device

You can change the active graphics device with dev.set(integer) where (integer) is the number associated with the graphics device you want to switch to. The function dev.copy copies a plot from one device to another, and dev.copy2pdf specifically copies a plot to a PDF file.

Close device

When you are done plotting the graph onto your pdf you MUST close the device dev.off()

Save to pdf


Another way would be to use the function dev.copy copies a plot from one device to another, and dev.copy2pdf specifically copies a plot to a PDF file.

pdf(file="myplot.pdf")
#then run the plot
with(faithful, plot(eruptions,waiting))
title(main="Old Faithful Geyser data")
# the saved file will not appear till you close the connection
dev.off()

Save to png


Similar to saving to pdf, we can do it in several ways

Use dev.copy()

We can use dev.copy(), and remember it will not save to the file until we close the connection. So, let’s save the plot to a png file.

with(faithful,plot(eruptions, waiting))
title(main="Old Faithful Geyser data")
dev.copy(png,file="geyserplot.png")
dev.off()

Use png()

png(filename = "/plot1.png", width = 480, height = 480, units = "px")
with(twodays,hist(Global_active_power,
                  xlab="Global Active Power (kilowatts)",
                  main="Global Active Power",
                  col="red") )
dev.off()

GGSAVE

A graphics device allows a plot to appear on your computer. Examples include:

  • A window on your computer (screen device)

  • A PDF, PNG, or JPEG file (file device)

  • An SVG, or scalable vector graphics file (file device)

When you make a plot in R, it has to be “sent” to a specific graphics device. To save images without using ggsave(), you can open an R graphics device like png() or pdf(); these will allow you to save your plot as a .png or .pdf file. You can also choose to print the plot and then close the device using dev.off().

Example of using png() Example of using pdf()
png(file = “exampleplot.png”, bg = “transparent”) plot(1:10) rect(1, 5, 3, 7, col = “white”) dev.off() pdf(file = “/Users/username/Desktop/example.pdf”,     width = 4,      height = 4)  plot(x = 1:10,       y = 1:10) abline(v = 0) text(x = 0, y = 1, labels = “Random text”) dev.off()

To learn more about the different processes for saving images, check out these resources:

  • Saving images without ggsave(): This resource is pulled directly from the ggplot2 documentation at tidyverse.org. It explores the tools you can use to save images in R, and includes several examples to follow along with and learn how to save images in your own R workspace.

  • How to save a ggplot: This resource covers multiple different methods for saving ggplots. It also includes copyable code with explanations about how each function is being used so that you can better understand each step in the process.

  • Saving a plot in R: This guide covers multiple file formats that you can use to save your plots in R. Each section includes an example with an actual plot that you can copy and use for practice in your own R workspace.

The `ggsave()` function will save your image as a 7x7 at the file path you input by default, which makes it simple to export your plots from R.

ggsave('hotel_booking_chart.png', width=7, height=7)