 It's LinkedIn Learning author Monica Wahee with today's data science makeover. Watch while Monica Wahee demonstrates using the gg-save command to export a plot in a designated format and dimensions in R. Hi everyone! Recently I wrote a blog post and made some videos showing you how to make box plots in R. In one of them I showed you how to make a box plot gg-plot 2 style. So I've always had this problem after making a plot in R and that is exporting the plot so that it has the right dimensions. Now I learned that if I use gg-save which is a command in the gg-plot 2 package I can solve that problem so I'm going to show you how to do that now. Okay so all this code is rerun. This is a repeat from one of my videos. I'll put these links in the description. And all this code is on github. But let me tell you about it. We start by reading in a csv called city compare. This just has three columns in it. Here I'll show you. See the data set? It has three columns. Hosp name, hosp city, and staff beds. If you guess that hosp stands for hospital you guessed right. What I did was I took this public information about hospitals in Boston where I live now and the hospitals in the Minneapolis St. Paul metropolitan area where I grew up. You'll see I'll put their names, the city, and how many staffed beds they had when I gathered this public data. That's how many beds the hospital is actually making available for inpatient admissions which became a big deal during the pandemic. But the thing you need to care about when it comes to the staff beds variable is that this is the continuous variable we are going to graph in the box plot. And the reason why we call the data frame city compare is that I wanted to use ggplot2 to demonstrate how to make two box plots side by side. One for each metro area so we could compare the distribution of staff beds and hospitals. But in this video I don't care about that at all. In this video we are just going to accept the ggplot2 code. We will just accept the box plots. We don't even care about interpreting them. This plot is just there for me to show you about gg save which is the command I'm going to use an R to automatically export the box plot out of R into my working directory which I mapped at the beginning of the session. And it's going to have the dimensions I set and be in the format I want. It's going to be gorgeous trust me. So this is the box plot code I went over in the other video. In this video we are just going to run it because I want to show you the problem I used to have with the plot. Oops I can't forget to run the library up here too. Okay let's run all this. Hey where's the plot? Why didn't it open? Oh yeah I forgot I had to do this thing. In the other video I just ran the ggplot2 code. In this video I actually had to save the ggplot2 plot as an object. See here I named the object boxplot underscore figure. Most of the time when you see tutorials in this on the web people just name it one letter like p for plot or g for graph. I named this a long name so you wouldn't freak out and go what is this p thing? What is g for? Because that's what I would always do and then it would be impossible for me to understand the code. I'm naming it this big name just to make it clear that I'm just saving this plot as an object. In fact let me just run this box plot underscore figure and prove this to you. Alrighty here it is. Okay so now here's the thing. See how it kind of looks like a box? Now when I did this for you on GitHub I just went here. I went to file, save as, jpeg and 100 quality. And this is what I got. See how this is exactly the shape of a square? Okay that's the shape. It came out before I saved it. It's not exactly the size and shape of a PowerPoint slide is it? Okay let's try something else. What happens if we stretch this out more? Wow look at those fat boxes. I hope my birthday present comes in one of those boxes. Okay let's see what happens if we save this plot the way I saved the other one. I'm going to do the same thing I did before. Go to file, save as, jpeg and 100 quality. And I'm going to name the file box plots wide. Let's go look at how it's saved. Look at those nice wide boxes. I love it. Okay but I don't love it for a PowerPoint slide. And I don't love it when a publisher asks me to put these plots in a size of a certain dimension and says they want png and not jpeg. I don't want to do that by hand so that's why we have gg save. Let's go back to our code. Okay let's close this wide thing and look at our gg save code. Okay now we are dealing with this code down here. Let's look at each line. Notice the first argument says file equals compare cities box plot dot png. That is the name the file is going to take on as our exports it and saves it. And remember it's going to save it in whatever working directory you have mapped so you will have to go there to look for the plot after you run this code. Okay in the next line we have units. A character vector that says in for inches. This is because I am in the U.S. If you are using the metric system or you want to use pixels you can change the setting. I'll link you to a gg save page in the description with more info about these options. So the next lines are width equals 8 and height equals 5.5 that translates to 8 inches wide and 5.5 inches tall. This was for a PowerPoint slide but if you want a portrait letter page you could just do 8.5 by 11. That's what we use for dimensions in the U.S. Finally we set the dots per inch or dpi at 300. This is what you can increase to improve the quality of the plot output. Finally our last argument is you guessed it the name of the object to gg save which is box plot underscore figure. Remember in most tutorials this is just a letter like g or p so if you see it in code you might miss it. Look carefully. Okay now when I run this we should be able to go to my working directory and see the file show up there. Let me do that. Okay now let's go look for this in our working directory. Okay see here this is the location to where I mapped our working directory for this video. These are the files on GitHub for you. Remember how we set the file option to comparisonisboxplot.png? So we should see a png file in here called comparisonisboxplot. Here it is. Let's open it. Oh it's too big. Let's zoom out a few notches. Okay there we go. This is a png and look. It's in perfect dimensions. Thanks to gg save our plot is gorgeous. Thank you for watching this data science makeover with LinkedIn Learning author Monica Wahee. Remember to check out Monica's data science courses on LinkedIn Learning. Click on the link in the description. Thank you so much for watching this video. If you actually like the video then please hit the like button. Also I encourage you to look around my channel and if you see anything you like subscribe. I'm trying to add videos that I think my learners will find useful so feel free to request a topic. Just leave me a comment and enjoy your day.