Skip to content

Commit 5657976

Browse files
committed
plot_avg_ranking
1 parent 7236d61 commit 5657976

File tree

4 files changed

+91
-7
lines changed

4 files changed

+91
-7
lines changed

NAMESPACE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export(imprr_weights)
66
export(item_to_rank)
77
export(ordinal_seq)
88
export(permn_augment)
9+
export(plot_average_rank)
910
export(plot_dist_ranking)
1011
export(rank_longer)
1112
export(recover_recorded_responses)
@@ -38,11 +39,14 @@ importFrom(estimatr,tidy)
3839
importFrom(ggplot2,aes)
3940
importFrom(ggplot2,geom_col)
4041
importFrom(ggplot2,geom_hline)
42+
importFrom(ggplot2,geom_linerange)
43+
importFrom(ggplot2,geom_point)
4144
importFrom(ggplot2,geom_text)
4245
importFrom(ggplot2,ggplot)
4346
importFrom(ggplot2,scale_fill_manual)
4447
importFrom(ggplot2,scale_y_continuous)
4548
importFrom(ggplot2,theme)
49+
importFrom(ggplot2,theme_bw)
4650
importFrom(ggplot2,xlab)
4751
importFrom(ggplot2,ylab)
4852
importFrom(purrr,imap)

R/plot_avg_ranking.R

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#' Plot Average Rank Results
2+
#'
3+
#' This function takes the output from the `imprr_direct` function
4+
#' and plots the average rank results with confidence intervals.
5+
#' As long as the mean and the confidence intervals are provided,
6+
#' this function will plot other quantities of interest such as
7+
#' marginal, pairwise, top-k rankings.
8+
#'
9+
#' @param data The results data from the `imprr_direct` function.
10+
#' If an external data frame, make sure that the column names
11+
#' are the same as the output from the `imprr_direct` function.
12+
#' @param qoi_filter The quantity of interest (QOI) to filter for.
13+
#' Defaults to "average rank".
14+
#' @param xlab The x-axis label. Defaults to NULL.
15+
#' If you'd like it to be empty, specify an empty string.
16+
#' @param ylab The y-axis label. Defaults to an empty string.
17+
#'
18+
#' @return A ggplot object.
19+
#'
20+
#' @importFrom ggplot2 ggplot aes geom_point geom_linerange theme_bw xlab ylab
21+
#'
22+
#' @export
23+
#'
24+
plot_average_rank <- function(data,
25+
qoi_filter = "average rank",
26+
xlab = NULL,
27+
ylab = "") {
28+
item <- lower <- upper <- NULL
29+
30+
if (!is.null(qoi_filter)) {
31+
data <- data[data$qoi == qoi_filter, ]
32+
if (is.null(xlab)) {
33+
## Simple capitalization
34+
s <- strsplit(qoi_filter, " ")[[1]]
35+
xlab <- paste(
36+
toupper(substring(s, 1, 1)),
37+
substring(s, 2), sep = "", collapse = " "
38+
)
39+
} else {
40+
xlab <- ""
41+
}
42+
}
43+
44+
# Level/label setting should be done outside the function
45+
# Generate the plot
46+
p <- data %>%
47+
ggplot(aes(x = mean, y = item)) +
48+
geom_point() +
49+
geom_linerange(aes(xmin = lower, xmax = upper)) +
50+
theme_bw() +
51+
xlab(xlab) +
52+
ylab(ylab)
53+
54+
return(p)
55+
}

man/plot_average_rank.Rd

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vignettes/v2-bias-correction.Rmd

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,7 @@ out_direct$results %>%
143143
labels = c("party", "religion", "gender", "race")
144144
)
145145
) %>%
146-
filter(qoi == "average rank") %>%
147-
ggplot(aes(x = mean, y = item)) +
148-
geom_point() +
149-
geom_linerange(aes(xmin = lower, xmax = upper)) +
150-
theme_bw() +
151-
xlab("average rank") +
152-
ylab("")
146+
plot_average_rank()
153147
```
154148

155149
## Weighting-Based Bias Correction via `imprr_weight`

0 commit comments

Comments
 (0)