Skip to content

Commit

Permalink
hide gganimate
Browse files Browse the repository at this point in the history
  • Loading branch information
perlatex committed Dec 25, 2020
1 parent 7ac5558 commit 12a74ac
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 43 deletions.
2 changes: 1 addition & 1 deletion eda_nobel.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ df %>%


让图骚动起来吧
```{r eda-nobel-14}
```{r eda-nobel-14, eval=FALSE}
library(gganimate) # install.packages("gganimate", dependencies = T)
df %>%
Expand Down
2 changes: 2 additions & 0 deletions eda_vaccine_effectiveness.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ $$
library(tidyverse)
library(tidybayes)
library(rstan)
rstan_options(auto_write = FALSE)
options(mc.cores = 4)
```

然后,我们建立如下数学模型:
Expand Down
82 changes: 41 additions & 41 deletions ggplot2_gganimate.Rmd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ggplot2之让你的数据骚动起来 {#ggplot2-gganimate}


这节课,我们讲如何让我们的图动起来。
这节课,我们讲如何让我们的图动起来。(因为渲染需要花费很长时间,所以文档中的动图代码都没有执行。)


## 为什么要使用动图
Expand All @@ -21,15 +21,15 @@ install.packages("gganimate")


### 先来一张静态图
```{r ggplot2-gganimate-2}
```{r ggplot2-gganimate-2, eval=FALSE}
library(tidyverse)
library(covdata) # remotes::install_github("kjhealy/covdata")
library(gganimate)
```



```{r ggplot2-gganimate-3}
```{r ggplot2-gganimate-3, eval=FALSE}
covdata::covnat %>%
dplyr::filter(iso3 == "USA") %>%
dplyr::filter(cu_cases > 0) %>%
Expand All @@ -44,7 +44,7 @@ covdata::covnat %>%

让它动起来,我们只需要增加一行代码!

``` {r}
``` {r, eval=FALSE}
covdata::covnat %>%
dplyr::filter(iso3 == "USA") %>%
dplyr::filter(cu_cases > 0) %>%
Expand All @@ -60,23 +60,23 @@ covdata::covnat %>%

### 相对复杂点的例子

```{r ggplot2-gganimate-4}
```{r ggplot2-gganimate-4, eval=FALSE}
library(datasauRus)
ggplot(datasaurus_dozen) +
aes(x, y, color = dataset) +
geom_point()
```

用分面展示
```{r ggplot2-gganimate-5}
```{r ggplot2-gganimate-5, eval=FALSE}
ggplot(datasaurus_dozen) +
aes(x, y, color = dataset) +
geom_point() +
facet_wrap(~dataset)
```

可以用动图展示
```{r ggplot2-gganimate-6}
```{r ggplot2-gganimate-6, eval=FALSE}
ggplot(datasaurus_dozen) +
aes(x, y, color = dataset) +
geom_point() +
Expand Down Expand Up @@ -110,20 +110,20 @@ ggplot(datasaurus_dozen) +
### transition_states

- `transition_states(states = )`, 这里的参数states往往带有分组信息,可以等价于静态图中的分面。
```{r ggplot2-gganimate-7}
```{r ggplot2-gganimate-7, eval=FALSE}
diamonds %>%
ggplot(aes(carat, price)) +
geom_point()
```

```{r ggplot2-gganimate-8}
```{r ggplot2-gganimate-8, eval=FALSE}
diamonds %>%
ggplot(aes(carat, price)) +
geom_point() +
facet_wrap(vars(color))
```

```{r ggplot2-gganimate-9}
```{r ggplot2-gganimate-9, eval=FALSE}
diamonds %>%
ggplot(aes(carat, price)) +
geom_point() +
Expand All @@ -134,7 +134,7 @@ diamonds %>%

- `transition_time(time = )`, 这里的time一般认为是**连续**的值,相比于`transition_states`,没有了`transtion_length`这个选项,是因为`transtion_length`默认为time. 事实上,`transition_time``transition_states`的一种特例,但其实也有分组的要求

```{r ggplot2-gganimate-10}
```{r ggplot2-gganimate-10, eval=FALSE}
p <- gapminder::gapminder %>%
ggplot(aes(x = gdpPercap, y = lifeExp, size = pop, colour = country)) +
geom_point(alpha = 0.7, show.legend = FALSE) +
Expand All @@ -147,7 +147,7 @@ p <- gapminder::gapminder %>%
p
```

```{r ggplot2-gganimate-11}
```{r ggplot2-gganimate-11, eval=FALSE}
anim <- p +
transition_time(time = year) +
labs(title = "year: {frame_time}")
Expand All @@ -157,15 +157,15 @@ anim
### transition_reveal
- `transition_reveal(along = )`, along 这个词可以看出,它是按照某个变量**依次**显示的意思,比如顺着x轴显示

```{r ggplot2-gganimate-12}
```{r ggplot2-gganimate-12, eval=FALSE}
ggplot(data = economics) +
aes(x = date, y = unemploy) +
geom_line()
```



```{r ggplot2-gganimate-13}
```{r ggplot2-gganimate-13, eval=FALSE}
ggplot(economics) +
aes(x = date, y = unemploy) +
geom_line() +
Expand All @@ -178,7 +178,7 @@ ggplot(economics) +
### transition_filter

- `transition_filter( 至少2个筛选条件,transition_length = , filter_length =)`, 动图将会在这些筛选条件对应的子图之间转换
```{r ggplot2-gganimate-14}
```{r ggplot2-gganimate-14, eval=FALSE}
diamonds %>%
ggplot(aes(carat, price)) +
geom_point() +
Expand All @@ -196,7 +196,7 @@ diamonds %>%

- `transition_layers()`: 依次显示每个图层

```{r ggplot2-gganimate-15}
```{r ggplot2-gganimate-15, eval=FALSE}
mtcars %>%
ggplot(aes(mpg, disp)) +
geom_point() +
Expand Down Expand Up @@ -225,7 +225,7 @@ mtcars %>%

动画过程中,绘图窗口怎么变化呢?

```{r views-tbl, echo=FALSE}
```{r views-tbl, echo=FALSE, eval=FALSE}
tribble(
~Function, ~Description,
"view_follow", "完全跟随当前数据的范围",
Expand All @@ -238,7 +238,7 @@ tribble(

### view_follow

```{r ggplot2-gganimate-16}
```{r ggplot2-gganimate-16, eval=FALSE}
ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_point() +
labs(title = "{closest_state}") +
Expand All @@ -265,15 +265,15 @@ ggplot(iris, aes(Sepal.Length, Sepal.Width)) +

### shadow_wake()

```{r ggplot2-gganimate-17}
```{r ggplot2-gganimate-17, eval=FALSE}
p +
transition_time(time = year) +
labs(title = "year: {frame_time}") +
shadow_wake(wake_length = 0.1, alpha = FALSE)
```


```{r ggplot2-gganimate-18}
```{r ggplot2-gganimate-18, eval=FALSE}
ggplot(iris, aes(Petal.Length, Sepal.Length)) +
geom_point(size = 2) +
labs(title = "{closest_state}") +
Expand All @@ -282,14 +282,14 @@ ggplot(iris, aes(Petal.Length, Sepal.Length)) +
```

### shadow_trail()
```{r ggplot2-gganimate-19}
```{r ggplot2-gganimate-19, eval=FALSE}
p +
transition_time(time = year) +
labs(title = "year: {frame_time}") +
shadow_trail(distance = 0.1)
```

```{r ggplot2-gganimate-20}
```{r ggplot2-gganimate-20, eval=FALSE}
ggplot(iris, aes(Petal.Length, Sepal.Length)) +
geom_point(size = 2) +
labs(title = "{closest_state}") +
Expand All @@ -300,15 +300,15 @@ ggplot(iris, aes(Petal.Length, Sepal.Length)) +

### shadow_mark()

```{r ggplot2-gganimate-21}
```{r ggplot2-gganimate-21, eval=FALSE}
p +
transition_time(time = year) +
labs(title = "year: {frame_time}") +
shadow_mark(alpha = 0.3, size = 0.5)
```


```{r ggplot2-gganimate-22}
```{r ggplot2-gganimate-22, eval=FALSE}
ggplot(airquality, aes(Day, Temp)) +
geom_line(color = "red", size = 1) +
transition_time(Month) +
Expand All @@ -321,7 +321,7 @@ ggplot(airquality, aes(Day, Temp)) +

出现和退去的函数是成对的

```{r enter-exit-tbl, echo=FALSE}
```{r enter-exit-tbl, echo=FALSE, eval=FALSE}
tribble(
~Function, ~Description,
"enter_appear/exit_disappear", "突然出现,或者闪退",
Expand All @@ -338,7 +338,7 @@ tribble(

透明度上的变化,我这里用柱状图展示,效果要明显一点。

```{r ggplot2-gganimate-23}
```{r ggplot2-gganimate-23, eval=FALSE}
tibble(
x = month.name,
y = sample.int(12)
Expand All @@ -350,7 +350,7 @@ tibble(
```


```{r ggplot2-gganimate-24}
```{r ggplot2-gganimate-24, eval=FALSE}
tibble(
x = month.name,
y = sample.int(12)
Expand All @@ -364,7 +364,7 @@ tibble(
```


```{r ggplot2-gganimate-25}
```{r ggplot2-gganimate-25, eval=FALSE}
p +
transition_time(time = year) +
labs(title = "year: {frame_time}") +
Expand All @@ -375,7 +375,7 @@ p +

大小上的变化

```{r ggplot2-gganimate-26}
```{r ggplot2-gganimate-26, eval=FALSE}
tibble(
x = month.name,
y = sample.int(12)
Expand All @@ -389,7 +389,7 @@ tibble(
```


```{r ggplot2-gganimate-27}
```{r ggplot2-gganimate-27, eval=FALSE}
p +
transition_time(time = year) +
labs(title = "year: {frame_time}") +
Expand Down Expand Up @@ -418,15 +418,15 @@ Source: https://easings.net/


看下面的案例:
```{r ggplot2-gganimate-29}
```{r ggplot2-gganimate-29, eval=FALSE}
diamonds %>%
ggplot(aes(carat, price)) +
geom_point() +
transition_states(color, transition_length = 3, state_length = 1) +
ease_aes("cubic-in") # Change easing of all aesthetics
```

```{r ggplot2-gganimate-30}
```{r ggplot2-gganimate-30, eval=FALSE}
diamonds %>%
ggplot(aes(carat, price)) +
geom_point() +
Expand Down Expand Up @@ -558,7 +558,7 @@ anim_save("first_saved_animation.gif", animation = animation_to_save)

这是网上有段时间比较火的racing_bar图

```{r ggplot2-gganimate-33}
```{r ggplot2-gganimate-33, eval=FALSE}
ranked_by_date <- covdata::covnat %>%
group_by(date) %>%
arrange(date, desc(cu_cases)) %>%
Expand All @@ -568,7 +568,7 @@ ranked_by_date <- covdata::covnat %>%
```


```{r ggplot2-gganimate-34}
```{r ggplot2-gganimate-34, eval=FALSE}
ranked_by_date %>%
filter(date >= "2020-05-01") %>%
ggplot(
Expand Down Expand Up @@ -611,14 +611,14 @@ ranked_by_date %>%

## 案例演示二

```{r ggplot2-gganimate-35}
```{r ggplot2-gganimate-35, eval=FALSE}
bats <- readr::read_csv("./demo_data/bats-subset.csv") %>%
dplyr::mutate(id = factor(id))
```



```{r ggplot2-gganimate-36}
```{r ggplot2-gganimate-36, eval=FALSE}
bats %>%
ggplot(aes(
x = longitude,
Expand All @@ -634,7 +634,7 @@ bats %>%

### 常规的方法

```{r ggplot2-gganimate-37}
```{r ggplot2-gganimate-37, eval=FALSE}
bats %>%
ggplot(aes(
x = longitude,
Expand All @@ -652,7 +652,7 @@ bats %>%
- geom_path()是按照数据点出现的先后顺序
- geom_line()是按照数据点在x轴的顺序

```{r ggplot2-gganimate-38}
```{r ggplot2-gganimate-38, eval=FALSE}
bats %>%
ggplot(aes(
x = longitude,
Expand All @@ -671,7 +671,7 @@ bats %>%
<!-- 此时geom_path() + transition_reveal(time) = transition_time(time) -->


```{r ggplot2-gganimate-39}
```{r ggplot2-gganimate-39, eval=FALSE}
bats %>%
dplyr::mutate(
image = "images/bat-cartoon.png"
Expand All @@ -691,7 +691,7 @@ bats %>%
## 案例演示三

全球R-Ladies组织,会议活动的情况,我们在地图上用动图展示
```{r ggplot2-gganimate-40}
```{r ggplot2-gganimate-40, eval=FALSE}
rladies <- read_csv("./demo_data/rladies.csv")
rladies
```
Expand Down Expand Up @@ -835,7 +835,7 @@ bats %>%
# remove the objects
# rm(list=ls())
#rm(anim, bats, p, ranked_by_date, rladies, world, world_map)
rm(anim, bats, p, ranked_by_date, rladies)
#rm(anim, bats, p, ranked_by_date, rladies)
```


Expand Down
2 changes: 1 addition & 1 deletion index.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ options(
htmltools.dir.version = FALSE, formatR.indent = 2, width = 55, digits = 4
)
knitr::opts_chunk$set(cache = TRUE,
knitr::opts_chunk$set(
echo = TRUE,
warning = FALSE,
message = FALSE,
Expand Down

0 comments on commit 12a74ac

Please sign in to comment.