Skip to content

Commit f4d7de8

Browse files
authored
Merge pull request #12 from gagneurlab/main
merge
2 parents e15d494 + 0a02146 commit f4d7de8

25 files changed

Lines changed: 11865 additions & 824 deletions

_bookdown.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ language:
1111
edit: "Edit"
1212
chapter_name: "Chapter "
1313

14-
14+
1515
rmd_files: [
1616
"index.Rmd",
1717
"topic00_Introduction/script/script_00.Rmd",
1818
"topic01_R_Basics/script/script_01.qmd"
19-
# "topic02_Data_Wrangling/script/script_02.Rmd",
20-
# "topic03_Tidy_Data/script/script_03.Rmd",
21-
# "topic04_Plotting-I/script/script_04.Rmd",
22-
# "topic05_Plotting-II/script/script_05.Rmd",
19+
"topic02_Data_Wrangling/script/script_02.Rmd",
20+
"topic03_Tidy_Data/script/script_03.qmd",
21+
"topic04_Plotting-I/script/script_04.qmd",
22+
"topic05_Plotting-II/script/script_05.qmd",
2323
# "topic06_Plotting-III/script/script_06.Rmd",
2424
# "topic07_Statistical-Testing-I/script/script_07.Rmd",
2525
# "topic08_Statistical-Testing-II/script/script_08.Rmd",
@@ -29,7 +29,7 @@ rmd_files: [
2929
# "topic12_SupervisedLearning/script/script_12.Rmd",
3030
# "appendix01_Data-Importing/script/appendix_01.Rmd",
3131
# "appendix02_R_Basics/script/appendix_02.Rmd",
32-
# "appendix03_Plotting/script/appendix_03.Rmd",
33-
# "appendix04_Probabilities/script/appendix_04.Rmd"
32+
"appendix03_Plotting/script/appendix_03.qmd",
33+
"appendix04_Probabilities/script/appendix_04.qmd"
3434
]
3535
before_chapter_script: "_common.R"

_quarto.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ book:
8080
- topic02_Data_Wrangling/script/script_02.qmd
8181
- topic03_Tidy_Data/script/script_03.qmd
8282
- topic04_Plotting-I/script/script_04.qmd
83+
- topic05_Plotting-II/script/script_05.qmd
8384

84-
appendices:
85+
appendices:
86+
- appendix03_Plotting/script/appendix_03.qmd
8587
- appendix04_Probabilities/script/appendix_04.qmd
8688

appendix03_Plotting/script/appendix_03.Rmd

Lines changed: 0 additions & 146 deletions
This file was deleted.
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
---
2+
title: "Additional plotting tools"
3+
format:
4+
html:
5+
number-sections: true
6+
embed-resources: true
7+
execute:
8+
kernel: dataviz
9+
echo: true
10+
fig-height: 3
11+
fig-width: 4
12+
warning: false
13+
14+
---
15+
16+
```{python}
17+
#| include: false
18+
19+
import pandas as pd
20+
import numpy as np
21+
from plotnine import *
22+
import matplotlib.pyplot as plt
23+
from IPython.display import display, Image
24+
import matplotlib
25+
26+
ind = pd.read_csv('../../extdata/CPI_HDI.csv').drop(columns=['Unnamed: 0'])
27+
28+
# Plot theme similar to the original R `mytheme`
29+
mysize = 9
30+
mytheme = theme(
31+
axis_title = element_text(size=mysize),
32+
axis_text = element_text(size=mysize),
33+
legend_title = element_text(size=mysize),
34+
legend_text = element_text(size=mysize),
35+
) + theme_minimal(base_size=mysize)
36+
37+
np.random.seed(0)
38+
```
39+
40+
## Plotting themes
41+
42+
Themes control non-data parts of your plots, such as:
43+
44+
* Overall appearance
45+
* Axes
46+
* Plot title
47+
* Legends
48+
49+
They control the appearance of your plots (size, color, position) but not how the data is represented.
50+
One example is `theme_bw` which provides a white background.
51+
52+
```{python}
53+
#| fig-align: center
54+
55+
(ggplot(ind, aes('CPI', 'HDI', color='region')) +
56+
geom_point() +
57+
theme_bw())
58+
```
59+
60+
Other complete themes are: `theme_classic`, `theme_minimal`, `theme_light`, `theme_dark`.
61+
62+
## Axes
63+
64+
The appearance of the axis titles can be controlled with the global variable `axis_title`,
65+
or independently for x and y using `axis_title_x` and `axis_title_y`.
66+
67+
```{python}
68+
#| fig-align: center
69+
(ggplot(ind, aes('CPI', 'HDI', color='region')) +
70+
geom_jitter() +
71+
theme(axis_title=element_text(size=20, color='red')))
72+
```
73+
74+
Similar for `axis_text`:
75+
```{python}
76+
#| fig-align: center
77+
(ggplot(ind, aes('CPI', 'HDI', color='region')) +
78+
geom_jitter() +
79+
theme(axis_text=element_text(size=20, color='blue')))
80+
```
81+
82+
### Axis elements
83+
84+
The axis elements control the appearance of the axes:
85+
86+
Element | Setter | Description
87+
--------------------|-------------------|---------------------------
88+
axis_line | `element_line()` | line parallel to axis (hidden in default themes)
89+
axis_text | `element_text()` | tick labels
90+
axis_text_x | `element_text()` | x-axis tick labels
91+
axis_text_y | `element_text()` | y-axis tick labels
92+
axis_title | `element_text()` | axis titles
93+
axis_title_x | `element_text()` | x-axis title
94+
axis_title_y | `element_text()` | y-axis title
95+
axis_ticks | `element_line()` | axis tick marks
96+
axis_ticks_length | `unit()` | length of tick marks
97+
98+
## Plot title
99+
100+
The appearance of the plot's title can be controlled with the variable `plot_title`.
101+
```{python}
102+
#| fig-align: center
103+
(ggplot(ind, aes('CPI', 'HDI', color='region')) +
104+
geom_jitter() +
105+
ggtitle('CPI vs HDI') +
106+
theme(plot_title=element_text(size=20, weight='bold')))
107+
```
108+
109+
## Legend
110+
111+
The appearance of the legend can be controlled with `legend_text` and `legend_title`.
112+
```{python}
113+
#| fig-align: center
114+
base = (ggplot(ind, aes('CPI', 'HDI', color='region')) +
115+
geom_jitter())
116+
(base + theme(
117+
legend_text=element_text(size=15),
118+
legend_title=element_text(size=15, weight='bold'),
119+
figure_size=(5,3)))
120+
```
121+
122+
The legend elements control the appearance of all legends. You can also modify the appearance of individual legends by modifying the same elements in `guide_legend()` or `guide_colorbar()`.
123+
124+
Element | Setter | Description |
125+
--------------------|---------------------------|---------------------------------------------|
126+
legend_background | `element_rect()` | legend background |
127+
legend_key | `element_rect()` | background of legend keys |
128+
legend_key_size | `unit()` | legend key size |
129+
legend_key_height | `unit()` | legend key height |
130+
legend_key_width | `unit()` | legend key width |
131+
legend_margin | `unit()` | legend margin |
132+
legend_text | `element_text()` | legend labels |
133+
legend_text_align | 0--1 | legend label alignment (0 = right, 1 = left)|
134+
legend_title | `element_text()` | legend name |
135+
legend_title_align | 0--1 | legend name alignment (0 = right, 1 = left) |

0 commit comments

Comments
 (0)