R:直方图中堆叠栏的自上而下订单
我试图绘制在直方图上行驶的距离,并为三类行进的距离(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))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过将
group
转换为factor
的顺序,以级别的级别
以所需的顺序设置:You could achieve your desired result by converting
Group
to afactor
with the order of thelevels
set in your desired order: