Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "thirdcircle" option to allow narrowed hemicycles #60

Merged
merged 5 commits into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Suggests:
scales,
markdown
VignetteBuilder: knitr
RoxygenNote: 6.1.1
RoxygenNote: 7.3.2
Encoding: UTF-8
Collate:
'ggparliament_package.R'
Expand Down
3 changes: 1 addition & 2 deletions R/ggparliament_package.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#' ggparliament
#' @name ggparliament-package
#' @docType package
#' @keywords package
#' @keywords "_PACKAGE"
#'
#' @import ggplot2
#'
Expand Down
13 changes: 10 additions & 3 deletions R/helper_funcs.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,16 @@ calc_coordinates <- function(N, M, limits, segment = 0.5) {
lapply(1:M, function(i) {
# find how many seats for this parl_row
counts[i] <<- round(N * radii[i] / sum(radii[i:M]))
# seq from 0-180degress for the row for the cartesian position
### ROB- Need to symmetry-ise this for non 0.5/1 values of segment ###
theta <- seq(0, segment * 2 * pi, len = counts[i])
# For a hemicycle, seq from 0-180degrees for the row for the cartesian position
# For a narrower cycle (where segment < 0.5), calculate the offset (theta_narrow)
# in radians from each side
theta_narrow <- (0.5 - segment) * pi
# theta_left is the left-hand start of the arc
theta_left <- theta_narrow
# theta_right is the right-hand end of the arc
theta_right <- pi - theta_narrow
theta <- seq( theta_left, theta_right,
len = counts[i])
# subtract the seats already plotted from N
# N becomes 'seats left to calculate'
N <<- N - counts[i]
Expand Down
18 changes: 15 additions & 3 deletions R/parliament_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ parliament_data <- function(election_data = NULL,
type = c(
"horseshoe",
"semicircle",
"thirdcircle",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be "thirdcircle" or "third_circle"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could it be both? I'm inclined to allow more synonymous options - I have another branch where I've put in "hemicycle" for "semicircle" since this is a common term in French to describe the format of a such a parliament.

"third_circle",
"circle",
"classroom",
"opposing_benches"
Expand All @@ -44,13 +46,17 @@ parliament_data <- function(election_data = NULL,
else if (type == "semicircle") {
parl_layout <- calc_coordinates(sum(party_seats), parl_rows, c(1, 2))
}

else if (type == "thirdcircle" | type == "third_circle") {
parl_layout <- calc_coordinates(sum(party_seats), parl_rows, c(1, 2), segment = 1/3)
}

else if (type == "circle") {
parl_layout <- calc_coordinates(sum(party_seats), parl_rows, c(1, 2), segment = 1)
}

else if (type == "classroom") {
# calculate parl_layour by expanding a grid of rows vs the length each row needs to be
# calculate parl_layout by expanding a grid of rows vs the length each row needs to be
parl_layout <- expand.grid(
y = 1:parl_rows,
x = seq_len(ceiling(sum(party_seats) / parl_rows))
Expand Down Expand Up @@ -120,8 +126,14 @@ parliament_data <- function(election_data = NULL,
election_data <- election_data[order(-group, -party_seats), ]
}

else if (is.null(type) | !type %in% c("horseshoe", "semicircle", "circle", "classroom", "opposing_benches")) {
warning("Warning: parliament layout 'type' not supported.")
else if (is.null(type) | !type %in% c("horseshoe", "thirdcircle",
"third_circle", "semicircle", "circle", "classroom", "opposing_benches")) {
if (is.null(type)) {
type_name <- "<null>"
} else {
type_name <- type
}
warning("Warning: parliament layout \"", type_name, "\" not supported.")
}

# if election data is not null, bind layout to original data
Expand Down
12 changes: 9 additions & 3 deletions man/draw_majoritythreshold.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions man/draw_partylabels.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions man/draw_totalseats.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/election_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions man/geom_parliament_seats.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions man/ggparliament-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions man/parliament_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions vignettes/a_basic-parliament-plots_1.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,37 @@ au <- ggplot(australia_horseshoe, aes(x = x,
au
```



### Narrower "hemicycle" Parliament

```{r}
australia_sen <- election_data %>%
filter(country == "Australia" &
house == "Senate" &
year == 2016)

australia_senate <- parliament_data(election_data = australia_sen,
party_seats = australia_sen$seats,
parl_rows = 5,
type = "thirdcircle")
```

```{r,fig.width=4, fig.height=4}
au_senate <- ggplot(australia_senate, aes(x = x,
y = y,
colour = party_short)) +
geom_parliament_seats() +
theme_ggparliament() +
labs(colour = NULL,
title = "Australian Senate") +
scale_colour_manual(values = australia_sen$colour,
limits = australia_sen$party_short) +
theme(legend.position = 'bottom')

au_senate
```

### Opposing Benches Parliament

The opposing bench style of parliament calls for a variable to split the left and right bench. Using the ggparliament election data, we split on the government column which is a binary variable.
Expand Down