Skip to content

Commit

Permalink
markdown source builds
Browse files Browse the repository at this point in the history
Auto-generated via {sandpaper}
Source  : 397ea0c
Branch  : main
Author  : Alber Sánchez <[email protected]>
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
  • Loading branch information
actions-user committed Jan 11, 2024
1 parent 03ca5c2 commit 0ab69c2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 35 deletions.
63 changes: 29 additions & 34 deletions 08-writing-data.md
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 @@ -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.
Expand All @@ -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

Expand Down Expand Up @@ -166,19 +161,19 @@ 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.

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


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

::::::::::::::::::::::::::::::::::::::::
2 changes: 1 addition & 1 deletion md5sum.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 0ab69c2

Please sign in to comment.