【发布时间】:2022-12-04 09:03:31
【问题描述】:
我正在使用 facet_grid() 显示 2x2 不同模型类型组合的种族群体和程序参与级别。
通过使用scales = "free",我能够分离出每行的 y 轴并仅显示相关系数。但是,我怎样才能在每个面板行中指定模型/变量顺序?通常,我会做类似的事情:
model_order <- c("White", "Black", "Hispanic")
然后将其传递给scale_x_discrete()。 (并且会依次为高、中、低)。
但这在这种情况下似乎不起作用,因为使用了scales = "free"。有控制订单的解决方法吗?
代码:
mylabels <- c("1" = "Linear",
"2" = "Logit",
"3" = "Race",
"4" = "Level")
ggplot(dx, aes(x = var, y = coef,
ymin = ci_lower, ymax = ci_upper)) +
geom_point(size = 2) +
geom_errorbar(width = 0.1,
size = 1) +
facet_grid(effect~model,
scales = "free",
labeller = as_labeller(mylabels)) +
scale_y_continuous(breaks = seq(-3, 3, by = 1)) +
coord_flip() +
theme_bw(base_size = 15) +
theme(legend.position = "none")
数据:
structure(list(var = c("White", "Black", "Hispanic", "White",
"Black", "Hispanic", "High", "Medium", "Low", "High", "Medium",
"Low"), coef = c(1.64, 1.2, 0.4, 1.45, 0.17, 0.6, 1.04, 0.05,
-0.74, -0.99, -0.45, -0.3045), ci_lower = c(1.3, 0.86, 0.06,
1.11, -0.17, 0.26, 0.7, -0.29, -1.08, -1.33, -0.79, -0.6445),
ci_upper = c(1.98, 1.54, 0.74, 1.79, 0.51, 0.94, 1.38, 0.39,
-0.4, -0.65, -0.11, 0.0355), model = c(1, 1, 1, 2, 2, 2,
1, 1, 1, 2, 2, 2), effect = c(3, 3, 3, 3, 3, 3, 4, 4, 4,
4, 4, 4)), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -12L), 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")), model = structure(list(), class = c("collector_double",
"collector")), effect = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1L), class = "col_spec"))
【问题讨论】:
标签: r ggplot2 facet-grid