如何翻转分组条形图中的条形顺序?
我正在构建组条形图。这是我到目前为止写的代码:
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))
我对此感到满意,但我希望将其配对中的酒吧订单进行翻转:Pre-Crisis应该在危机之后。有人知道如何解决这个问题吗?任何帮助将不胜感激。这是一个最小可再现的示例的数据:
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")
I am constructing a group bar chart. Here is the code I have written thus far:
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))
This yields the following plot:
I am happy with this overall, but I want the order of the bars in the pairs to be flipped: pre-crisis should come before post-crisis. Does anyone know how to solve this problem? Any help would be appreciated. Here are data for a minimally reproducible example:
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")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个选项是转换为使用
forcats :: fct_rev
将转换为因子并逆转group
列的顺序:One option would be to convert to use
forcats::fct_rev
which converts to factor and reverse the order of yourGroup
column: