I have a variable (-coef-) that is just regression coefficient values. I am hoping to arrange all the -var- (variables of different models) in my plot based on the size of their respective -coef- values. I have tried various solutions using reorder() (such as https://www.rpubs.com/dvdunne/reorder_ggplot_barchart_axis) but they all seem geared towards geom_bar or geom_col and haven't worked with geom_point/geom_errorbar.
ggplot(d, aes(x = var, y = coef,
ymin = ci_lower, ymax = ci_upper,
color = var)) +
geom_point(size = 2) +
geom_errorbar(width = .5,
size = 1) +
coord_flip() +
theme_minimal()
EDIT: Some example data
d <- structure(list(var = c("a", "b", "c", "d", "e", "f", "g", "h",
"i", "j"), coef = c(0.1, 0.2, 0.3, 0.35, 0.46, 0.64, 0.54, 0.13,
0.87, 0.41), ci_lower = c(0.05, 0.15, 0.25, 0.3, 0.41, 0.59,
0.49, 0.08, 0.82, 0.36), ci_upper = c(0.15, 0.25, 0.35, 0.4,
0.51, 0.69, 0.59, 0.18, 0.92, 0.46)), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -10L), spec = structure(list(
cols = list(var = structure(list(), class = c("collector_character",
"collector")), coef = structure(list(), class = c("collector_double",
"collector")), ci_lower = structure(list(), class = c("collector_double",
"collector")), ci_upper = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1L), class = "col_spec"))
You could
arrange
your data by "coef" and then recode "var" withfactor(ordered = T)
. Note that ggplot will switch to a different color scale for ordered factors, so I also specifyscale_color_hue()
in the plot: