Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ggsave instead of pdf - dev.off to save plots #148

Merged
merged 2 commits into from
Jan 11, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 29 additions & 34 deletions episodes/08-writing-data.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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.

::::::::::::::::::::::::::::::::::::::::
Expand All @@ -39,23 +39,18 @@ 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, eval=FALSE}
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.
Expand All @@ -64,33 +59,33 @@ 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, eval=FALSE}
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, just change the file extension for
albhasan marked this conversation as resolved.
Show resolved Hide resolved
`jpeg`, `png`, `tiff`, or `bmp`.



## Writing data

Expand Down Expand Up @@ -171,19 +166,19 @@ unlink("cleaned-data", recursive=TRUE)

:::::::::::::::::::::::::::::::::::::::: keypoints

- Save plots using `ggsave()` or `pdf()` combined with `dev.off()`.
- Save plots using `ggsave()`.
- Use `write.csv` to save tabular data.

::::::::::::::::::::::::::::::::::::::::::::::::::


:::::::::::::::::::::::::::::::::::::::: 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.

::::::::::::::::::::::::::::::::::::::::
Loading