From 0ab69c2209072a92c43dfe3917c1f3097acff365 Mon Sep 17 00:00:00 2001 From: GitHub Actions Date: Thu, 11 Jan 2024 14:09:55 +0000 Subject: [PATCH] markdown source builds MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Auto-generated via {sandpaper} Source : 397ea0c2a65a8145ddf1662d807c4072a2a641cf Branch : main Author : Alber Sánchez Time : 2024-01-11 14:07:23 +0000 Message : Merge pull request #148 from albhasan/ggsave_121 Use ggsave instead of pdf - dev.off to save plots --- 08-writing-data.md | 63 +++++++++++++++++++++------------------------- md5sum.txt | 2 +- 2 files changed, 30 insertions(+), 35 deletions(-) diff --git a/08-writing-data.md b/08-writing-data.md index 8372f3d4..b6d3f295 100644 --- a/08-writing-data.md +++ b/08-writing-data.md @@ -19,8 +19,8 @@ source: Rmd :::::::::::::::::::::::::::::::::::::::: instructor -Learners will need to have created the directory structure described in -[Project Management With RStudio](../episodes/02-project-intro.Rmd) in order +Learners will need to have created the directory structure described in +[Project Management With RStudio](../episodes/02-project-intro.Rmd) in order for the code in this episode to work. :::::::::::::::::::::::::::::::::::::::: @@ -33,24 +33,19 @@ You can save a plot from within RStudio using the 'Export' button in the 'Plot' window. This will give you the option of saving as a .pdf or as .png, .jpg or other image formats. -Sometimes you will want to save plots without creating them in the -'Plot' window first. Perhaps you want to make a pdf document with -multiple pages: each one a different plot, for example. Or perhaps -you're looping through multiple subsets of a file, plotting data from -each subset, and you want to save each plot. -In this case you can use a more flexible approach. The -`pdf()` function creates a new pdf device. You can control the size and resolution -using the arguments to this function. +Sometimes you will want to save plots without creating them in the 'Plot' +window first. Perhaps you want to make a pdf document, for example. Or perhaps +you're looping through multiple subsets of a file, plotting data from each +subset, and you want to save each plot. +In this case you can use flexible approach. The `ggsave` function saves the +latest plot by default. You can control the size and resolution using the +arguments to this function. ```r -pdf("Distribution-of-gdpPercap.pdf", width=12, height=4) -ggplot(data = gapminder, aes(x = gdpPercap)) + +ggplot(data = gapminder, aes(x = gdpPercap)) + geom_histogram() - -# You then have to make sure to turn off the pdf device! - -dev.off() +ggsave("Distribution-of-gdpPercap.pdf", width=12, height=4) ``` Open up this document and have a look. @@ -59,34 +54,34 @@ Open up this document and have a look. ## Challenge 1 -Rewrite your 'pdf' command to print a second -page in the pdf, showing the side-by-side bar -plot of gdp per capita in countries in the Americas -in the years 1952 and 2007 that you created in the +Create and save a new plot showing the side-by-side bar plot of gdp per capita +in countries in the Americas in the years 1952 and 2007 that you created in the previous episode. + + ::::::::::::::: solution ## Solution to challenge 1 ```r -pdf("Distribution-of-gdpPercap.pdf", width = 12, height = 4) -ggplot(data = gapminder, aes(x = gdpPercap)) + -geom_histogram() - -ggplot(data = gapminder_small_2, aes(x = country, y = gdpPercap, fill = as.factor(year))) + -geom_col(position = "dodge") + coord_flip() - -dev.off() +ggplot(data = gapminder_small_2, aes(x = country, y = gdpPercap, + fill = as.factor(year))) + + geom_col(position = "dodge") + + coord_flip() +# Note that ggsave saves by default the latest plot. +ggsave("Distribution-of-gdpPercap.pdf", width = 12, height = 4) ``` ::::::::::::::::::::::::: :::::::::::::::::::::::::::::::::::::::::::::::::: -The commands `jpeg`, `png`, etc. are used similarly to produce -documents in different formats. +To produce documents in different formats, change the file extension for +`jpeg`, `png`, `tiff`, or `bmp`. + + ## Writing data @@ -166,7 +161,7 @@ write.csv(gapminder_after_1990, :::::::::::::::::::::::::::::::::::::::: keypoints -- Save plots using `ggsave()` or `pdf()` combined with `dev.off()`. +- Save plots using `ggsave()`. - Use `write.csv` to save tabular data. :::::::::::::::::::::::::::::::::::::::::::::::::: @@ -174,11 +169,11 @@ write.csv(gapminder_after_1990, :::::::::::::::::::::::::::::::::::::::: instructor -- Now that learners know the fundamentals of R, the rest of the workshop will +- Now that learners know the fundamentals of R, the rest of the workshop will apply these concepts to working with geospatial data in R. -- Packages and functions specific for working with geospatial data will be the +- Packages and functions specific for working with geospatial data will be the focus of the rest of the workshop. -- They will have lots of challenges to practice applying and expanding these +- They will have lots of challenges to practice applying and expanding these skills in the next lesson. :::::::::::::::::::::::::::::::::::::::: diff --git a/md5sum.txt b/md5sum.txt index 57e15ab0..36d68323 100644 --- a/md5sum.txt +++ b/md5sum.txt @@ -10,7 +10,7 @@ "episodes/05-data-subsetting.Rmd" "b673744f991a865b9996504197cc013e" "site/built/05-data-subsetting.md" "2023-11-21" "episodes/06-dplyr.Rmd" "5d6106566981f73f1e3dc6a5c011fa28" "site/built/06-dplyr.md" "2023-11-21" "episodes/07-plot-ggplot2.Rmd" "7cbd4da57c055ecbc3ee80bd2694497a" "site/built/07-plot-ggplot2.md" "2023-11-21" -"episodes/08-writing-data.Rmd" "09b3d50f82e56fd74bdf0c2fae973ce3" "site/built/08-writing-data.md" "2023-11-21" +"episodes/08-writing-data.Rmd" "6fc88f62e725949865ac72611402feb6" "site/built/08-writing-data.md" "2024-01-11" "instructors/instructor-notes.md" "308d3e90d18e3f82f273f69cbf60cd9a" "site/built/instructor-notes.md" "2023-11-21" "learners/discuss.md" "9817497c91bc3ef97879d4d2941d158d" "site/built/discuss.md" "2023-11-21" "learners/reference.md" "868028752fe0796f1aa5ae8235ffaf63" "site/built/reference.md" "2023-11-21"