首页 > 解决方案 > ggplot2中的自定义重新排序直方图填充类别

问题描述

在 ggplot2 中,直方图填充类别按字母顺序排列。我想为我的填充分配一个自定义订单,如下所示:

cust_order <- c("Goal", "Shot on target", "Shot blocked", "Wide shot")

这与此处描述的重新排序颜色不同:How do you order the fill-colours within ggplot2 geom_bar .Code so far...

shot_df %>% filter(Team == "Team A") %>% 
  ggplot(., aes(x = xG, fill=Type)) +
  geom_histogram(binwidth = 0.10)

最终(按字母顺序)输出: 样本直方图

谢谢!

标签: rggplot2

解决方案


我们可以在客户订单中转换为factorwith然后它应该可以工作levelsvector

shot_df$Type<- factor(shot_df$Type, levels = cust_order)

推荐阅读