如何翻转分组条形图中的条形顺序?

发布于 2025-01-19 23:03:07 字数 1183 浏览 4 评论 0原文

我正在构建组条形图。这是我到目前为止写的代码:

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:enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我的黑色迷你裙 2025-01-26 23:03:07

一个选项是转换为使用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))

“”

One option would be to convert to use forcats::fct_rev which converts to factor and reverse the order of your Group column:

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))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文