Add ggplot2 elements to cheat sheet.
This commit is contained in:
@@ -6,12 +6,69 @@ description: >
|
||||
and maybe it is helpful for you, too, internet wanderer.
|
||||
date: 2024-11-05T08:00:00+0100
|
||||
draft: false
|
||||
toc: true
|
||||
---
|
||||
{{< git-info >}}
|
||||
{{< param "description" >}}
|
||||
|
||||
## R
|
||||
|
||||
## ggplot2 elements
|
||||
|
||||
I can never remember which part of a [ggplot2][] graph/figure is the "plot"
|
||||
and which is the "panel", so I made this little figure:
|
||||
|
||||
{{< figure src="/img/ggplot2-elements.png" >}}
|
||||
|
||||
The figure was generated with this code:
|
||||
|
||||
```r
|
||||
library(tidyverse)
|
||||
tibble(x = 1:3, y = 1:3) |>
|
||||
ggplot(aes(x = x, y = y)) +
|
||||
geom_col(fill = "white") +
|
||||
geom_text(
|
||||
label = "The green area is the PANEL BACKGROUND",
|
||||
x = 0.5,
|
||||
y = 3.5,
|
||||
hjust = 0,
|
||||
color = "dark green",
|
||||
fontface = "bold"
|
||||
) +
|
||||
geom_text(
|
||||
label = "The blue border at the top and the right is the PLOT BORDER",
|
||||
x = 0.5,
|
||||
y = 3.2,
|
||||
hjust = 0,
|
||||
color = "navy",
|
||||
fontface = "bold"
|
||||
) +
|
||||
geom_text(
|
||||
label = "The gray area is the PLOT BACKGROUND",
|
||||
x = 0.5,
|
||||
y = -0.5,
|
||||
hjust = 0,
|
||||
color = "black",
|
||||
fontface = "bold"
|
||||
) +
|
||||
coord_cartesian(clip = "off") +
|
||||
scale_x_continuous(expand = expansion(), limits = c(0, 4)) +
|
||||
scale_y_continuous(expand = expansion(), limits = c(0, 4)) +
|
||||
theme(
|
||||
panel.border = element_rect(fill = "light blue", color = "blue"),
|
||||
panel.background = element_rect(fill = "light green", color = "green"),
|
||||
plot.background = element_rect(fill = "gray", color = "black"),
|
||||
plot.margin = unit(rep(0.1, 4), units = "npc")
|
||||
)
|
||||
ggsave(
|
||||
"ggplot2-elements.png",
|
||||
width = 9,
|
||||
height = 6,
|
||||
units = "in",
|
||||
dpi = 96
|
||||
)
|
||||
```
|
||||
|
||||
### Print R objects so that they can be re-generated from code
|
||||
|
||||
```r
|
||||
@@ -101,3 +158,5 @@ starwars |>
|
||||
### Human interaction with IMAP servers
|
||||
|
||||
<https://geekmungus.co.uk/?p=4358>
|
||||
|
||||
[ggplot2]: https://ggplot2.tidyverse.org
|
||||
BIN
static/img/ggplot2-elements.png
Normal file
BIN
static/img/ggplot2-elements.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 18 KiB |
Reference in New Issue
Block a user