Plot a dot plot after averaging the values
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | plot_avg_dot(df, by_var = "region", value_var = "avg", incl_x_axis = TRUE,
x_label = NULL, x_limits = NULL, x_breaks = waiver(),
include_n = TRUE, n_shape = "square", low_colour = grey10K,
high_colour = grey70K, use_weights = FALSE, strata_var = "strata",
psu_var = "psu", weight_var = "weight", na.rm = TRUE,
sort_asc = FALSE, sort_by = "avg", plot_ci = TRUE, ci_factor = 2,
lb_var = "lb", ub_var = "ub", ci_colour = grey25K, ci_alpha = 0.6,
ci_size = 2, ref_line = TRUE, ref_text = "sample average",
label_ref = TRUE, nudge_ref_label = NULL, ref_label_y = 1,
ref_arrow = arrow(length = unit(0.007, "npc")), ref_stroke = 0.5,
ref_colour = grey75K, lollipop = FALSE, lollipop_stroke = 0.25,
lollipop_colour = grey75K, facet_var = NULL, ncol = NULL, nrow = NULL,
scales = "fixed", dot_size = 6, dot_shape = 21,
dot_fill_cont = brewer.pal(9, "YlGnBu"), label_vals = TRUE,
label_size = 3, label_colour = grey75K, label_digits = 1,
percent_vals = FALSE, value_label_offset = NULL, sat_threshold = 0.5,
horiz = TRUE, file_name = NULL, width = 10, height = 6,
saveBoth = FALSE, font_normal = "Lato", font_semi = "Lato",
font_light = "Lato Light", panel_spacing = 1, font_axis_label = 12,
font_axis_title = font_axis_label * 1.15, font_facet = font_axis_label *
1.15, font_legend_title = font_axis_label,
font_legend_label = font_axis_label * 0.8, font_subtitle = font_axis_label
* 1.2, font_title = font_axis_label * 1.3, legend.position = "none",
legend.direction = "horizontal", grey_background = FALSE,
background_colour = grey10K, projector = FALSE)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | # generate random data
library(dplyr)
df = data.frame(avg = sample(1:100, 10), region = letters[1:10], ci = sample(1:100, 10)/10) %>% mutate(lb = avg - ci, ub = avg + ci)
# sans confidence intervals
plot_dot(df, by_var = 'region', value_var = 'avg')
# with confidence intervals, no labels
plot_dot(df, by_var = 'region', value_var = 'avg', plot_ci = TRUE, label_vals = FALSE)
# as lollipops
df2 = data.frame(avg = sample(-100:100, 10)/100, region = letters[1:10], ci = sample(1:100, 20)/1000) %>% mutate(lb = avg - ci, ub = avg + ci)
library(RColorBrewer)
plot_dot(df2, by_var = 'region', value_var = 'avg', lollipop = TRUE, dot_fill_cont = brewer.pal(10, 'RdYlBu'))
# percent labels
plot_dot(df2, by_var = 'region', value_var = 'avg', percent_vals = TRUE, lollipop = TRUE, dot_fill_cont = brewer.pal(10, 'RdYlBu'))
# with reference line
plot_dot(df2, by_var = 'region', value_var = 'avg', ref_line = 0, ref_text = 'no change', label_ref = FALSE, lollipop = TRUE, dot_fill_cont = brewer.pal(10, 'RdYlBu'), percent_vals = TRUE)
# horizontal
plot_dot(df2, by_var = 'region', value_var = 'avg', horiz = FALSE, ref_line = 0, ref_text = 'no change', lollipop = TRUE, plot_ci = TRUE, dot_fill_cont = brewer.pal(10, 'RdYlBu'))
# in-built facet_wrap. Note: may screw up ordering, since will sort based on ALL the data.
df3 = data.frame(avg = sample(-100:100, 20), region = rep(letters[1:10], 2), group = c(rep('group1', 10), rep('group2', 10)))
plot_dot(df3, by_var = 'region', value_var = 'avg', facet_var = 'group', lollipop = TRUE, dot_fill_cont = brewer.pal(10, 'RdYlBu'))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.