Skip to content

Commit 2763a7f

Browse files
committed
markdown source builds
Auto-generated via `{sandpaper}` Source : 54fc9ab Branch : main Author : Christian Knudsen <christian.knudsen@gmail.com> Time : 2025-03-07 11:07:09 +0000 Message : Merge pull request #47 from chrbknudsen/main så er vi vist tæt på målet
1 parent 7b6876d commit 2763a7f

7 files changed

Lines changed: 293 additions & 1 deletion
38.7 KB
Loading
22.9 KB
Loading

fig/theming-rendered-theming-1.png

22 KB
Loading

md5sum.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"file" "checksum" "built" "date"
22
"CODE_OF_CONDUCT.md" "c93c83c630db2fe2462240bf72552548" "site/built/CODE_OF_CONDUCT.md" "2025-03-07"
33
"LICENSE.md" "064f8d1e4ca631646e5171f0079c3808" "site/built/LICENSE.md" "2025-03-07"
4-
"config.yaml" "9bcbdbb6fe2f9d0e79a7d7df0268f4b1" "site/built/config.yaml" "2025-03-07"
4+
"config.yaml" "56716664ad877ea6fea2c2c9fea3ae0b" "site/built/config.yaml" "2025-03-07"
55
"index.md" "a02c9c785ed98ddd84fe3d34ddb12fcd" "site/built/index.md" "2025-03-07"
66
"links.md" "8184cf4149eafbf03ce8da8ff0778c14" "site/built/links.md" "2025-03-07"
77
"episodes/introduction.Rmd" "53c8b8d7f72a2b56fefcf2ecc78cc161" "site/built/introduction.md" "2025-03-07"
@@ -10,6 +10,9 @@
1010
"episodes/different_plots.Rmd" "876ae633cee342249d5e9507954d42d0" "site/built/different_plots.md" "2025-03-07"
1111
"episodes/facets.Rmd" "144c3fad41afb8510992320f534ab906" "site/built/facets.md" "2025-03-07"
1212
"episodes/scaling-and-coordinates.Rmd" "fc43a8f9e747e78c6dfc47fba6f6c464" "site/built/scaling-and-coordinates.md" "2025-03-07"
13+
"episodes/theming.Rmd" "470adc462f6041114588b46409c84c20" "site/built/theming.md" "2025-03-07"
14+
"episodes/saving.Rmd" "1f0fb37b3a3459f098b42dba856797bb" "site/built/saving.md" "2025-03-07"
15+
"episodes/whats-next.Rmd" "6f12f5d1ac48c801d6cd8ea823b071bc" "site/built/whats-next.md" "2025-03-07"
1316
"instructors/instructor-notes.md" "5cf113fd22defb29d17b64597f3c9bc0" "site/built/instructor-notes.md" "2025-03-07"
1417
"learners/reference.md" "527a12e217602daae51c5fd9ef8958df" "site/built/reference.md" "2025-03-07"
1518
"learners/setup.md" "61568b36c8b96363218c9736f6aee03a" "site/built/setup.md" "2025-03-07"

saving.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
title: "Saving and exporting"
3+
teaching: 5
4+
exercises: 0
5+
---
6+
7+
::::questions
8+
- "How can I save the plots?"
9+
::::
10+
11+
::::objectives
12+
- "Learn how to save your plots in different formats"
13+
::::
14+
15+
16+
17+
18+
19+
20+
## It would be nice to be able to save the plot.
21+
22+
Saving a plot can be done directly from the plot pane in
23+
RStudio
24+
![Saving from Rstudio](../fig/08-saving-plot.png)
25+
26+
27+
ggplot2 also includes a function for saving the last plot
28+
you made. This function will save it as "myimage.png" in your
29+
current directory. The image will be 800x600 pixels (px) in size, and with a resolution of 300 dpi.
30+
31+
32+
``` r
33+
ggsave("myimage.png", width = 800,
34+
height = 600, units = "px",
35+
dpi = 300)
36+
```
37+
38+
However, this does not look very nice:
39+
![Nasty looking](../fig/myimage.png)
40+
The points are too big for the plot!
41+
42+
Be prepared for a lot of fiddling about with your plots if
43+
you want to use `ggsave()`.
44+
45+
Adjusting size, and getting af nice image is often easier
46+
adjusting the size of the plot pane directly in RStudio.
47+
48+
The saved image will reflect what you see on the screen.
49+
50+
## There is more than JPG and PNG in the world!
51+
JPG is a popular format for saving images. It produces nice,
52+
small files. PNG is also a popular format for images.
53+
54+
By default, ggsave is able to recognize the extension you
55+
give your file name (.png in the example above), and save
56+
to these formats:
57+
58+
* eps
59+
* ps
60+
* tex
61+
* pdf
62+
* jpeg/jpg
63+
* tiff
64+
* png
65+
* bmp
66+
* svg
67+
* wmf (only on windows)
68+
69+
We would like to recommend the "svg" format. That format is a "vector-based" format
70+
that you can scale to any size you want.
71+
72+
::::keypoints:
73+
- "The easiest way to adjust the size of your saved plots is by adjusting the plot window in RStudio"
74+
::::

theming.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
---
2+
title: "Theming"
3+
teaching: 10
4+
exercises: 5
5+
---
6+
7+
:::: questions
8+
- "How can I make the plot look good?"
9+
- "How do I get rid of that grey background?"
10+
- "How do I get rid of the gridlines?"
11+
::::
12+
13+
::::objectives
14+
- "Learn to use different themes"
15+
- "Learn to adjust the appearance of specific parts of the plot"
16+
::::
17+
18+
19+
20+
21+
22+
### The THEME_FUNCTIONs
23+
24+
Every part of the plot can be changed. The grey background might be annoying
25+
The gridlines might be confusing.
26+
27+
These non-data components of the plots can be controlled using the family of `theme`
28+
functions:
29+
30+
31+
``` r
32+
ggplot(diamonds, aes(carat, price)) +
33+
geom_point() +
34+
theme_bw()
35+
```
36+
37+
<img src="fig/theming-rendered-theme_bw-1.png" style="display: block; margin: auto;" />
38+
More exists:
39+
40+
``` error
41+
Error in default | bw: operations are possible only for numeric, logical or complex types
42+
```
43+
44+
:::: callout
45+
## Notice the pattern?
46+
47+
A general pattern of function names in ggplot2 can be seen.
48+
49+
Themes are named "theme_" and then the name of the theme. We saw the
50+
same pattern with the scale functions: "scale_" and then the axis, followed
51+
by what we did to the axis, eg: "scale_y_log10"
52+
53+
::::
54+
55+
### Even more theming
56+
57+
*Every* element in the plot can be controlled. The `theme()` function is the
58+
way to do that:
59+
60+
61+
``` r
62+
diamonds %>%
63+
ggplot(aes(carat, price)) +
64+
geom_point() +
65+
theme_dark() +
66+
theme(axis.text.y = element_text(angle= 45))
67+
```
68+
69+
<img src="fig/theming-rendered-theming-1.png" style="display: block; margin: auto;" />
70+
71+
Angling the labels in a plot can be good for readability. However the actual
72+
way to do it can be a bit more involved as you see above. Read the help for
73+
theme to get at complete list of things that can be changed. There are 97
74+
things in total.
75+
76+
Also note, that we can add theming on top of previous theming. Here we begin
77+
with a built-in theme that we like, and change the parts we want to change.
78+
79+
Finally note, that the order is important:
80+
81+
82+
83+
``` r
84+
diamonds %>%
85+
ggplot(aes(carat, price)) +
86+
geom_point() +
87+
theme_dark() +
88+
theme(axis.text.y = element_text(angle= 45))
89+
```
90+
and
91+
92+
``` r
93+
diamonds %>%
94+
ggplot(aes(carat, price)) +
95+
geom_point() +
96+
theme(axis.text.y = element_text(angle= 45)) +
97+
theme_dark()
98+
```
99+
100+
Will not give the same result. `theme_dark` has a setting for the way the text
101+
on the y-axis is shown, and will overwrite the changes done before calling it.
102+
103+
Most of the `elements` of the plot need to be defined in a special way. If we
104+
want the "theme" a text element, we set the `axis.text` to be an `element_text()`
105+
function with specific arguments to specify *what* we want to do. For the background
106+
of the plot we are changing a rectangular object `element_rect`, and can set the background
107+
color like this:
108+
109+
110+
``` r
111+
diamonds %>%
112+
ggplot(aes(carat, price)) +
113+
geom_point() +
114+
theme(axis.text.y = element_text(angle= 45),
115+
panel.background = element_rect(fill = "red"))
116+
```
117+
118+
<img src="fig/theming-rendered-background-1.png" style="display: block; margin: auto;" />
119+
Note that we are not setting the `plot.background`, as that would change the
120+
background of the entire plot, rather than the background of the actual
121+
area on which we are plotting.
122+
123+
::::keypoints
124+
- "EVERYTHING in the plot can be customized"
125+
::::

whats-next.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
---
2+
title: "Whats next?"
3+
teaching: 10
4+
exercises: 5
5+
---
6+
7+
::::questions
8+
- "What is the next step in learning to plot?"
9+
::::
10+
11+
::::objectives
12+
- "Provide tips on where to locate data for plotting"
13+
- "Provide tips for finding inspiration for plotting"
14+
::::
15+
16+
17+
18+
19+
20+
## What should I do next?
21+
22+
First of all: If you do not have data you want to visualize
23+
already. Find some!
24+
25+
[Kaggle](https://www.kaggle.com/datasets) host competitions
26+
in machine learning. For the use of those competitions, they
27+
give access to a lot of interesting datasets to work with.
28+
With more than 200.000 datasets at time of writing, it can
29+
be a bit overwhelming, so consider looking at the "[Data Visualization](https://www.kaggle.com/datasets?fileType=csv&tags=13208-Data+Visualization)" category. The link provided only shows datasets saved as CSV-files, and has only about 2.500 datasets.
30+
31+
## Play around!
32+
33+
ggplot2 comes with a lot of functionality. This is the
34+
list of build-in geoms in ggplot2:
35+
36+
37+
``` output
38+
[1] "geom_abline" "geom_area" "geom_bar"
39+
[4] "geom_bin_2d" "geom_bin2d" "geom_blank"
40+
[7] "geom_boxplot" "geom_col" "geom_contour"
41+
[10] "geom_contour_filled" "geom_count" "geom_crossbar"
42+
[13] "geom_curve" "geom_density" "geom_density_2d"
43+
[16] "geom_density_2d_filled" "geom_density2d" "geom_density2d_filled"
44+
[19] "geom_dotplot" "geom_errorbar" "geom_errorbarh"
45+
[22] "geom_freqpoly" "geom_function" "geom_hex"
46+
[25] "geom_histogram" "geom_hline" "geom_jitter"
47+
[28] "geom_label" "geom_line" "geom_linerange"
48+
[31] "geom_map" "geom_path" "geom_point"
49+
[34] "geom_pointrange" "geom_polygon" "geom_qq"
50+
[37] "geom_qq_line" "geom_quantile" "geom_raster"
51+
[40] "geom_rect" "geom_ribbon" "geom_rug"
52+
[43] "geom_segment" "geom_sf" "geom_sf_label"
53+
[46] "geom_sf_text" "geom_smooth" "geom_spoke"
54+
[49] "geom_step" "geom_text" "geom_tile"
55+
[52] "geom_violin" "geom_vline"
56+
```
57+
ggplot2 is also build as an extensible package, making
58+
it relatively easy to build extensions, that does things
59+
that ggplot2 is not able to on its own. [This page](https://exts.ggplot2.tidyverse.org/) contains a collection of these.
60+
61+
62+
## Get some inspiration!
63+
64+
The website/book [Fundamentals of Data Visualization](https://clauswilke.com/dataviz/) is a great resource for tips, tricks and thinking about visualizations,
65+
especially the [directory of visualizations](https://clauswilke.com/dataviz/directory-of-visualizations.html). Note, however, that the author does not provide examples of the code you need to write to make the plots.
66+
67+
There exist an online challenge, [#30DayChartChallenge](https://30daychartchallenge.org/), that
68+
challenges you to create a data visualization on a certain topic each day of april. That can be a bit of a mouthful and
69+
we are not going to participate ourselves. But! The collection
70+
of visualizations from 2022 is humongous, and a great place
71+
to find ideas.
72+
73+
74+
## Other online courses
75+
76+
[EdX](https://www.edx.org/search?q=ggplot) offers a multitude of interesting courses - here we link to their ggplot-related courses.
77+
78+
Codecademy also offers free courses (and a certificate of completion if you pay) [This course](https://www.codecademy.com/learn/learn-ggplot-2) offers
79+
a more indepth introduction to ggplot2.
80+
81+
## Extensions
82+
83+
ggplot2 is build for extensions. And there are many.
84+
85+
* (Hack for ggplot)[https://teunbrand.github.io/ggh4x/] A packages with utilities for doing stuff on the edge of what ggplot is designed for.
86+
87+
88+
::::keypoints
89+
- "ggplot2 is extensible - a LOT of extensions are available"
90+
::::

0 commit comments

Comments
 (0)