我正在构造一个组条形图。以下是我迄今编写的代码:
p <- ggplot(data, aes(x = Word, y = Estimate, fill = Group)) +
geom_col(position = "dodge") +
geom_errorbar(
aes(ymin = Estimate - SE, ymax = Estimate + SE),
position = position_dodge(.9),
width = .2
) + labs(x = "Focal Word", y = "Norm of Beta Coefficients", title = "Figure 1: Results of Context Embedding Regression Model", caption = "p.")
p + theme(axis.text.x = element_text(angle = 90))这就产生了以下情节:

我对此总体上很满意,但我希望这两对酒吧的顺序被翻转:危机前应该出现在危机后。有人知道如何解决这个问题吗?任何帮助都将不胜感激。以下是一个重复性最低的例子的数据:
structure(list(Word = c("Economy", "Economy", "Civil Rights",
"Civil Rights", "Health", "Health"), Group = c("Pre-Crisis",
"Post-Crisis", "Pre-Crisis", "Post-Crisis", "Pre-Crisis", "Post-Crisis"
), Estimate = c(0.08197375, 0.07068641, 0.3041591, 0.4429921,
0.09703231, 0.1558241), SE = c(0.006251288, 0.003762346, 0.04490241,
0.06448664, 0.01176194, 0.01211825)), row.names = c(NA, 6L), class = "data.frame")发布于 2022-04-06 18:29:23
一种选择是转换为使用forcats::fct_rev,这将转换为因子和反转Group列的顺序:
library(ggplot2)
p <- ggplot(data, aes(x = Word, y = Estimate, fill = forcats::fct_rev(Group))) +
geom_col(position = "dodge") +
geom_errorbar(
aes(ymin = Estimate - SE, ymax = Estimate + SE),
position = position_dodge(.9),
width = .2
) +
labs(x = "Focal Word", y = "Norm of Beta Coefficients", title = "Figure 1: Results of Context Embedding Regression Model", caption = "p.")
p + theme(axis.text.x = element_text(angle = 90))

https://stackoverflow.com/questions/71771804
复制相似问题