2025-02-10
Party composition of the 8th German Bundestag, 1976–1980
Inspired by: https://en.wikipedia.org/wiki/File:Piecharts.svg
Inspired by: https://en.wikipedia.org/wiki/File:Piecharts.svg
Inspired by: https://en.wikipedia.org/wiki/File:Piecharts.svg
Inspired by: https://en.wikipedia.org/wiki/File:Piecharts.svg
Change in the gender composition of the Rwandan parliament from 1997 to 2016
Pie chart | Stacked bars | Side-by-side bars | |
---|---|---|---|
Allows easy comparison of relative proportions | ✖ | ✖ | ✔ |
Pie chart | Stacked bars | Side-by-side bars | |
---|---|---|---|
Allows easy comparison of relative proportions | ✖ | ✖ | ✔ |
Shows data as proportions of a whole | ✔ | ✔ | ✖ |
Pie chart | Stacked bars | Side-by-side bars | |
---|---|---|---|
Allows easy comparison of relative proportions | ✖ | ✖ | ✔ |
Shows data as proportions of a whole | ✔ | ✔ | ✖ |
Emphasizes simple fractions (1/2, 1/3, …) | ✔ | ✖ | ✖ |
Pie chart | Stacked bars | Side-by-side bars | |
---|---|---|---|
Allows easy comparison of relative proportions | ✖ | ✖ | ✔ |
Shows data as proportions of a whole | ✔ | ✔ | ✖ |
Emphasizes simple fractions (1/2, 1/3, …) | ✔ | ✖ | ✖ |
Visually appealing for small datasets | ✔ | ✖ | ✔ |
Pie chart | Stacked bars | Side-by-side bars | |
---|---|---|---|
Allows easy comparison of relative proportions | ✖ | ✖ | ✔ |
Shows data as proportions of a whole | ✔ | ✔ | ✖ |
Emphasizes simple fractions (1/2, 1/3, …) | ✔ | ✖ | ✖ |
Visually appealing for small datasets | ✔ | ✖ | ✔ |
Works well for a large number of subsets | ✖ | ✖ | ✔ |
Pie chart | Stacked bars | Side-by-side bars | |
---|---|---|---|
Allows easy comparison of relative proportions | ✖ | ✖ | ✔ |
Shows data as proportions of a whole | ✔ | ✔ | ✖ |
Emphasizes simple fractions (1/2, 1/3, …) | ✔ | ✖ | ✖ |
Visually appealing for small datasets | ✔ | ✖ | ✔ |
Works well for a large number of subsets | ✖ | ✖ | ✔ |
Works well for time series and similar | ✖ | ✔ | ✖ |
No one visualization fits all scenarios!
Dataset: Bridges in Pittsburgh by construction material and era of construction
Dataset: Bridges in Pittsburgh by construction material and era of construction
Dataset: Land surface area of US states
Dataset: Bridges in Pittsburgh by construction material and era of construction
Dataset: Bridges in Pittsburgh by construction material and era of construction
Dataset: Bridges in Pittsburgh by construction material and era of construction
We have three options:
geom_bar()
/geom_col()
with poolar coordinatesgeom_arc_bar()
with stat_pie()
geom_arc_bar()
with manual computation# the data
bundestag <- tibble(
party = c("CDU/CSU", "SPD", "FDP"),
seats = c(243, 214, 39)
)
# make bar chart in polar coords
ggplot(bundestag) +
aes(seats, "YY", fill = party) +
geom_col() +
coord_polar() +
scale_x_continuous(
name = NULL, breaks = NULL
) +
scale_y_discrete(
name = NULL, breaks = NULL
) +
ggtitle("German Bundestag 1976-1980")
library(ggforce)
ggplot(bundestag) +
aes(
x0 = 0, y0 = 0, # position of pie center
r0 = 0, r = 1, # inner and outer radius
amount = seats, # size of pie slices
fill = party
) +
geom_arc_bar(stat = "pie") +
coord_fixed( # make pie perfectly circular
# adjust limits as needed
xlim = c(-1, 3), ylim = c(-1, 3)
)
library(ggforce)
ggplot(bundestag) +
aes(
x0 = 1, y0 = 1, # position of pie center
r0 = 0, r = 1, # inner and outer radius
amount = seats, # size of pie slices
fill = party
) +
geom_arc_bar(stat = "pie") +
coord_fixed( # make pie perfectly circular
# adjust limits as needed
xlim = c(-1, 3), ylim = c(-1, 3)
)
library(ggforce)
ggplot(bundestag) +
aes(
x0 = 1, y0 = 1, # position of pie center
r0 = 1, r = 2, # inner and outer radius
amount = seats, # size of pie slices
fill = party
) +
geom_arc_bar(stat = "pie") +
coord_fixed( # make pie perfectly circular
# adjust limits as needed
xlim = c(-1, 3), ylim = c(-1, 3)
)
# prepare pie data
pie_data <- bundestag |>
arrange(seats) |> # sort so pie slices end up sorted
mutate(
end_angle = 2*pi*cumsum(seats)/sum(seats), # ending angle for each pie slice
start_angle = lag(end_angle, default = 0) # starting angle for each pie slice
)
pie_data
# A tibble: 3 × 4
party seats end_angle start_angle
<chr> <dbl> <dbl> <dbl>
1 FDP 39 0.494 0
2 SPD 214 3.20 0.494
3 CDU/CSU 243 6.28 3.20
# prepare pie data
pie_data <- bundestag |>
arrange(seats) |> # sort so pie slices end up sorted
mutate(
end_angle = 2*pi*cumsum(seats)/sum(seats), # ending angle for each pie slice
start_angle = lag(end_angle, default = 0), # starting angle for each pie slice
mid_angle = 0.5*(start_angle + end_angle), # middle of each pie slice, for text labels
)
pie_data
# A tibble: 3 × 5
party seats end_angle start_angle mid_angle
<chr> <dbl> <dbl> <dbl> <dbl>
1 FDP 39 0.494 0 0.247
2 SPD 214 3.20 0.494 1.85
3 CDU/CSU 243 6.28 3.20 4.74
# prepare pie data
pie_data <- bundestag |>
arrange(seats) |> # sort so pie slices end up sorted
mutate(
end_angle = 2*pi*cumsum(seats)/sum(seats), # ending angle for each pie slice
start_angle = lag(end_angle, default = 0), # starting angle for each pie slice
mid_angle = 0.5*(start_angle + end_angle), # middle of each pie slice, for text labels
# horizontal and vertical justifications for outer labels
hjust = if_else(mid_angle > pi, 1, 0),
vjust = if_else(mid_angle < pi/2 | mid_angle > 3*pi/2, 0, 1)
)
pie_data
# A tibble: 3 × 7
party seats end_angle start_angle mid_angle hjust vjust
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 FDP 39 0.494 0 0.247 0 0
2 SPD 214 3.20 0.494 1.85 0 1
3 CDU/CSU 243 6.28 3.20 4.74 1 0
ggplot(pie_data) +
aes(
x0 = 0, y0 = 0, r0 = 0, r = 1,
start = start_angle, end = end_angle,
fill = party
) +
geom_arc_bar() +
geom_text( # place amounts inside the pie
aes(
x = 0.6 * sin(mid_angle),
y = 0.6 * cos(mid_angle),
label = seats
)
) +
geom_text( # place party name outside the pie
aes(
x = 1.05 * sin(mid_angle),
y = 1.05 * cos(mid_angle),
label = party,
hjust = hjust, vjust = vjust
)
) +
coord_fixed()
ggplot(pie_data) +
aes(
x0 = 0, y0 = 0, r0 = 0, r = 1,
start = start_angle, end = end_angle,
fill = party
) +
geom_arc_bar() +
geom_text( # place amounts inside the pie
aes(
x = 0.6 * sin(mid_angle),
y = 0.6 * cos(mid_angle),
label = seats
)
) +
geom_text( # place party name outside the pie
aes(
x = 1.05 * sin(mid_angle),
y = 1.05 * cos(mid_angle),
label = party,
hjust = hjust, vjust = vjust
)
) +
coord_fixed(
xlim = c(-1.8, 1.3)
)
ggplot(pie_data) +
aes(
x0 = 0, y0 = 0, r0 = 0.4, r = 1,
start = start_angle, end = end_angle,
fill = party
) +
geom_arc_bar() +
geom_text( # place amounts inside the pie
aes(
x = 0.7 * sin(mid_angle),
y = 0.7 * cos(mid_angle),
label = seats
)
) +
geom_text( # place party name outside the pie
aes(
x = 1.05 * sin(mid_angle),
y = 1.05 * cos(mid_angle),
label = party,
hjust = hjust, vjust = vjust
)
) +
coord_fixed(
xlim = c(-1.8, 1.3)
)
ggplot(pie_data) +
aes(
x0 = 0, y0 = 0, r0 = 0.4, r = 1,
start = start_angle, end = end_angle,
fill = party
) +
geom_arc_bar() +
geom_text( # place amounts inside the pie
aes(
x = 0.7 * sin(mid_angle),
y = 0.7 * cos(mid_angle),
label = seats
)
) +
geom_text( # place party name outside the pie
aes(
x = 1.05 * sin(mid_angle),
y = 1.05 * cos(mid_angle),
label = party,
hjust = hjust, vjust = vjust
)
) +
coord_fixed(
xlim = c(-1.8, 1.3), ylim = c(-1.0, 1.1)
) +
theme_void()
position_stack()
, position_fill()
position_dodge()
geom_arc_bar()