Skip to content

Commit

Permalink
Merge pull request #308 from thomasp85/patch-1
Browse files Browse the repository at this point in the history
A few fixes to the style etc of the ggplot2 cheatsheet
  • Loading branch information
mine-cetinkaya-rundel authored Jul 26, 2023
2 parents cb02b6d + 119d132 commit e2796e1
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 34 deletions.
4 changes: 2 additions & 2 deletions _freeze/html/data-visualization/execute-results/html.json

Large diffs are not rendered by default.

Binary file modified data-visualization.pdf
Binary file not shown.
60 changes: 28 additions & 32 deletions html/data-visualization.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@ Common aesthetic values.

- `linetype`: Integer or string (0 = `"blank"`, 1 = `"solid"`, 2 = `"dashed"`, 3 = `"dotted"`, 4 = `"dotdash"`, 5 = `"longdash"`, 6 = `"twodash"`).

- `lineend`: String (`"round"`, `"butt"`, or `"square"`).

- `linejoin`: String (`"round"`, `"mitre"`, or `"bevel"`).

- `size`: Integer (line width in mm for outlines).

- `linewidth`: Integer (line width in mm for lines).
Expand Down Expand Up @@ -172,7 +168,7 @@ e <- ggplot(mpg, aes(cty, hwy))
- `e + geom_label(aes(label = cty), nudge_x = 1, nudge_y = 1)`: Add text with a rectangle background.
`aes()` arguments: - `x`, `y`, `label`, `alpha`, `angle`, `color`, `family`, `fontface`, `hjust`, `lineheight`, `size`, `vjust`.

- ``` e +``geom_point``() ```: Draw a scatter plot.
- `e + geom_point()`: Draw a scatter plot.
`aes()` arguments: `x`, `y`, `alpha`, `color`, `fill`, `shape`, `size`, `stroke`.

- `e + geom_quantile()`: Fit and draw quantile regression for the plot data.
Expand Down Expand Up @@ -307,50 +303,50 @@ An alternative way to build a layer.
A stat builds new variables to plot (e.g., count, prop).

Visualize a stat by changing the default stat of a geom function, `geom_bar(stat = "count")`, or by using a stat function, `stat_count(geom = "bar")`, which calls a default geom to make a layer (equivalent to a geom function).
Use `..name..` syntax to map stat variables to aesthetics.
Use `after_stat(name)` syntax to map the stat variable `name` to an aesthetic.

```{r}
#| eval: false
i + stat_density_2d(aes(fill = ..level..), geom = "polygon")
i + stat_density_2d(aes(fill = after_stat(level)), geom = "polygon")
```

In this example, `"polygon"` is the geom to use, `stat_density_2d()` is the stat function, `aes()` contains the geom mappings, and `..level..` is the variable created by stat.
In this example, `"polygon"` is the geom to use, `stat_density_2d()` is the stat function, `aes()` contains the geom mappings, and `level` is the variable created by stat.

- `c + stat_bin(binwidth = 1, boundary = 10)`: `x`, `y` \| `..count..`, `..ncount..`, `..density..`, `..ndensity..`
- `c + stat_bin(binwidth = 1, boundary = 10)`: `x`, `y` \| `count`, `ncount`, `density`, `ndensity`

- `c + stat_count(width = 1)`: `x`, `y` \| `..count..`, `..density..`
- `c + stat_count(width = 1)`: `x`, `y` \| `count`, `density`

- `c + stat_density(adjust = 1, kernel = "gaussian")`: `x`, `y` \| `..count..`, `..density..`, `..scaled..`
- `c + stat_density(adjust = 1, kernel = "gaussian")`: `x`, `y` \| `count`, `density`, `scaled`

- `e + stat_bin_2d(bins = 30, drop = T)`: `x`, `y`, `fill` \| `..count..`, `..density..`
- `e + stat_bin_2d(bins = 30, drop = T)`: `x`, `y`, `fill` \| `count`, `density`

- `e + stat_bin_hex(bins =30)`: `x`, `y`, `fill` \| `..count..`, `..density..`
- `e + stat_bin_hex(bins =30)`: `x`, `y`, `fill` \| `count`, `density`

- `e + stat_density_2d(contour = TRUE, n = 100)`: `x`, `y`, `color`, `linewidth` \| `..level..`
- `e + stat_density_2d(contour = TRUE, n = 100)`: `x`, `y`, `color`, `linewidth` \| `level`

- `e + stat_ellipse(level = 0.95, segments = 51, type = "t")`

- `l + stat_contour(aes(z = z))`: `x`, `y`, `z`, `order` \| `..level..`
- `l + stat_contour(aes(z = z))`: `x`, `y`, `z`, `order` \| `level`

- `l + stat_summary_hex(aes(z = z), bins = 30, fun = max)`: `x`, `y`, `z`, `fill` \| `..value..`
- `l + stat_summary_hex(aes(z = z), bins = 30, fun = max)`: `x`, `y`, `z`, `fill` \| `value`

- `l + stat_summary_2d(aes(z = z), bins = 30, fun = mean)`: `x`, `y`, `z`, `fill` \| `..value..`
- `l + stat_summary_2d(aes(z = z), bins = 30, fun = mean)`: `x`, `y`, `z`, `fill` \| `value`

- `f + stat_boxplot(coef = 1.5)`: `x`, `y` \| `..lower..`, `..middle..`, `..upper..`, `..width..`, `..ymin..`, `..ymax..`
- `f + stat_boxplot(coef = 1.5)`: `x`, `y` \| `lower`, `middle`, `upper`, `width`, `ymin`, `ymax`

- `f + stat_ydensity(kernel = "gaussian", scale = "area")`: `x`, `y` \| `..density..`, `..scaled..`, `..count..`, `..n..`, `..violinwidth..`, `..width..`
- `f + stat_ydensity(kernel = "gaussian", scale = "area")`: `x`, `y` \| `density`, `scaled`, `count`, `n`, `violinwidth`, `width`

- `e + stat_ecdf(n = 40)`: `x`, `y` \| `..x..`, `..y..`
- `e + stat_ecdf(n = 40)`: `x`, `y` \| `x`, `y`

- `e + stat_quantile(quantiles = c(0.1, 0.9), formula = y ~ log(x), method = "rq")`: `x`, `y` \| `..quantile..`
- `e + stat_quantile(quantiles = c(0.1, 0.9), formula = y ~ log(x), method = "rq")`: `x`, `y` \| `quantile`

- `e + stat_smooth(method = "lm", formula = y ~ x, se = T, level = 0.95)`: `x`, `y` \| `..se..`, `..x..`, `..y..`, `..ymin..`, `..ymax..`
- `e + stat_smooth(method = "lm", formula = y ~ x, se = T, level = 0.95)`: `x`, `y` \| `se`, `x`, `y`, `ymin`, `ymax`

- `ggplot() + xlim(-5, 5) + stat_function(fun = dnorm, n = 20, geom = "point")`: `x` \| `..x..`, `..y..`
- `ggplot() + xlim(-5, 5) + stat_function(fun = dnorm, n = 20, geom = "point")`: `x` \| `x`, `y`

- `ggplot() + stat_qq(aes(sample = 1:100))`: `x`, `y`, `sample` \| `..sample..`, `..theoretical..`
- `ggplot() + stat_qq(aes(sample = 1:100))`: `x`, `y`, `sample` \| `sample`, `theoretical`

- `e + stat_sum()`: `x`, `y`, `size` \| `..n..`, `..prop..`
- `e + stat_sum()`: `x`, `y`, `size` \| `n`, `prop`

- `e + stat_summary(fun.data = "mean_cl_boot")`

Expand Down Expand Up @@ -536,22 +532,22 @@ Facets divide a plot into subplots based on the values of one or more discrete v
t <- ggplot(mpg, aes(cty, hwy)) + geom_point()
```

- `t + facet_grid(cols = vars(fl))`: Facet into a column based on fl.
- `t + facet_grid(. ~ fl)`: Facet into a column based on fl.

- `t + facet_grid(rows = vars(year))`: Facet into rows based on year.
- `t + facet_grid(year ~ .)`: Facet into rows based on year.

- `t + facet_grid(rows = vars(year), cols = vars(fl))`: Facet into both rows and columns.
- `t + facet_grid(year ~ fl)`: Facet into both rows and columns.

- `t_facet_wrap(vars(fl))`: Wrap facets into a rectangular layout.
- `t + facet_wrap(~ fl)`: Wrap facets into a rectangular layout.

- `t + facet_grid(rows = vars(drv), cols = vars(fl), scales = "free")`: Set **scales** to let axis limits vary across facets.
- `t + facet_grid(drv ~ fl, scales = "free")`: Set **scales** to let axis limits vary across facets.
Also `"free_x"` for x axis limits adjust to individual facets and `"free_y"` for y axis limits adjust to individual facets.

Set **labeller** to adjust facet label:

- `t + facet_grid(cols = vars(fl), labeller = label_both)`: Labels each facet as "fl: c", "fl: d", etc.
- `t + facet_grid(. ~ fl, labeller = label_both)`: Labels each facet as "fl: c", "fl: d", etc.

- `t + facet_grid(rows = vars(fl), labeller = label_bquote(alpha ^ .(fl)))`: Labels each facet as "𝛼^c^", "𝛼^d^", etc.
- `t + facet_grid(fl ~ ., labeller = label_bquote(alpha ^ .(fl)))`: Labels each facet as "𝛼^c^", "𝛼^d^", etc.

## Labels and Legends

Expand Down
Binary file modified html/images/logo-ggplot2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified html/images/logo-purrr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified keynotes/data-visualization.key
Binary file not shown.

0 comments on commit e2796e1

Please sign in to comment.