我希望两个不同嵌套分组变量的水平显示在图的y轴中的单独行上,而不是图例中。我现在有这样的代码:
data <- tibble::tribble( ~ term, ~estimate, ~conf.low, ~conf.high, ~group,
"HH cultivated plots", 0.0163, -0.0130, 0.0456, "Extensive Margin",
"# of farm assets (hoe, spade, axe), all HHs", 0.356, 0.143, 0.569, "Assets",
"Number of plots", -0.0539, -0.158, 0.0498, "Intensive Margin",
"Farm size", 0.208, -0.0394, 0.456, "Intensive Margin",
"# of HH members that cultivated plots, all HHs", 0.0541, -0.0647, 0.173, "Intensive Margin",
"Avg # of days HH members spent cultivating plots, all HHs", -0.192, -0.738, 0.354, "Intensive Margin",
"Annual agricultural revenue", 0.329, 0.170, 0.487, "Revenue")
ag_coef_plot <- data %>%
ggplot(
aes(x = estimate,
y = term,
color = group
)) +
geom_pointrange( # Confidence interval
aes(
xmin = conf.low,
xmax = conf.high
),
show.legend = FALSE,
) +
scale_x_continuous(name = "") +
geom_vline(xintercept = 0) +
scale_y_discrete(name = "",
limits = rev,
expand = c(.05, 0)) +
theme_minimal() +
theme(
plot.title = element_text(size = 15, hjust = 0.5, face = "bold"),
plot.title.position = "plot",
plot.background = element_rect(color = "black"), # For some reason this is necessary to ensure background color ISN'T black. Go figure
axis.text.y = element_text(size = 15),
axis.text.x = element_text(size = 15),
legend.position = "left",
legend.title = element_blank(),
legend.text = element_blank()
) +
scale_color_brewer(palette = "Set2")
ag_coef_plot
这段代码生成了这样的图:
但我想实现这样的东西,在完美的场景中,组标签与点的颜色相同:
我试过这篇文章的解决方案,但我不能让它工作:Multirow axis labels with nested grouping variables
1条答案
按热度按时间ndasle7k1#
分组变量之间的垂直线不对应于ggplot中的任何主题元素,但如果您喜欢简单的框,则可以使用ggplot和ggtext实现其余部分: