I have a function in R that makes a bar graph, but the function places the bars alphabetically. I would like to be able to pin the "Placebo" bar to the left position so that any bar graphs automatically read "Placebo" "treatment". Would the reorder
command be what I need or is there some other way to coerce "Placebo" to stay on the left side?
Ideally I would like to alter the function itself rather than alter the position individually, as I have a fair number of these bar graphs to produce.
Thanks!
working graph not working graph
The data structure is similar to this:
T2 model_label model difference p_two_tail p_one_tail difference2 star p_label treatment outcome
1 trust_lo~2 trust_localgvt 1A 20.401650 0.1829079 0.09145395 +20.4 ^ +20.4^ Placebo 44.57343
2 trust_lo~2 trust_localgvt 1A 20.401650 0.1829079 0.09145395 +20.4 ^ +20.4^ Generic GOTV 64.97508
draw_bar_auto <-
function(data,
x = treatment,
y = outcome,
plot_title = model_label,
p_label = p_label,
fill_color = c("gold", "darkslateblue"),
fontsize_magnitude = 7,
line_size = 1.5,
linetype = "solid",
fontface = "bold",
fontsize_pvalue = 8,
...) {
x <- enquo(x)
y <- enquo(y)
plot_title <- enquo(plot_title)
p_label <- enquo(p_label)
# Use the variable `model_label` as plot title
plot_title <- data %>%
select(!!plot_title) %>%
pull()
# Stores the value for "outcome" variables to calculate whiskers position
whiskers <- data %>%
select(!!y) %>%
pull()
# Stores the p-value label
p_value_label <- data %>%
select(!!p_label) %>%
slice(1)
ggplot(data) +
# Plot a bar chart, `treatment` variable on x axis, `outcome` variable on y axis
geom_col(aes(x = !!x, y = !!y,
# Vary fill and outline color by `treatment` variable
fill = !!x, color = !!x)) +
# Label outcome variable values on each bar, print only one decimal place
geom_text(aes(x = !!x, y = !!y, label = sprintf('%.1f', !!y)),
# make vertical adjustment, set font size
vjust = -1, size = fontsize_magnitude) +
# Set y axis range (0 to 100 in this case)
coord_cartesian(ylim = c(0, 110), clip = "off") +
# Set y axis ticks break (none in this case)
scale_y_continuous(breaks = c(0, 100)) +
# Color for bar chart fill
scale_fill_manual(values = fill_color) +
# Color for bar chart outline
scale_color_manual(values = c("grey30", "grey30")) +
# Plot title and axis labels
labs(title = "",
# Remove labels for x and y axis
x = "", y = "") +
# Draw horizontal line
annotate("segment",
# x coord position: starts on 1st bar, ends on 2nd bar
x = 1, xend = 2,
# height of horizontal bar: height of taller bar + 25
y = whiskers[2] + 15, yend = whiskers[2] + 15,
size = line_size, linetype = linetype) +
# Draw left whisker
annotate("segment", x = 1, xend = 1,
# starts at: left bar + 15. ends at: height of taller bar + 25
y = whiskers[1] + 10, yend = whiskers[2] + 15,
size = line_size, linetype = linetype) +
# Draw right whisker
annotate("segment", x = 2, xend = 2,
# starts at: right bar + 15. ends at: height of taller bar + 25
y = whiskers[2] + 10, yend = whiskers[2] + 15,
size = line_size, linetype = linetype) +
# Add effect size and p-value
annotate("text", x = 1.5,
# position of text: height of taller bar + 32 (i.e. horizontal bar + 7)
y = whiskers[2] + 20,
# text shown: values stored in p_value_label
label = p_value_label,
size = fontsize_pvalue, fontface = fontface, ...)
}