To facet your parliament plot, use the split-apply-combine strategy in a dplyr
chain.
You must:
1) split by year 2) apply the coordinates for each party in parliament_data 3) combine the rows into one large data frame.
This can be done using map from purrr
.
A few examples are as follows:
usa <- election_data %>%
filter(country == "USA" &
house == "Representatives") %>%
split(.$year) %>% # split
map(~parliament_data(election_data = ., # apply
party_seats = .$seats,
parl_rows = 8,
type = "semicircle")) %>%
bind_rows() # combine
us <- ggplot(usa, aes(x, y, colour = party_short)) +
geom_parliament_seats() +
geom_highlight_government(government == 1) +
labs(colour = NULL,
title = "American Congress",
subtitle = "The party that has control of US Congress is encircled in black.") +
theme_ggparliament() +
scale_colour_manual(values = usa$colour,
limits = usa$party_short) +
theme(legend.position = 'bottom') +
facet_grid(~year, scales = 'free')
us
australia <- election_data %>%
filter(country == "Australia" &
year == "2016") %>%
split(.$house) %>% # split
map(~parliament_data(election_data = ., # apply
party_seats = .$seats,
parl_rows = 4,
type = "horseshoe")) %>%
bind_rows() # combine
au <- ggplot(australia, aes(x, y, colour=party_short, type = "horseshoe")) +
geom_parliament_seats() +
geom_highlight_government(government == 1) +
labs(colour = NULL,
title = "Australian Parliament",
subtitle = "Government encircled in black.") +
scale_colour_manual(values = australia$colour,
limits = australia$party_short) +
theme_ggparliament() +
theme(legend.position = 'bottom') +
facet_grid(~house, scales = 'free')
#> Warning: Ignoring unknown aesthetics: type
au
uk<- election_data %>%
filter(country == "UK") %>%
split(.$year) %>%
map(~parliament_data(election_data = .,
party_seats = .$seats,
group = .$government,
type = "opposing_benches")) %>%
bind_rows()
ggplot(data = uk,
aes(x = x,
y = y,
color = party_long)) +
geom_parliament_seats(size = 1.5) +
coord_flip() +
facet_wrap(~year, ncol = 2) +
scale_color_manual(values = uk$colour,
limits = uk$party_long) +
theme_ggparliament()