Economic Policy Visualization
Visualization
October 21, 2024
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.5
✔ forcats 1.0.0 ✔ stringr 1.5.1
✔ ggplot2 3.5.1 ✔ tibble 3.2.1
✔ lubridate 1.9.3 ✔ tidyr 1.3.1
✔ purrr 1.0.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Bad:
Good:
Bad:
Good:
Bad:
Good:
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
Gestalt Principles describe how humans group similar elements, recognize patterns and simplify complex images. “Gestalt” is German for “unified whole”.
The question is how humans typically gain meaningful perceptions from the chaotic stimuli around them. The idea is that the mind “informs” what the eye sees by perceiving a series of individual elements as a whole.
Component | Function | Explanation |
---|---|---|
Data | ggplot(data) | The raw data that you want to visualise. |
Aesthetics | aes() | Aesthetic mappings between variables and visual properties. |
Geometries | geom_*() | The geometric shapes representing the data. |
Statistics | stat_*() | The statistical transformations applied to the data. |
Scales | scale_*() | Maps between the data and the aesthetic dimensions. |
Coordinate System | coord_*() | Maps data into the plane of the data rectangle. |
Facets | facet_*() | The arrangement of the data into a grid of plots. |
Visual Themes | theme() | The overall visual defaults of a plot. |
Geometries | Aesthetics | Scales | Dimensions |
---|---|---|---|
points | positions (x, y) | scale_x_*(), scale_y_*() | position (continuous, discrete, reverse, log10, sqrt, date) |
lines | colors (color, fill) | scale_color_*(), scale_fill_*() |
colors (continuous, discrete, manual, gradient, gradient2, brewer) |
polygons | shapes (shape, linetype) | scale_shape_*(), scale_linetype_*() | shapes (continuous, discrete, manual, ordinal) |
text | size (size) | scale_size_*(), scale_radius_*() | sizes (continuous, discrete, manual, ordinal, area, date) |
boxplot | transparency (alpha) | scale_alpha_*() | transparency (continuous, discrete, manual, ordinal, date) |
…many more! | groupings (group) |
data |> ggplot(aes(x = bill_length_mm,
y = bill_depth_mm,
color = species)) +
geom_point(size = 1.5, alpha = 0.5) +
scale_color_manual(values = MetBrewer::met.brewer("Lakota")) +
scale_x_continuous(limits = c(30,60), breaks = seq(30,60,10)) +
scale_y_continuous(limits = c(12,21), breaks = seq(12,21,3)) +
theme_minimal()
Warning: Removed 8 rows containing missing values or values outside the scale range
(`geom_point()`).
data |> ggplot(aes(x = bill_length_mm,
y = bill_depth_mm,
color = species)) +
geom_point(size = 1.5, alpha = 0.5) +
scale_color_manual(values = MetBrewer::met.brewer("Lakota")) +
scale_x_continuous(limits = c(30,60), breaks = seq(30,60,10)) +
scale_y_continuous(limits = c(12,21), breaks = seq(12,21,3)) +
labs(x = "Bill length (in mm)", y = "Bill depth (in mm)",
title = "Penguins are awesome",
subtitle = "Depth and length of bills") +
theme_minimal()
Warning: Removed 8 rows containing missing values or values outside the scale range
(`geom_point()`).
data |> ggplot(aes(x = bill_length_mm,
y = bill_depth_mm,
color = species)) +
geom_point(size = 1.5, alpha = 0.5) +
scale_color_manual(values = MetBrewer::met.brewer("Lakota")) +
scale_x_continuous(limits = c(30,60), breaks = seq(30,60,10)) +
scale_y_continuous(limits = c(12,21), breaks = seq(12,21,3)) +
annotate("text", x = c(34.7, 55.7, 50.7), y = c(20.7, 19, 13.6),
color = MetBrewer::met.brewer("Lakota")[1:3],
label = c("Adélie","Chinstrap","Gentoo"), fontface = "bold", size = 4) +
labs(x = "Bill length (in mm)", y = "Bill depth (in mm)",
title = "Penguins are awesome",
subtitle = "Depth and length of bills") +
theme_minimal() +
theme(legend.position = "none")
Warning: Removed 8 rows containing missing values or values outside the scale range
(`geom_point()`).
data |> ggplot(aes(x = bill_length_mm,
y = bill_depth_mm,
color = species)) +
geom_point(size = 1.5, alpha = 0.5) +
scale_color_manual(values = MetBrewer::met.brewer("Lakota")) +
scale_x_continuous(limits = c(30,60), breaks = seq(30,60,10)) +
scale_y_continuous(limits = c(12,21), breaks = seq(12,21,3)) +
annotate("text", x = c(34.7, 55.7, 50.7), y = c(20.7, 19, 13.6),
color = MetBrewer::met.brewer("Lakota")[1:3],
label = c("Adélie","Chinstrap","Gentoo"), fontface = "bold", size = 4) +
labs(x = "Bill length (in mm)", y = "Bill depth (in mm)",
title = "Penguins are awesome",
subtitle = "Depth and length of bills") +
theme_minimal() +
theme(legend.position = "none",
plot.title.position = "plot",
plot.title = element_text(size = 16, face="bold"),
plot.subtitle = element_text(size = 13),
panel.grid.minor = element_blank())
Warning: Removed 8 rows containing missing values or values outside the scale range
(`geom_point()`).
# A tibble: 3 × 5
species bill_length_mm_median bill_length_mm_sd bill_depth_mm_median
<fct> <dbl> <dbl> <dbl>
1 Adelie 38.8 2.66 18.4
2 Chinstrap 49.6 3.34 18.4
3 Gentoo 47.3 3.08 15
# ℹ 1 more variable: bill_depth_mm_sd <dbl>
data |> ggplot(aes(x = bill_length_mm,
y = bill_depth_mm,
color = species)) +
geom_point(size = 1.5, alpha = 0.5) +
geom_errorbar(
data = data_summary,
aes(x = bill_length_mm_median,
ymin = bill_depth_mm_median - bill_depth_mm_sd,
ymax = bill_depth_mm_median + bill_depth_mm_sd,
color = species,
color = after_scale(colorspace::darken(color, .2, space = "combined"))),
inherit.aes = FALSE, width = .5, size = .8) +
geom_errorbar(
data = data_summary,
aes(y = bill_depth_mm_median,
xmin = bill_length_mm_median - bill_length_mm_sd,
xmax = bill_length_mm_median + bill_length_mm_sd,
color = species,
color = after_scale(colorspace::darken(color, .2, space = "combined"))),
inherit.aes = FALSE, width = .25, size = .8) +
scale_color_manual(values = MetBrewer::met.brewer("Lakota")) +
scale_x_continuous(limits = c(30,60), breaks = seq(30,60,10)) +
scale_y_continuous(limits = c(12,21), breaks = seq(12,21,3)) +
annotate("text", x = c(34.7, 55.7, 50.7), y = c(20.7, 19, 13.6), color = MetBrewer::met.brewer("Lakota")[1:3], label = c("Adélie","Chinstrap","Gentoo"), fontface = "bold", size = 4) +
labs(x = "Bill length (in mm)", y = "Bill depth (in mm)",
title = "Penguins are awesome",
subtitle = "Depth and length of bills") +
theme_minimal() +
theme(legend.position = "none",
plot.title.position = "plot",
plot.title = element_text(size = 16, face="bold"),
plot.subtitle = element_text(size = 13),
panel.grid.minor = element_blank())
Warning: Duplicated aesthetics after name standardisation: colour
Duplicated aesthetics after name standardisation: colour
Warning: Removed 8 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Duplicated aesthetics after name standardisation: colour
Duplicated aesthetics after name standardisation: colour
Warning: Removed 8 rows containing missing values or values outside the scale range
(`geom_point()`).
PI 0750 Economic Policy (Applied track) | Winter term 2024