-
Notifications
You must be signed in to change notification settings - Fork 7
updates from utils/update.r scripts, adding CI paramteres to ei_iter,… #150
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5acfdce
updates from utils/update.r scripts, adding CI paramteres to ei_iter,…
8b7614a
ensure updated documentation, moved eiExpand features to rpv_coef_plo…
dda995d
Clean up rpv_toDF, summary, and ei_rxc formatting and comments
24bdaff
final additions, fixed rpv_toDF.R's line 153, updated News.md and Des…
3db12d2
add south_carolina dataset to eiCompare, needed for rpv_toDF examples
7640cdc
remove rpv_normalize in example since its from eiExpand
593dd20
update rpv_toDF example docs
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| #' @export | ||
| #' @import ggplot2 | ||
| #' @importFrom rlang .data | ||
| #' | ||
| #' @author Rachel Carroll <rachelcarroll4@gmail.com> | ||
| #' @author Stephen El-Khatib <stevekhatib@gmail.com> | ||
| #' @author Loren Collingwood <lcollingwood@unm.edu> | ||
| #' | ||
| #' @title Racially Polarized Voting Analysis (RPV) Coefficient Plot | ||
| #' @description Creates a coefficient plot showing of RPV results estimate ranges | ||
| #' of all contests by voter race | ||
| #' @param rpvDF A data.frame containing RPV results | ||
| #' @param title The plot title | ||
| #' @param caption The plot caption | ||
| #' @param ylab Label along y axis | ||
| #' @param colors Character vector of colors, one for each racial group. The order | ||
| #' of colors will be respective to the order of racial groups. | ||
| #' @param race_order Character vector of racial groups from the \code{voter_race} column of | ||
| #' \code{rpvDF} in the order they should appear in the plot. If not specified, | ||
| #' the race groups will appear in alphabetical order. | ||
| #' | ||
| #' @return Coefficient plot of RPV analysis as a ggplot2 object | ||
| #' | ||
| #' @examples | ||
| #'library(eiCompare) | ||
| #'data(example_rpvDF) | ||
| #' | ||
| #'dem_rpv_results <- example_rpvDF %>% dplyr::filter(Party == "Democratic") | ||
| #'rpv_coef_plot(dem_rpv_results) | ||
| #' | ||
| rpv_coef_plot <- function( | ||
| rpvDF = NULL, | ||
| title = "Racially Polarized Voting Analysis Estimates", | ||
| caption = "Data: eiCompare RPV estimates", | ||
| ylab = NULL, | ||
| colors = NULL, | ||
| race_order = NULL | ||
| ) { | ||
|
|
||
| # ----------------------------- QC CHECKS ----------------------------- | ||
|
|
||
| colnames(rpvDF) <- stringr::str_to_lower(colnames(rpvDF)) | ||
|
|
||
| ##### new code (copied from eiExpand lines 40-58) | ||
| # make sure rpvDF argument is defined | ||
| if(is.null(rpvDF)){stop("you must include rpvDF argument")} | ||
|
|
||
| # make sure necessary columns are included | ||
| dif <- setdiff(c("party", "voter_race", "estimate", "lower_bound", "upper_bound"), | ||
| colnames(rpvDF)) | ||
|
|
||
| if( length(dif) > 0 ) { | ||
| stop(paste("rpvDF is missing the following fields:", | ||
| paste(dif, collapse = ", "))) | ||
| } | ||
|
|
||
| # make sure only one party is in rpvDF | ||
| if( length(unique(rpvDF$party)) > 1 ){ | ||
| stop("rpvDF should only contain one unique values in column Party")} | ||
| ##### end QC checks | ||
|
|
||
| # ---------------------- Prep Data and Plot Inputs ---------------------- | ||
|
|
||
| ##### Voter Race Order ##### | ||
| ##### old code (from Updates_7_1_2024.R) | ||
| # rpvDF$voter_race <- factor(rpvDF$voter_race, levels = race_order) | ||
| ##### new code (copied from eiExpand lines 64-69) | ||
| # proper case for plot | ||
| rpvDF$voter_race <- stringr::str_to_title(rpvDF$voter_race) | ||
| #get factor order if not specified | ||
| if( is.null(race_order) ) { race_order <- sort(unique(rpvDF$voter_race)) } | ||
| #set factor | ||
| rpvDF$voter_race <- factor(rpvDF$voter_race, | ||
| levels = race_order) | ||
|
|
||
| ##### Colors ##### | ||
| len_race <- length(unique(rpvDF$voter_race)) | ||
| ##### old code (from Updates_7_1_2024.R) | ||
| # if (is.null(colors)) { | ||
| # if (len_race == 2) { | ||
| # race_colors <- c(viridis::viridis(10)[4], viridis::viridis(10)[7]) | ||
| # names(race_colors) <- race_order | ||
| # ggplot_color_obj <- scale_color_manual(values = race_colors) | ||
| # } | ||
| # else { | ||
| # ggplot_color_obj <- viridis::scale_color_viridis(drop = FALSE, | ||
| # discrete = TRUE, option = "turbo", alpha = 0.8) | ||
| # } | ||
| # } | ||
| ##### new code (copied from eiExpand lines 71-85) | ||
| if( is.null(colors) ){ | ||
| if( len_race == 2 ){ | ||
| race_colors <- c(viridis::viridis(10)[4], viridis::viridis(10)[7]) | ||
| names(race_colors) <- race_order | ||
|
|
||
| ggplot_color_obj <- scale_color_manual(values = race_colors) | ||
|
|
||
| } else { | ||
| ggplot_color_obj <- viridis::scale_color_viridis(drop = FALSE, | ||
| discrete = TRUE, | ||
| option = "turbo", | ||
| alpha = .8) | ||
| } | ||
| } # END if( is.null(colors) ) | ||
|
|
||
| ##### ylab ##### | ||
| if( is.null(ylab) ){ | ||
| prty <- unique(rpvDF$party) %>% stringr::str_to_title() | ||
| ylab <- paste("Percent Voting for", prty, "Candidate") | ||
| } | ||
|
|
||
| ##### mean percent vote for label ##### | ||
| mean <- rpvDF %>% | ||
| dplyr::group_by(.data$voter_race) %>% | ||
| dplyr::summarize(avg = mean(.data$estimate)) | ||
|
|
||
| rpvDF <- dplyr::left_join(rpvDF, mean, by = "voter_race") | ||
| rpvDF$panelLab <- paste0(rpvDF$voter_race, "\n(mean: ", round(rpvDF$avg,1), "%)") | ||
|
|
||
| # -------------------------- Build Plot -------------------------- | ||
|
|
||
| coef_plot <- ggplot(rpvDF, | ||
| aes(x = 0, y = 0:100)) + | ||
| scale_y_continuous(breaks = seq(0,100, by = 10), | ||
| limits = c(0, 100), | ||
| labels = sprintf("%0.1f%%", seq(0,100, by = 10)), | ||
| expand = c(0, 0)) + | ||
| geom_hline(yintercept = 50, colour = "#000000", size = 0.75) + # Line at 0 | ||
| geom_pointrange(aes(y = .data$estimate, | ||
| ymin = .data$lower_bound, | ||
| ymax = .data$upper_bound, | ||
| color = .data$voter_race), | ||
| position = position_jitter(width = 0.1), | ||
| size = 2, | ||
| fatten = 1.5, | ||
| show.legend = F) + # Ranges for each coefficient | ||
| ggplot_color_obj + | ||
| facet_grid(~panelLab) + | ||
| labs(y = ylab, | ||
| title = title, | ||
| caption = caption) + # Labels | ||
| theme_minimal() + | ||
| theme(legend.title = element_blank(), | ||
| axis.title.x = element_blank(), | ||
| axis.ticks.x = element_blank(), | ||
| axis.text.x = element_blank(), | ||
| panel.border = element_rect(fill = NA, colour = "grey"), | ||
| panel.grid.major.x = element_blank(), | ||
| panel.grid.minor.x = element_blank(), | ||
| panel.grid.minor.y = element_blank(), | ||
| axis.text.y = element_text(size = 20, face = "bold", family = "serif"), | ||
| axis.title.y = element_text(size = 24, face = "bold", family = "serif"), | ||
| strip.text.x = element_text(size = 15, face = "bold", family = "serif"), | ||
| #strip.text.x = element_blank(), | ||
| title = element_text(size = 30, hjust = .5, face = "bold", family = "serif"), | ||
| plot.caption = element_text(size = 12, face = "italic", family = "serif") | ||
| ) | ||
|
|
||
| # -------------------------- Return -------------------------- | ||
| return(coef_plot) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I strongly prefer to keep these comments. The substantive changes will be clreaer and its helpful to have well commented out code