Skip to content

Commit

Permalink
add practice chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
perlatex committed Mar 6, 2021
1 parent b24ea69 commit 982fb12
Show file tree
Hide file tree
Showing 14 changed files with 2,325 additions and 15 deletions.
6 changes: 4 additions & 2 deletions _bookdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ rmd_files: [
"ggplot2_guides.Rmd",
"ggplot2_customize.Rmd",
"ggplot2_stat_layer.Rmd",
"ggplot2_colors.Rmd",
"tidyverse_workflow.Rmd",
"tidyverse_tips.Rmd",
"adv_dplyr.Rmd",
Expand Down Expand Up @@ -76,10 +77,11 @@ rmd_files: [
"stars.Rmd",
"rowwise.Rmd",
"ggplot2_academic.Rmd",
"ggplot2_colors.Rmd",

"ggplot2_gganimate.Rmd",
"lazyman.Rmd",

# "pandas.Rmd",
"practice.Rmd",

"Appendix.Rmd",
"references.Rmd"
Expand Down
1,465 changes: 1,465 additions & 0 deletions demo_data/tempnormals.csv

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion dplyr.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ df %>% select(!type)

## `filter() `

`select`是列方向的选择,我们还可以对数据行方向的选择和筛选,选出符合我们条件的某些行
`select`是列方向的选择,我们还可以对数据行方向的选择和筛选,选出符合我们条件的某些行^[
注意,这里filter()函数不是字面上“过滤掉”的意思,而是保留符合条件的行,也就说keep,不是drop的意思。 第一次会有一点点迷惑,我相信习惯就好了]

```{r dplyr-24, out.width = '65%', echo = FALSE}
knitr::include_graphics("images/dplyr-filter.png")
Expand Down
146 changes: 144 additions & 2 deletions forcats.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
## 创建因子
```{r forcats-1}
library(tidyverse)
library(palmerpenguins)
```

```{r forcats-2}
Expand Down Expand Up @@ -167,13 +168,154 @@ d %>%
```


```{r forcats-20, echo = F}

## 可视化中应用
可能没说明白,那就看企鹅柱状图吧
```{r forcats-20}
ggplot(penguins, aes(y = species)) +
geom_bar()
```


```{r forcats-21}
ggplot(penguins, aes(y = fct_rev(species))) +
geom_bar()
```





```{r forcats-22a, eval=FALSE}
penguins %>%
count(species) %>%
pull(species)
penguins %>%
count(species) %>%
mutate(species = fct_relevel(species, "Chinstrap", "Gentoo", "Adelie")) %>%
pull(species)
```



```{r forcats-22}
# Move "Chinstrap" in front, rest alphabetic
ggplot(penguins, aes(y = fct_relevel(species, "Chinstrap"))) +
geom_bar()
```



```{r forcats-23}
# Use order "Chinstrap", "Gentoo", "Adelie"
ggplot(penguins, aes(y = fct_relevel(species, "Chinstrap", "Gentoo", "Adelie"))) +
geom_bar()
```


```{r forcats-24}
penguins %>%
mutate(species = fct_relevel(species, "Chinstrap", "Gentoo", "Adelie")) %>%
ggplot(aes(y = species)) +
geom_bar()
```


```{r forcats-25}
ggplot(penguins, aes(y = fct_relevel(species, "Adelie", after = Inf))) +
geom_bar()
```


```{r forcats-26}
# Use the order defined by the number of penguins of different species
# The order is descending, from most frequent to least frequent
penguins %>%
mutate(species = fct_infreq(species)) %>%
ggplot(aes(y = species)) +
geom_bar()
```


```{r forcats-27}
penguins %>%
mutate(species = fct_rev(fct_infreq(species))) %>%
ggplot(aes(y = species)) +
geom_bar()
```


```{r forcats-28}
# Reorder based on numeric values
penguins %>%
count(species) %>%
mutate(species = fct_reorder(species, n)) %>%
ggplot(aes(n, species)) +
geom_col()
```

## 作业

- 画出的2007年美洲人口寿命的柱状图,要求从高到低排序
```{r forcats-29}
library(gapminder)
gapminder %>%
filter(
year == 2007,
continent == "Americas"
)
```


```{r forcats-30, eval=FALSE, echo = FALSE}
gapminder %>%
filter( year == 2007, continent == "Americas") %>%
mutate( country = fct_reorder(country, lifeExp)) %>%
ggplot(aes(lifeExp, country)) +
geom_point()
```

- 这是四个国家人口寿命的变化图
```{r forcats-31}
gapminder %>%
filter(country %in% c("Norway", "Portugal", "Spain", "Austria")) %>%
ggplot(aes(year, lifeExp)) + geom_line() +
facet_wrap(vars(country), nrow = 1)
```

- 要求给四个分面排序,按每个国家寿命的中位数
```{r forcats-32, eval=FALSE, echo = FALSE}
gapminder %>%
filter(country %in% c("Norway", "Portugal", "Spain", "Austria")) %>%
mutate(country = fct_reorder(country, lifeExp)) %>% # default: order by median
ggplot(aes(year, lifeExp)) + geom_line() +
facet_wrap(vars(country), nrow = 1)
```



- 要求给四个分面排序,按每个国家寿命差(最大值减去最小值)

```{r forcats-33, eval=FALSE, echo = FALSE}
gapminder %>%
filter(country %in% c("Norway", "Portugal", "Spain", "Austria")) %>%
# order by custom function: here, difference between max and min
mutate(country = fct_reorder(country, lifeExp, function(x) { max(x) - min(x) })) %>%
ggplot(aes(year, lifeExp)) + geom_line() +
facet_wrap(vars(country), nrow = 1)
```



```{r forcats-34, echo = F}
# remove the objects
# rm(list=ls())
rm(d, income, x)
```


```{r forcats-21, echo = F, message = F, warning = F, results = "hide"}
```{r forcats-35, echo = F, message = F, warning = F, results = "hide"}
pacman::p_unload(pacman::p_loaded(), character.only = TRUE)
```
137 changes: 136 additions & 1 deletion ggplot2_colors.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,28 @@ knitr::include_graphics("images/hcl-palettes-principles.png")
三种类型对应着三个函数 `qualitative_hcl()`, `sequential_hcl()`, 和 `diverging_hcl()`.



## 配色模板

```{r ggplot2-colors-4, out.width = '100%'}
hcl_palettes(plot = TRUE)
```

## 使用案例
**colorspace** 模板使用起来很方便,有统一格式`scale_<aesthetic>_<datatype>_<colorscale>()`,

- 这里 `<aesthetic>` 是指定映射 (`fill`, `color`, `colour`),
- 这里 `<datatype>` 是表明数据变量的类型 (`discrete`, `continuous`, `binned`),
- 这里 `colorscale` 是声明颜色标度类型 (`qualitative`, `sequential`, `diverging`, `divergingx`).


|Scale function | Aesthetic | Data type | Palette type |
|:----------- | :-------- | :--------- | :------------|
|`scale_color_discrete_qualitative()` | `color` | discrete | qualitative |
|`scale_fill_continuous_sequential()` | `fill` | continuous | sequential |
|`scale_colour_continous_divergingx()` | `colour` | continuous | diverging |


## 使用案例1

ggplot2默认
```{r ggplot2-colors-5}
Expand Down Expand Up @@ -67,6 +82,126 @@ penguins %>%
```


## 使用案例2


```{r ggplot2-colors-8}
temps_months <- read_csv("./demo_data/tempnormals.csv") %>%
group_by(location, month_name) %>%
summarize(mean = mean(temperature)) %>%
mutate(
month = factor(
month_name,
levels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
),
location = factor(
location, levels = c("Death Valley", "Houston", "San Diego", "Chicago")
)
) %>%
select(-month_name)
temps_months
```





```{r ggplot2-colors-9}
temps_months %>%
ggplot(aes(x = month, y = location, fill = mean)) +
geom_tile(width = 0.95, height = 0.95) +
coord_fixed(expand = FALSE) +
theme_classic()
```


```{r ggplot2-colors-10}
temps_months %>%
ggplot(aes(x = month, y = location, fill = mean)) +
geom_tile(width = 0.95, height = 0.95) +
coord_fixed(expand = FALSE) +
theme_classic() +
scale_fill_gradient()
```


```{r ggplot2-colors-11}
temps_months %>%
ggplot(aes(x = month, y = location, fill = mean)) +
geom_tile(width = 0.95, height = 0.95) +
coord_fixed(expand = FALSE) +
theme_classic() +
scale_fill_viridis_c()
```


```{r ggplot2-colors-12}
temps_months %>%
ggplot(aes(x = month, y = location, fill = mean)) +
geom_tile(width = 0.95, height = 0.95) +
coord_fixed(expand = FALSE) +
theme_classic() +
scale_fill_viridis_c(option = "B", begin = 0.15)
```


```{r ggplot2-colors-13}
temps_months %>%
ggplot(aes(x = month, y = location, fill = mean)) +
geom_tile(width = 0.95, height = 0.95) +
coord_fixed(expand = FALSE) +
theme_classic() +
scale_fill_continuous_sequential(palette = "YlGnBu", rev = FALSE)
```


```{r ggplot2-colors-14}
temps_months %>%
ggplot(aes(x = month, y = location, fill = mean)) +
geom_tile(width = 0.95, height = 0.95) +
coord_fixed(expand = FALSE) +
theme_classic() +
scale_fill_continuous_sequential(palette = "Viridis", rev = FALSE)
```


```{r ggplot2-colors-15}
temps_months %>%
ggplot( aes(x = month, y = location, fill = mean)) +
geom_tile(width = 0.95, height = 0.95) +
coord_fixed(expand = FALSE) +
theme_classic() +
scale_fill_continuous_sequential(palette = "Inferno", begin = 0.15, rev = FALSE)
```


```{r ggplot2-colors-16}
temps_months %>%
ggplot(aes(x = month, y = location, fill = mean)) +
geom_tile(width = 0.95, height = 0.95) +
coord_fixed(expand = FALSE) +
theme_classic() +
scale_fill_continuous_sequential(palette = "Plasma", begin = 0.35, rev = FALSE)
```

## 配色

```{block ggplot2-colors-17, type="danger"}
这里有个小小的提示:
- 尽可能不避免使用`"red"`, `"green"`, `"blue"`, `"cyan"`, `"magenta"`, `"yellow"`颜色
- 使用相对柔和的颜色`"firebrick"`, `"springgreen4"`, `"blue3"`, `"turquoise3"`, `"darkorchid2"`, `"gold2"`,会让人觉得舒服
```


可以对比下
```{r ggplot2-colors-18}
colorspace::swatchplot(c("red", "green", "blue", "cyan", "magenta", "yellow"))
colorspace::swatchplot(c("firebrick", "springgreen4", "blue3", "turquoise3", "darkorchid2", "gold2"))
```


## color-wheel

Expand Down
2 changes: 1 addition & 1 deletion ggplot2_gganimate.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ transition_reveal(along = ) +
### Renderer options

```{r renderer-tbl, echo=FALSE}
tribble(
tibble::tribble(
~Function, ~Description,
"gifski_renderer", "Default, super fast gif renderer.",
"magick_renderer", "Somewhat slower gif renderer.",
Expand Down
5 changes: 5 additions & 0 deletions ggplot2_scales.Rmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# ggplot2之标度 {#ggplot2-scales}

> 用 ggplot2 画图,有种恋爱的感觉:
> “你懂我的图谋不轨,我懂你的故作矜持”


这一章我们一起学习ggplot2中的scales语法,推荐大家阅读Hadley Wickham最新版的[《ggplot2: Elegant Graphics for Data Analysis》](https://ggplot2-book.org/),但如果需要详细了解**标度**参数体系,还是要看[ggplot2官方文档](https://cran.r-project.org/web/packages/ggplot2/index.html)


Expand Down
Binary file added images/make_data_tidy3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/to_reproduce.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 982fb12

Please sign in to comment.