如何在 ggplot2 中更改位置 = "identity" 的重叠条的顺序

How to change order of overlaid bars with position = "identity" in ggplot2

我在 R 方面不是很有经验,我一直有一个我无法弄清楚的 geom_col(position = "identity") 问题。

这是一些混淆后的数据,仍然显示我的问题和图表的代码。

#Dataframe
print(colorsshapes, n = 30)
# A tibble: 30 x 4
# Groups:   year, shape [15]
    year shape  color total
   <int> <chr>  <chr> <int>
 1  2015 circle Red     101
 2  2015 circle Blue   6960
 3  2015 star   Red     232
 4  2015 star   Blue   9436
 5  2015 square Red     218
 6  2015 square Blue   9417
 7  2016 circle Red     146
 8  2016 circle Blue   7041
 9  2016 star   Red     415
10  2016 star   Blue   9702
11  2016 square Red     381
12  2016 square Blue   9653
13  2017 circle Red     167
14  2017 circle Blue   6890
15  2017 star   Red     780
16  2017 star   Blue  10277
17  2017 square Red     754
18  2017 square Blue  10231
19  2018 circle Red     306
20  2018 circle Blue   7067
21  2018 star   Red    1066
22  2018 star   Blue  10255
23  2018 square Red    1058
24  2018 square Blue  10233
25  2019 circle Red     457
26  2019 circle Blue   6304
27  2019 star   Red    1886
28  2019 star   Blue  10082
29  2019 square Red    1870
30  2019 square Blue  10057

#Graph
basic_hist <- colorsshapes %>%
    ggplot(aes(x = shape, y = total, fill = color)) +
    geom_col(position = "identity", color = "black") +
    scale_x_discrete(name = "Shape", 
        breaks = c("star", "circle", "square"), 
        labels = c("Star", "Circle", "Square")) +
    scale_y_continuous(name = "Total", 
        limits = c(0,10500)) +
    scale_fill_manual (name = "Color", 
        breaks = c("Red", "Blue"), 
        labels = c("Red", "Blue"), 
        values = c("indianred1", "darkslategray3")) +
    facet_wrap(~year, nrow = 1) +
    geom_text (aes(x = shape, y = total, label = total), stat = "identity", 
        position = position_nudge(y = 200), show.legend = FALSE) +
    theme(axis.title = element_text(face = "bold", size = 12),
        axis.text.x = element_text(angle = 345, size = 12, hjust = 0, vjust = 1),
        axis.title.x = element_text(margin = margin(t = 10, r = 2, b = 1, l = 2)),
        legend.title = element_text(face = "bold", size = 12),
        legend.text = element_text(size = 10),
        panel.spacing = unit(0.5, "cm"), 
        strip.text = element_text(size = 12, face = "bold"))

R 首先绘制较高的条形图,使较小的条形图不可见。我宁愿不调整 alpha 级别,因为图例不会与较亮条的颜色完美匹配。我的主管也更喜欢对比色,这使得调整 alpha 在美学上不太令人愉悦。

我试过将 fill 变量转换为一个因子并手动设置标签。没有运气。我尝试重新排序 scale_fill_manual() 调用中的级别。没有运气。我在图形代码中尝试了 arrange() 和 sort() 的变体。没有运气。有什么想法吗?

非常感谢!

这可以通过重新排序数据集来实现,例如使用例如dplyr::arrange(desc(total))

library(dplyr)
library(ggplot2)

colorsshapes %>%
  arrange(desc(total)) %>% 
  ggplot(aes(x = shape, y = total, fill = color)) +
  geom_col(position = "identity", color = "black") +
  scale_x_discrete(name = "Shape", 
                   breaks = c("star", "circle", "square"), 
                   labels = c("Star", "Circle", "Square")) +
  scale_y_continuous(name = "Total", 
                     limits = c(0,10500)) +
  scale_fill_manual (name = "Color", 
                     breaks = c("Red", "Blue"), 
                     labels = c("Red", "Blue"), 
                     values = c("indianred1", "darkslategray3")) +
  facet_wrap(~year, nrow = 1) +
  geom_text (aes(x = shape, y = total, label = total), stat = "identity", 
             position = position_nudge(y = 200), show.legend = FALSE) +
  theme(axis.title = element_text(face = "bold", size = 12),
        axis.text.x = element_text(angle = 345, size = 12, hjust = 0, vjust = 1),
        axis.title.x = element_text(margin = margin(t = 10, r = 2, b = 1, l = 2)),
        legend.title = element_text(face = "bold", size = 12),
        legend.text = element_text(size = 10),
        panel.spacing = unit(0.5, "cm"), 
        strip.text = element_text(size = 12, face = "bold"))

reprex package (v0.3.0)

于 2020-11-30 创建