Graphics devices in R are very important because we need them to make plots. There are many different types of graphics devices that we can on which we can make plots, and so this lecture's going to cover, just briefly the various different graphics devices that are available to most users of R. So, first of all, what is a graphics device? So very simply, a graphics device is, is something or some place where you can make a plot appear. So the most obvious place for, for a plot to appear is on your screen, or, or a window on your screen. And so when you open up R, and you call the plot function, a window, a graphics window launches and your plot appears there. So that's called the screen device. Other places where you might want to send the plot include things like files. And so files come in a variety of formats. You can have a PDF file, a PNG file, or a JPEG file, or, or a scalable vector graphics SVG file. And so there are a lot of different file devices that you can choose from, and you can send a plot to those files. So any time you make a plot in R, it has to be sent to a specific graphics device. otherwise, it, that, it, it's, there's no way for it to be generated. The most, and the most common place for a plot to be sent is this, this, is this screen device. So, the screen device on the Mac, is called a quartz, on Windows, it's called windows and on the, on Unix/Linux systems it's called x11. So when you make a plot, you, the, you first have to think about where's this plot going to show up. And there's a full list of the devices that are available to you under the devices help page here, which you can call up with the question mark devices. There are also graphics devices that have been created by users on CRAN, that you can make available to R, by installing those packages. We won't talk about those here. I'm going to focus mostly on the devices that come with R. so, when you're plotting data and you're doing some visualizations and exploratory analysis and kind of looking through some data, usually the most obvious place to send a plot is the screen device. So you call a plot function like plot, or xyplot, or maybe you're using qplot and ggplot2 and the plot will go to the screen device. And then you can look at your data, very quickly, and see if you want to make a, see what you're going to do for the next plot. on, on any given platform, whether you're using a Mac or Windows or Unix, there's only one screen device. So, you don't have to make any choice about, you know, what screen device you're going to send it to. There's only, there's only one place to go, and so, you don't have to worry about it. Now, however, usually after you've done some exploratory analysis and you've kind of figured out, well, what plots are things that you want to keep or you want to include. Or maybe you want to send a plot to someone then you have to, you have to make the plot on another type of device, you for example, a file device. So when you make a plot in a file then you can take that file. You can include it in a report. You can include in a, in a presentation that you're making. Or maybe you can email it someone. And so files are more, much more useful for that type of purpose. So there are many different files devices to choose from unlike with the screen device and so we'll talk about some of those file devices here. It's important to realize that not every graphics device that I talk about in this lecture, will be available on all platforms. And so, for, for example, there's no Windows device on a Mac or on a UNIX system and similarly there's no quartz device on a Windows system. So, you won't be able to open every single graphics device on every platform. so, how does a plot get created? And this might seem a little obvious, but there's actually more than one way to, to create a plot. So the first one is which is the, kind of the simplest approach and probably the one that you're most, you're already familiar with is that you call a plotting function like plot, or xyplot, or qplot, and the plot just appears on the screen or the screen device I should say. And then you can annotate the plot with functions like text or title or ac, or or things like that. And then once you finish annotating your plot and it looks the way you like it, then that's it. You're finished. The plot is on the screen and you can look at it. You can show it to someone else on the screen. So here's some code that just loads the dataset and creates a plot using the plot function and then it adds a title to the top of the plot and then that's it. So, it's, that's a very simple way to create a plot on the screen. Another way for creating a plot is most commonly used for file devices and it's, and it involves explicitly launching a graphics device. So you might open a file device, and then you create the plot using plotting functions. You can in the case of, if your using a file device it's a little bit tricky because the plot will not actually appear on the screen, because all the plotting information is going into a file. And so you have to know exactly what you're doing when you create a, a plot to a file because you won't be able to see it on the screen. So typically, the best way to do this is to save your code, your plotting code, in a separate file and that way you can just kind of cut and paste the code into R, and you, and you don't have to worry about, you know, making a, making a mistake while you're typing the code in. So after you create the plot, you annotate it if you need to. Then you have to close the graphics device with the function dev.off. Once you've closed the graphics device then the, the plot is complete and if you send it to a file, you can open the file in your computer or you can look at it. You can include it in some, in a re, in a presentation or you can email it to someone. So here's some code that just opens the PDF device here, with the PDF function. You have to give it a file name so I just call, called it myplot.pdf. And then you have to make the plot, do some annotation and then you have to close the plot with the dev.off function. So, once I've called dev.off there should be a file in my working directory called myplot.pdf and you can open that with any PDF viewer.