0

我在 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 不那么美观。

我尝试将填充变量转换为因子并手动设置标签。没运气。我尝试在 scale_fill_manual() 调用中重新排序级别。没运气。我在图形代码中尝试了排列()和排序()的变体。没运气。有任何想法吗?

非常感激!

4

1 回答 1

1

这可以通过重新排序数据集来实现,以便使用例如在最后和顶部绘制较小的条形图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 包于 2020-11-30 创建(v0.3.0)

于 2020-11-30T20:03:53.733 回答