Generate a simple Dumbbell plot with all of Gradient's best practices applied. Almost instantly ready for publication.
Usage
g6tr_dumbbell_plot(
df,
x1,
x2,
x3 = NULL,
y,
facet = NULL,
display_text = c("all", "highlighted", "none"),
display_values = NULL,
text_fontface = c("plain", "bold", "italic", "bold.italic"),
text_position = c("side", "above", "below"),
text_remove_middle = FALSE,
highlight = NULL,
highlight_color = g6tr_pal("core")(1),
highlight_color_other = "gray",
point_size = 10,
point_stroke = 0.2 * point_size,
pch = 21,
pch3 = 10,
text_size = 0.7 * point_size,
expand = 0.1,
xintercept = NULL,
scale = "none",
accuracy = 1,
legend_labels = NULL
)Arguments
- df
Data frame that holds x and y columns.
- x1
Unquoted X variable. One end of the dumbbell plot.
- x2
Unquoted X variable. Other end of the dumbbell plot.
- x3
Unquoted X variable. (optional) Third side of the dumbbell plot.
- y
Unquoted Y variable.
- facet
Unquoted variable. variable to split plot by. Default is NULL which means there's nothing to split plot by.
- display_text
Character. How text should be displayed. One of
all(default) (apply text labels to every bar),highlighted(apply text label to only items passed inhighlight) ornone(don't apply text labels).- display_values
xvariables to display text on. By default, it is set to NULL. When no variable is passed tox3, it displays text labels onx1andx2but if a value is passed inx3, it displays text on the far left and far right. Passing a value here would override the default arrangement and would only display text on the variables passed in.- text_fontface
Character. One of
bold(default),"plain","italic"or"bold.italic".- text_position
Character. How text labels should be displayed. One of
side,aboveandbelow.- text_remove_middle
Logical. Default is FALSE Should the middle text label be removed?
- highlight
Character. Item(s) from
yto be highlighted in the plot.- highlight_color, highlight_color_other
Character. Valid color (6 digit hex-code or must consist in
grDevices::colors()) for the dumbbells and highlights of the plot. By default, whenhighlightisn't specified, all dumbbells are g6tr_pal()(n_levels)n_levels= number of unique labels inyand whenhighlightis specified, highlighted dumbbells are displayed in g6tr core blue while the other dumbbells are in grey. If arguments are passed tohighlight,highlight_colorhandles the color of the highlighted fields andhighlight_color_otherhandles the colors of the fields not highlighted.- point_size
Numeric. Size of points at dumbbell ends.
- point_stroke
Numeric. Width of outline of points at dumbbell ends.
- pch
Integer. Point shape. Ranges from 21-25.
- pch3
Integer. Point shape. Ranges from 0-25. to be used to distinguish
x3variable.- text_size
Numeric. Size of the text passed to
ggplot2::geom_text().- expand
Numeric. Value to be passed the
multargument ofggplot2::expansion()default is 0.1.- xintercept
Numeric (default is NULL) Set intercepts on the x axis before creating plots. Can be passed in as a vector of values.
- scale
Character. How should the labels and x axis be formatted. One of
percent,dollar,none(default) or a function e.gscales::comma.- accuracy
Numeric. Passed to the
accuracyargument of the selectedscalefunction to set numeric accuracy. Default is1.- legend_labels
Character. (default is NULL) A 2/3L vector to specify label names. Follows the format c(
x1,x2) when only x1 and x2 are used and c(x1,x2,x3) when all x variables are used.
Examples
library(dplyr)
library(ggplot2)
library(tidyr)
## Prepare sample data
set.seed(2022)
guess_master <-
tibble(
car_name = c("Acura", "Kia", "Nissan", "Porsche"),
m2_actual = sample(1:100, 4),
m2_guess = sample(1:100, 4),
m2_past = sample(1:100, 4)
)
## Plot a basic dumbbell chart
guess_master %>%
g6tr_dumbbell_plot(
x1 = m2_actual,
x2 = m2_guess,
y = car_name,
xintercept = 50,
legend_labels = c("Actual", "Estimated")
)
## Plot a basic dumbbell chart with all 3 X variables
guess_master %>%
g6tr_dumbbell_plot(
x1 = m2_actual,
x2 = m2_guess,
x3 = m2_past,
y = car_name,
xintercept = 50,
legend_labels = c("Actual", "Estimated", "Past")
)
## Plot dumbbell chart with highlighting features
guess_master %>%
g6tr_dumbbell_plot(
x1 = m2_actual,
x2 = m2_guess,
y = car_name,
display_text = "highlighted",
highlight = c("Kia", "Acura"),
legend_labels = c("Actual", "Estimated")
)
## Plot dumbbell chart with highlighting features with all 3 X variables
guess_master %>%
g6tr_dumbbell_plot(
x1 = m2_actual,
x2 = m2_guess,
x3 = m2_past,
y = car_name,
display_text = "highlighted",
text_position = "above",
highlight = c("Porsche", "Kia"),
legend_labels = c("Actual", "Estimated", "Past")
)
## Using a custom scale and label display order
## Take a look at how the text is displayed
guess_master %>%
g6tr_dumbbell_plot(
x1 = m2_actual,
x2 = m2_guess,
x3 = m2_past,
y = car_name,
display_values = c(m2_actual, m2_guess, m2_past),
text_position = "side",
scale = scales::dollar,
accuracy = 1,
legend_labels = c("Actual", "Estimated", "Past")
)
## Recreating the popular list experiment plot
data <-
tibble(
group = rep(c("Democrats", "Republicans", "Overall"), 2),
list_p = c(0.467, 0.249, 0.412, 0.676, 0.344, 0.494),
direct_p = c(0.691, 0.239, 0.493, 0.786, 0.352, 0.588),
question = rep(1:2, each = 3),
label = rep(
c(
"Statement 1: Yellow color is better than red",
"Statement 2: It was raining yesterday"
),
each = 3
)
)
## color palette for population groups that were studied
pal_party <- c(
"Democrats" = "#3693ef",
"Republicans" = "#de425b",
"Overall" = "#826cc4"
)
data %>%
g6tr_dumbbell_plot(
x1 = list_p,
x2 = direct_p,
y = group,
highlight = names(pal_party),
highlight_color = pal_party,
facet = label,
scale = "percent",
xintercept = 0.5,
accuracy = 1,
expand = c(0, 0.4),
legend_labels = c("Private Opinion", "Public Opinion")
) +
ggtitle(
paste(
c(
"Difference in private vs. public opinions\n",
"about the American judicial system"
),
collapse = ""
),
subtitle = paste0(
c(
"Percentages indicate the predicted probability of agreeing ",
"with the statement in a list\nexperiment (private) vs. a direct ",
"question (public)"
),
collapse = ""
)
)