R:直方图中堆叠栏的自上而下订单

发布于 2025-02-13 19:56:41 字数 1581 浏览 4 评论 0原文

我试图绘制在直方图上行驶的距离,并为三类行进的距离(0km-未到达目的地)上色,= 0km确实到达了目的地,并且> 0km-比目的地更远)。在中间,有一个堆叠的吧台,我想要的是绿色堆叠的吧台位于底部(最接近其他绿色条),蓝色的杠铃位于中间顶。

这是我数据的最小示例,其中组:-1未能到达目的地,0表示到达目的地,+1意味着比目的地更远。

structure(list(excess_distance_km = c(87.5, 27.8, 18, 22.3, 35, 
120.6, -0.3, 0, 0, 24.6, -0.2, 2.9, 2.9, 8.7, 27.6, 32.9, 32.9, 
0, -0.2, -52.3, 54.4, 64.2, 64.2, 64.2, 50.2, 43.5, -13.5, -17.6, 
82.1, 82.1, 0, 74, 34.9, 35.2, 0, 2, -11.4, 0, 2.5, -52.3, 68.1, 
18, 0, 74.5, 29.6, 0.2, 18.9, 0, 0.7, 1.4), Group = c("+1", "+1", 
"+1", "+1", "+1", "+1", "-1", "0", "0", "+1", "-1", "+1", "+1", 
"+1", "+1", "+1", "+1", "0", "-1", "-1", "+1", "+1", "+1", "+1", 
"+1", "+1", "-1", "-1", "+1", "+1", "0", "+1", "+1", "+1", "0", 
"+1", "-1", "0", "+1", "-1", "+1", "+1", "0", "+1", "+1", "+1", 
"+1", "0", "+1", "+1")), row.names = c(NA, -50L), class = "data.frame")

这是我的代码:

df %>%
  ggplot( aes(x = excess_distance_km)) +
    geom_histogram(aes(fill = Group), binwidth=5, alpha=0.9, colour = "grey25") +
    ggtitle("Excess Distance") +
    theme_classic() +
    theme(plot.title = element_text(size=15, hjust = 0.5),
          axis.title = element_text(size=12, hjust = 0.5),
          legend.text = element_text(size=12),
          legend.title = element_blank(),
          legend.position = "bottom"
    ) +
  guides(fill = guide_legend(nrow = 3)) 

这就是现在的样子:

I am trying to plot the distance traveled on a histogram and have coloured the distance traveled in three categories (<0km - did not reach destination, =0km did reach destination, and >0km - going farther than destination). In the middle, there is a stacked bar, and what I would like is for the green stacked bar to be at the bottom (closest to the other green bars), the blue bar to be in the middle, and the red bar to be on top.

This is a minimal example of my data, where for Group: -1 means did not reach destination, 0 means reached destination, +1 means going farther than destination.

structure(list(excess_distance_km = c(87.5, 27.8, 18, 22.3, 35, 
120.6, -0.3, 0, 0, 24.6, -0.2, 2.9, 2.9, 8.7, 27.6, 32.9, 32.9, 
0, -0.2, -52.3, 54.4, 64.2, 64.2, 64.2, 50.2, 43.5, -13.5, -17.6, 
82.1, 82.1, 0, 74, 34.9, 35.2, 0, 2, -11.4, 0, 2.5, -52.3, 68.1, 
18, 0, 74.5, 29.6, 0.2, 18.9, 0, 0.7, 1.4), Group = c("+1", "+1", 
"+1", "+1", "+1", "+1", "-1", "0", "0", "+1", "-1", "+1", "+1", 
"+1", "+1", "+1", "+1", "0", "-1", "-1", "+1", "+1", "+1", "+1", 
"+1", "+1", "-1", "-1", "+1", "+1", "0", "+1", "+1", "+1", "0", 
"+1", "-1", "0", "+1", "-1", "+1", "+1", "0", "+1", "+1", "+1", 
"+1", "0", "+1", "+1")), row.names = c(NA, -50L), class = "data.frame")

Here is my code:

df %>%
  ggplot( aes(x = excess_distance_km)) +
    geom_histogram(aes(fill = Group), binwidth=5, alpha=0.9, colour = "grey25") +
    ggtitle("Excess Distance") +
    theme_classic() +
    theme(plot.title = element_text(size=15, hjust = 0.5),
          axis.title = element_text(size=12, hjust = 0.5),
          legend.text = element_text(size=12),
          legend.title = element_blank(),
          legend.position = "bottom"
    ) +
  guides(fill = guide_legend(nrow = 3)) 

This is what it looks like right now:
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

场罚期间 2025-02-20 19:56:41

您可以通过将group转换为factor的顺序,以级别的级别以所需的顺序设置:

library(ggplot2)
library(dplyr, warn=FALSE)

df %>%
  mutate(Group = factor(Group, levels = c("-1", "0", "+1"))) %>%
  ggplot( aes(x = excess_distance_km)) +
  geom_histogram(aes(fill = Group), binwidth=5, alpha=0.9, colour = "grey25") +
  ggtitle("Excess Distance") +
  theme_classic() +
  theme(plot.title = element_text(size=15, hjust = 0.5),
        axis.title = element_text(size=12, hjust = 0.5),
        legend.text = element_text(size=12),
        legend.title = element_blank(),
        legend.position = "bottom"
  ) +
  guides(fill = guide_legend(nrow = 3)) 

You could achieve your desired result by converting Group to a factor with the order of the levels set in your desired order:

library(ggplot2)
library(dplyr, warn=FALSE)

df %>%
  mutate(Group = factor(Group, levels = c("-1", "0", "+1"))) %>%
  ggplot( aes(x = excess_distance_km)) +
  geom_histogram(aes(fill = Group), binwidth=5, alpha=0.9, colour = "grey25") +
  ggtitle("Excess Distance") +
  theme_classic() +
  theme(plot.title = element_text(size=15, hjust = 0.5),
        axis.title = element_text(size=12, hjust = 0.5),
        legend.text = element_text(size=12),
        legend.title = element_blank(),
        legend.position = "bottom"
  ) +
  guides(fill = guide_legend(nrow = 3)) 

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