Skip to content

Commit

Permalink
addressing reviewer comments from JOSS -- add list of of vignettes to…
Browse files Browse the repository at this point in the history
… README and remove type = "horseshoe" from one of the vignettes.
  • Loading branch information
zmeers committed Apr 18, 2019
1 parent dcf02e4 commit c378625
Show file tree
Hide file tree
Showing 29 changed files with 429 additions and 283 deletions.
10 changes: 5 additions & 5 deletions R/draw_majoritythreshold.R
Expand Up @@ -9,8 +9,8 @@
#' @examples
#' data <- election_data[
#' election_data$country == "USA" &
#' election_data$house == "Representatives" &
#' election_data$year == "2016",
#' election_data$house == "Representatives" &
#' election_data$year == "2016",
#' ]
#' usa_data <- parliament_data(
#' election_data = data,
Expand All @@ -23,7 +23,7 @@
#' draw_majoritythreshold(
#' n = 218,
#' label = TRUE,
#' type = 'semicircle'
#' type = "semicircle"
#' ) +
#' theme_ggparliament()
#' @author Zoe Meers
Expand Down Expand Up @@ -53,7 +53,7 @@ draw_majoritythreshold <- function(n = NULL,
)
}

#calculate the positions of the thresholds
# calculate the positions of the thresholds
#' @export
ggplot_add.majorityLine <- function(object, plot, object_name) {
filter <- group_by <- NULL
Expand Down Expand Up @@ -92,7 +92,7 @@ ggplot_add.majorityLine <- function(object, plot, object_name) {
ggplot2::geom_segment(aes(y = y_pos_oppbenches + 0.5, yend = y_pos_oppbenches + 0.5, x = min(plot$data$x), xend = max(plot$data$x) / 2),
colour = object$linecolour, size = object$linesize, linetype = object$linetype, alpha = object$linealpha
) +
ggplot2::annotate("text", x = max(plot$data$x) / 1.9, y = y_pos_oppbenches - (y_pos_oppbenches/15), label = paste0(object$n, " seats needed\nfor a majority."))
ggplot2::annotate("text", x = max(plot$data$x) / 1.9, y = y_pos_oppbenches - (y_pos_oppbenches / 15), label = paste0(object$n, " seats needed\nfor a majority."))
}
} else {
warning("Warning: parliament layout not supported.")
Expand Down
9 changes: 5 additions & 4 deletions R/draw_partylabels.R
Expand Up @@ -8,8 +8,8 @@
#' @examples
#' data <- election_data[
#' election_data$country == "USA" &
#' election_data$house == "Representatives" &
#' election_data$year == "2016",
#' election_data$house == "Representatives" &
#' election_data$year == "2016",
#' ]
#' usa_data <- parliament_data(
#' election_data = data,
Expand All @@ -27,7 +27,8 @@
#' ) +
#' ggplot2::scale_colour_manual(
#' values = usa_data$colour,
#' limits = usa_data$party_long) +
#' limits = usa_data$party_long
#' ) +
#' theme_ggparliament()
#' @author Zoe Meers
#' @export
Expand Down Expand Up @@ -57,7 +58,7 @@ draw_partylabels <- function(type = c("semicircle", "horseshoe"),
ggplot_add.partyLabels <- function(object, plot, object_name) {
x <- y <- filter <- group_by <- summarise <- NULL

#calculate the positions of the labels
# calculate the positions of the labels
filt_data <- dplyr::filter(plot$data, row == max(row))
group_data <- dplyr::group_by(filt_data, pn = !!object$party_names, ps = !!object$party_seats, pc = !!object$party_colours)
new_dat <- dplyr::summarise(group_data, mean_x = mean(x), mean_y = mean(y))
Expand Down
8 changes: 4 additions & 4 deletions R/draw_totalseats.R
Expand Up @@ -6,8 +6,8 @@
#' @examples
#' data <- election_data[
#' election_data$country == "USA" &
#' election_data$house == "Representatives" &
#' election_data$year == "2016",
#' election_data$house == "Representatives" &
#' election_data$year == "2016",
#' ]
#' usa_data <- parliament_data(
#' election_data = data,
Expand All @@ -17,7 +17,7 @@
#' )
#' ggplot2::ggplot(usa_data, ggplot2::aes(x, y, colour = party_long)) +
#' geom_parliament_seats() +
#' draw_totalseats(n = 435, type = 'semicircle') +
#' draw_totalseats(n = 435, type = "semicircle") +
#' theme_ggparliament()
#' @author Zoe Meers
#' @export
Expand Down Expand Up @@ -47,7 +47,7 @@ draw_totalseats <- function(n = NULL,

#' @export
ggplot_add.totalLabels <- function(object, plot, object_name) {
#n <- rlang::enquo(object$n)
# n <- rlang::enquo(object$n)
if (object$type == "horseshoe") {
plot +
ggplot2::annotate("text",
Expand Down
27 changes: 15 additions & 12 deletions R/geom_emphasize_parliamentarians.R
Expand Up @@ -6,22 +6,22 @@
#' @examples
#' data <- election_data[
#' election_data$country == "USA" &
#' election_data$house == "Representatives" &
#' election_data$year == "2016",
#' election_data$house == "Representatives" &
#' election_data$year == "2016",
#' ]
#' usa_data <- parliament_data(
#' election_data = data,
#' type = "semicircle",
#' party_seats = data$seats,
#' parl_rows = 8
#' )
#'
#'
#' women_in_congress <- c(1, 0, 0, 1)
#' number_of_women <- c(23, 218, 133, 61)
#'
#'
#' usa_data$women <- rep(women_in_congress, number_of_women)
#'
#' ggplot2::ggplot(usa_data, ggplot2::aes(x, y, colour=party_long)) +
#'
#' ggplot2::ggplot(usa_data, ggplot2::aes(x, y, colour = party_long)) +
#' geom_parliament_seats() +
#' geom_emphasize_parliamentarians(women == 1) +
#' theme_ggparliament(legend = FALSE) +
Expand All @@ -33,12 +33,15 @@
#' @export
#' @importFrom ggplot2 ggplot_add

geom_emphasize_parliamentarians <- function(expr, size = 3, shape = 19, stroke = 1.8) {
structure(list(expr = rlang::enquo(expr),
size = size,
shape = shape,
stroke = stroke),
class = "emphMPs")
geom_emphasize_parliamentarians <- function(expr, size = 3, shape = 19, stroke = 1.8) {
structure(list(
expr = rlang::enquo(expr),
size = size,
shape = shape,
stroke = stroke
),
class = "emphMPs"
)
}


Expand Down
4 changes: 2 additions & 2 deletions R/geom_highlight_government.R
Expand Up @@ -7,8 +7,8 @@
#' @examples
#' data <- election_data[
#' election_data$country == "USA" &
#' election_data$house == "Representatives" &
#' election_data$year == "2016",
#' election_data$house == "Representatives" &
#' election_data$year == "2016",
#' ]
#' usa_data <- parliament_data(
#' election_data = data,
Expand Down
44 changes: 24 additions & 20 deletions R/geom_overhang_seats.R
Expand Up @@ -3,31 +3,35 @@
#' @param expr Expr refers to the designated overhang seats.
#' @examples
#' germany <- data.frame(
#' year = 2013,
#' seats = c(64, 63, 311, 193),
#' government = c(0, 0, 1, 1),
#' colour = c("#BE3075","#64A12D", "#000000","#EB001F"),
#' party = c("The Left",
#' "Alliance 90/The Greens",
#' "Christian Democratic Union",
#' "Social Democratic Party")
#' year = 2013,
#' seats = c(64, 63, 311, 193),
#' government = c(0, 0, 1, 1),
#' colour = c("#BE3075", "#64A12D", "#000000", "#EB001F"),
#' party = c(
#' "The Left",
#' "Alliance 90/The Greens",
#' "Christian Democratic Union",
#' "Social Democratic Party"
#' )
#' )
#' german_data <- parliament_data(
#' election_data = germany,
#' parl_rows = 11,
#' party_seats = germany$seats,
#' type = "semicircle"
#' election_data = germany,
#' parl_rows = 11,
#' party_seats = germany$seats,
#' type = "semicircle"
#' )
#' german_data$overhang_seats <- rep(
#' c(1, 0, 1, 0, 1, 0, 1, 0),
#' c(16, 295, 11, 182, 3, 61, 3, 60)
#')
#' c(1, 0, 1, 0, 1, 0, 1, 0),
#' c(16, 295, 11, 182, 3, 61, 3, 60)
#' )
#' ggplot2::ggplot(german_data, ggplot2::aes(x, y, colour = party)) +
#' geom_parliament_seats() +
#' geom_overhang_seats(overhang_seats == 1) +
#' theme_ggparliament() +
#' ggplot2::scale_colour_manual(values = as.character(german_data$colour),
#' limits = as.character(german_data$party))
#' geom_parliament_seats() +
#' geom_overhang_seats(overhang_seats == 1) +
#' theme_ggparliament() +
#' ggplot2::scale_colour_manual(
#' values = as.character(german_data$colour),
#' limits = as.character(german_data$party)
#' )
#' @usage
#' geom_overhang_seats(expr)
#' @author Zoe Meers
Expand Down
68 changes: 39 additions & 29 deletions R/geom_parliament_bar.R
Expand Up @@ -4,17 +4,19 @@
#' @param label If label = TRUE, print the percentage above the bar.
#' @examples
#' data <- election_data[election_data$country == "USA" &
#' election_data$house == "Representatives" &
#' election_data$year == "2016",]
#' usa_data <- parliament_data(election_data = data,
#' type = "semicircle",
#' party_seats = data$seats,
#' parl_rows = 8)
#' election_data$house == "Representatives" &
#' election_data$year == "2016", ]
#' usa_data <- parliament_data(
#' election_data = data,
#' type = "semicircle",
#' party_seats = data$seats,
#' parl_rows = 8
#' )
#' ggplot2::ggplot(usa_data, ggplot2::aes(x, y, colour = party_long)) +
#' geom_parliament_seats() +
#' geom_parliament_bar(colour, party_long) +
#' ggplot2::scale_colour_manual(values = usa_data$colour, limits = usa_data$party_long) +
#' theme_ggparliament()
#' geom_parliament_seats() +
#' geom_parliament_bar(colour, party_long) +
#' ggplot2::scale_colour_manual(values = usa_data$colour, limits = usa_data$party_long) +
#' theme_ggparliament()
#' @author Zoe Meers
#' @export
#' @importFrom ggplot2 ggplot_add
Expand All @@ -41,32 +43,40 @@ ggplot_add.parliamentBar <- function(object, plot, object_name) {

x_max <- max(plot$data$x, na.rm = TRUE)
x_min <- min(plot$data$x, na.rm = TRUE)
difference <- x_max - x_min

difference <- x_max - x_min

new_dat <- plot$data
new_dat <- dplyr::mutate(new_dat, group_no = match(!!object$party, unique(!!object$party)))
new_dat <- dplyr::count(new_dat, group_no, p = !!object$party, c = !!object$colour)
new_dat$proportion <- new_dat$n/sum(new_dat$n)
new_dat$proportion <- new_dat$n / sum(new_dat$n)
new_dat$proportion1 <- new_dat$proportion * difference
new_dat$start = cumsum(new_dat$proportion1) - x_max
new_dat$start <- cumsum(new_dat$proportion1) - x_max
new_dat$end <- c(x_min, (cumsum(new_dat$proportion1)[-nrow(new_dat)] - x_max))


if(object$label == TRUE){
plot +
geom_rect(data = new_dat,
ggplot2::aes(xmin = start, xmax = end, fill = p,
ymin = max(plot$data$y)+0.2, ymax = max(plot$data$y)+0.5),
inherit.aes = FALSE, show.legend = FALSE) +
ggrepel::geom_text_repel(ggplot2::aes(x = rowMeans(cbind(start, end)), y = max(plot$data$y)+0.55, label = scales::percent(proportion)), vjust = 0, segment.size = 0.02, nudge_y = 0.05, direction = "x", data = new_dat, inherit.aes = FALSE) +

if (object$label == TRUE) {
plot +
geom_rect(
data = new_dat,
ggplot2::aes(
xmin = start, xmax = end, fill = p,
ymin = max(plot$data$y) + 0.2, ymax = max(plot$data$y) + 0.5
),
inherit.aes = FALSE, show.legend = FALSE
) +
ggrepel::geom_text_repel(ggplot2::aes(x = rowMeans(cbind(start, end)), y = max(plot$data$y) + 0.55, label = scales::percent(proportion)), vjust = 0, segment.size = 0.02, nudge_y = 0.05, direction = "x", data = new_dat, inherit.aes = FALSE) +
ggplot2::scale_fill_manual(values = new_dat$c, limits = new_dat$p)
} else{
plot +
geom_rect(data = new_dat,
ggplot2::aes(xmin = start, xmax = end, fill = p,
ymin = max(plot$data$y)+0.2, ymax = max(plot$data$y)+0.5),
inherit.aes = FALSE, show.legend = FALSE) +
} else {
plot +
geom_rect(
data = new_dat,
ggplot2::aes(
xmin = start, xmax = end, fill = p,
ymin = max(plot$data$y) + 0.2, ymax = max(plot$data$y) + 0.5
),
inherit.aes = FALSE, show.legend = FALSE
) +
ggplot2::scale_fill_manual(values = new_dat$c, limits = new_dat$p)
}
}
64 changes: 32 additions & 32 deletions R/geom_parliament_seats.R
Expand Up @@ -15,8 +15,8 @@
#' @examples
#' data <- election_data[
#' election_data$country == "USA" &
#' election_data$house == "Representatives" &
#' election_data$year == "2016",
#' election_data$house == "Representatives" &
#' election_data$year == "2016",
#' ]
#' usa_data <- parliament_data(
#' election_data = data,
Expand Down Expand Up @@ -58,33 +58,33 @@ geom_parliament_seats <- function(mapping = NULL,
#' @usage NULL
#' @export
GeomParliamentSeats <- ggplot2::ggproto("GeomParliamentSeats", ggplot2::Geom,
required_aes = c("x", "y", "colour"),
non_missing_aes = c("shape", "size"),
default_aes = ggplot2::aes(
shape = 19,
colour = "black",
size = 3.5,
fill = NA,
alpha = NA,
stroke = 1
),
draw_panel = function(data, panel_params, coord, na.rm = FALSE) {
coords <- coord$transform(data, panel_params)
ggplot2:::ggname(
"geom_parliament_seats",
grid::pointsGrob(
coords$x, coords$y,
pch = coords$shape,
gp = grid::gpar(
col = alpha(coords$colour, coords$alpha),
fill = alpha(coords$fill, coords$alpha),
fontsize = coords$size * ggplot2::.pt + coords$stroke * .stroke,
lwd = coords$stroke * .stroke
)
)
)
},
draw_key = ggplot2::draw_key_point
)
required_aes = c("x", "y", "colour"),
non_missing_aes = c("shape", "size"),
default_aes = ggplot2::aes(
shape = 19,
colour = "black",
size = 3.5,
fill = NA,
alpha = NA,
stroke = 1
),

draw_panel = function(data, panel_params, coord, na.rm = FALSE) {
coords <- coord$transform(data, panel_params)
ggplot2:::ggname(
"geom_parliament_seats",
grid::pointsGrob(
coords$x, coords$y,
pch = coords$shape,
gp = grid::gpar(
col = alpha(coords$colour, coords$alpha),
fill = alpha(coords$fill, coords$alpha),
fontsize = coords$size * ggplot2::.pt + coords$stroke * .stroke,
lwd = coords$stroke * .stroke
)
)
)
},

draw_key = ggplot2::draw_key_point
)

0 comments on commit c378625

Please sign in to comment.