Get access to all lessons in this course.
-
RMarkdown
- Why Use RMarkdown?
- RMarkdown Overview
- YAML
- Text
- Code Chunks
- Wrapping Up
-
Data Wrangling and Analysis
- Getting Started
- The Tidyverse
- select
- mutate
- filter
- summarize
- group_by
- count
- arrange
- Create a New Data Frame
- Crosstabs
- Wrapping Up
-
Data Visualization
- An Important Workflow Tip
- The Grammar of Graphics
- Scatterplots
- Histograms
- Bar Charts
- color and fill
- scales
- Text and Labels
- Plot Labels
- Themes
- Facets
- Save Plots
- Wrapping Up
-
Wrapping Up
- You Did It!
Fundamentals of R
Save Plots
This lesson is locked
This lesson is called Save Plots, part of the Fundamentals of R course. This lesson is called Save Plots, part of the Fundamentals of R course.
Transcript
Click on the transcript to go to that point in the video. Please note that transcripts are auto generated and may contain minor inaccuracies.
Your Turn
Complete the save plots sections of the data-visualization-exercises.Rmd file.
Learn More
Saving Plots Resources
I have only showed you how to save plots by saving the last plot you made. You can also save a plot as an object and then use this object as an argument in your ggsave function. For more on doing that, see the relevant page from the Stat545 course by Jenny Bryan.
You need to be signed-in to comment on this post. Login.
Catherine Roller White
April 13, 2021
I was able to save this as a .png, but when I tried to save as a .pdf, I received this message:
Error in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, : invalid font type
It still created a .pdf, but the only text on the figure was the data labels (no title, axes, etc.) Perhaps we will be covering this on Thursday when we talk about fonts? Thank you.
David Keyes
April 13, 2021
Yes, this is a common thing, unfortunately. We can talk about it more on Thursday. Another thing you can try is the strategy in the Customize Your Fonts lesson in Going Deeper.
Lina Khan
October 7, 2021
I'm having the same issue as the above, when tried saving to pdf! Sounds like this might be covered in a future lesson?
David Keyes
October 8, 2021
Yes, it will be. Things have changed recently with regard to custom fonts. I also recorded this video for the Spring 2021 R in 3 Months cohort, which explains a bit about the best way to deal with custom fonts today.
Matt M
October 10, 2021
I'm on a windows machine and still had problems saving a pdf even after loading ragg and systemfonts. "Error in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, : invalid font type In addition: There were 50 or more warnings (use warnings() to see the first 50)"
When I change the theme away from ipsum to theme_light() I had no problems saving as a pdf.
David Keyes
October 11, 2021
When you use
ggsave()
can you try addingdevice = cairo_pdf
as in this example:ggsave("plot.png", device = cairo_pdf)
Let me know if that makes a difference (if so, I'll explain a bit more about why).
Abby Isaacson
April 14, 2021
I saved without any errors, but I can't find it anywhere! Not in my files, nor the "plots" tab (what is that?).
David Keyes
April 14, 2021
Can you share your code so I can see it?
Abby Isaacson
April 14, 2021
ggsave(filename = "plots/my-sleep-plot.png", height = 5, width = 8, units = "in")
David Keyes
April 15, 2021
Very weird! And you're sure you're working in a project, right?
Abby Isaacson
April 15, 2021
OK, I found it in the fundamentals master folder from the class, under a new plots folder. However both my class exercises file and my class project file are saved under the same desktop folder pathway (Rin3mo), which Charlie mentioned in a real world might want to be saved separately. My class project is also having trouble saving my last plot (Error in grDevices::dev.off() : QuartzBitmap_Output - unable to open file 'plots/avg-mmr-by-type.png') - but I found it saved as "my-sleep-plot" (?) so something weird is going on.
Elan Sykes
October 13, 2021
My code for the facets section worked to get the right plot, and I wrote my saving code the same way the solutions suggest, but when I save the plot the background turns from white in the facets section original plot to dark grey in the png file. Is this something from the default? And how do I change individual elements of the plot while keeping the overall theme?
David Keyes
October 14, 2021
So I think what's happening is that the background is actually transparent and you're just seeing the dark grey being rendered on your computer. You can adjust this by tweaking your theme. You can learn a bit more about this in this lesson. tldr: use
theme(plot.background = element_rect(fill = "white"))
and it should (if my code is correct) make your background white.Jeremy Danz
February 4, 2023
I had the same issue as Elan, and this fix worked perfectly, thanks.
Andrew Paquin
April 5, 2023
Hi David, I have encountered a problem during this lesson. Something is up with RStudio, and I can't put my finger on it. It had been working well, but suddenly it appears that tidyverse is unavailable to me. When I try to knit, I get the following:
Error: package or namespace load failed for ‘tidyverse’: .onAttach failed in attachNamespace() for 'tidyverse', details: call: NULL error: package or namespace load failed for ‘ggplot2’ in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]): namespace ‘rlang’ 1.0.6 is already loaded, but >= 1.1.0 is required
I've also been getting this a lot: In addition: Warning message: In gzfile(file, mode) : cannot open compressed file 'C:\Users\paquinan\AppData\Local\Temp\Rtmp4cUZ6h/libloc_185_13d200b7.rds', probable reason 'No such file or directory'
All of this started last night when I tried to access my html file for this section and realized that there isn't one.
Any ideas?
I have tried to run updates, and I see that there are six of them, but they don't take. I go through a perpetual cycle of being asked if I want R to restart. In the end, nothing changes. This ongoing cycle (being asked about restarting R, saying yes, being asked again...) is accompanied by repetitions of this message:
> install.packages(c("cli", "ggplot2", "htmltools", "ps", "rlang", "vctrs")) Error in install.packages : Updating loaded packages
Restarting R session...
Andrew Paquin
April 5, 2023
With all my cutting and pasting, I see I put things out of order. The last section (starting at "I have tried to run updates" to "Restarting R session..." should come the "I've also been getting this a lot" section. Sorry about the confusion.
Melissa Mullins
April 19, 2023
My plot looked great as a png even though I had overlapping text issues displaying in R studio. I wanted to see what they looked like if I knit, but I couldn't successfully do it. When it gets to this line of code install.packages("devtools") I get an error that says this: processing file: data-visualization-exercises.Rmd |....................................... | 77% (unnamed-chunk-19)Quitting from lines 276-279 (data-visualization-exercises.Rmd) Error in contrib.url(repos, "source") : trying to use CRAN without setting a mirror Calls: ... eval_with_user_handlers -> eval -> eval -> install.packages -> contrib.url
Execution halted
Suggestions appreciated!
David Keyes
April 19, 2023
Putting this here for anyone wondering: you should put
install.packages("devtools")
in the console (not in your RMarkdown document). Hope that helps!