Skip to content

Commit

Permalink
add reference
Browse files Browse the repository at this point in the history
  • Loading branch information
perlatex committed Jan 9, 2021
1 parent 9635899 commit 7a09f9c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
59 changes: 56 additions & 3 deletions ggplot2_customize.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -639,18 +639,71 @@ ggplot(mpg, aes(x = displ, y = hwy)) +



## 练习

重复这张压平曲线(flatten curve)图

```{r ggplot2-customize-37, out.width = '80%', echo = FALSE}
knitr::include_graphics("images/flatten_curve_CDC.jpg")
```



```{r ggplot2-customize-38, eval=FALSE, include=FALSE}
# way one, simulate data
library(tidyverse)
high <- rnorm(1e5, mean = 12, sd = 4)
flat <- rnorm(1e5, mean = 35, sd = 12)
df <- tibble(
dist = c(rep("high", 1e5), rep("flat", 1e5)),
x = c(high, flat)
)
df %>%
ggplot(aes(x = x, color = dist)) +
geom_density() +
scale_y_continuous(expand = expansion(mult = c(0, NA))) +
scale_color_manual(
name = "分布",
values = c("high" = "tomato", "flat" = "dodgerblue"),
labels = c("high" = "分布1", "flat" = "分布2")
) +
theme_minimal() +
labs(x = "Days since the first case",
title = "Slow Down the Spread of COVID-19",
subtitle = "Practicing Social distancing can slow the spread of disease, which can prevent the overcrowding of hospitals")
# way two, using ggplot2::stat_function()
ggplot() +
stat_function(fun = dnorm,
args = list(mean = 12, sd = 4),
color = "red"
) +
stat_function(fun = dnorm,
args = list(mean = 35, sd = 12),
color = "dodgerblue"
) +
xlim(-5, 90)
```




```{r ggplot2-customize-37, echo = F}
```{r ggplot2-customize-39, echo = F}
# remove the objects
# rm(list=ls())
rm(central_park, d, drop_facet, gapdata, label_colors, myfun, nyc_squirrels, p1, p2, p3, p4, p5, p6, p7, p8, p9, pp, g1, g2)
rm(central_park, d, drop_facet, gapdata, label_colors, myfun, nyc_squirrels, p1, p2, p3, p4, p5, p6, p7, p8, p9, pp, g1, g2, df)
```

```{r ggplot2-customize-38, echo = F, message = F, warning = F, results = "hide"}
```{r ggplot2-customize-40, echo = F, message = F, warning = F, results = "hide"}
pacman::p_unload(pacman::p_loaded(), character.only = TRUE)
```
Binary file added images/flatten_curve_CDC.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ account: wangminjie
server: bookdown.org
hostUrl: https://bookdown.org/__api__
appId: 3039
bundleId: 32802
bundleId: 32804
url: https://bookdown.org/wangminjie/R4DS/
when: 1610165897.60778
when: 1610171499.94465

0 comments on commit 7a09f9c

Please sign in to comment.