【问题标题】:How to reoder the stacks in a ggplot bar chart to reflect the reordering of the legend如何重新排序ggplot条形图中的堆栈以反映图例的重新排序
【发布时间】:2022-01-06 17:37:23
【问题描述】:

我正在寻找一种方法来重新排序条形图的堆栈以反映重新排序的图例,以便该图更直观和可解释,图表左端有“大大减少”,“大大增加”右边,中间是“保持不变”。

我能够轻松地重新排序图例,但不能重新排序相关的堆叠条。请指教。

此外,颜色被选择为对色盲友好,因此我致力于使用所选的颜色。

首先,生成数据。

pop <- c("hunt", "hunt", "hunt", "hunt", "hunt", "hunt", "hunt", 
                "trap", "trap", "trap", "trap", "trap", "trap", "trap",
                "gp", "gp", "gp", "gp", "gp", "gp",  "gp", 
                "UP", "UP", "UP", "UP", "UP", "UP", "UP", 
                "SL", "SL", "SL", "SL", "SL", "SL", "SL", 
                "NL", "NL", "NL", "NL", "NL", "NL", "NL")

res <- c(1, 2, 3, 4, 5, 6, 7,
         1, 2, 3, 4, 5, 6, 7,
         1, 2, 3, 4, 5, 6, 7,
         1, 2, 3, 4, 5, 6, 7,
         1, 2, 3, 4, 5, 6, 7,
         1, 2, 3, 4, 5, 6, 7)

per <- c(1.6, 1.3, 2.26, 7.32, 26.94, 31.13, 29.46, 
         0.49, 0.56, 0.61, 4.25, 17.71, 29.82, 46.57, 
         2.57, 3.86, 5.15, 12.81, 26.75, 23.73, 25.13, 
         2.09713, 2.317881, 3.97351, 11.479029, 20.198675, 23.289183, 36.644592, 
         3.571429, 7.397959, 7.653061, 15.05102, 34.438776, 22.44898, 9.438776, 
         2.658487, 3.885481, 5.316973, 13.496933, 32.719836, 25.562372, 16.359918)

mydata <- data.frame(pop, res, per)

然后,确保加载正确的库。

library(tidyverse)
library(ggplot2)

接下来,将响应选项转换为文本响应。

## translate to discrete response options
mydata <- mutate(mydata, tres = recode(res, "1" = "Decreased greatly",
                                       "2" = "Decreased moderately",
                                       "3" = "Decreased somewhat",
                                       "4" = "Stayed about the same",
                                       "5" = "Increased somewhat",
                                       "6" = "Increased moderately",
                                       "7" = "Increased greatly"))

你可以在这里看到数据的结构

head(mydata)
> head(mydata)
   pop res   per                  tres
1 hunt   1  1.60     Decreased greatly
2 hunt   2  1.30  Decreased moderately
3 hunt   3  2.26    Decreased somewhat
4 hunt   4  7.32 Stayed about the same
5 hunt   5 26.94    Increased somewhat
6 hunt   6 31.13  Increased moderately

然后,生成图表。提醒 - 选择的颜色对色盲友好,所以我承诺使用所选的颜色。

leg_ord <- levels(with(mydata, reorder(tres, per)))

ggplot()+
  geom_bar(data = mydata, aes(x = reorder(pop,per), y=per, fill=tres), position="stack", stat="identity")+
  coord_flip() + 
  ggtitle("perception of population change")+
  ylab("Percentage")+
  xlab("population")+
  #  scale_fill_brewer(palette="PRGn")+
  theme(legend.position="bottom")+ 
  scale_fill_manual(breaks=leg_ord, 
                    values=c("Decreased greatly"="#D55E00", 
                             "Decreased moderately"="#CC79A7",
                             "Decreased somewhat"="#E69F00",
                             "Stayed about the same"="#999999",
                             "Increased somewhat"="#0072B2",
                             "Increased moderately"="#56B4E9", 
                             "Increased greatly"="#009E73"))

生成的图表如下所示。不幸的是,“保持不变”位于图表的左侧,而不是居中。真的,堆叠条形图中的所有条形都排列错误。我希望它们以与图例相同的方式订购。

如何固定图例和条形的顺序?我试过阅读其他几篇文章,但总是把图表弄乱了。请指教。

谢谢!

ETA 1 - 我最初包含了错误的图像和 ggplot 代码,但现在已修复。

ETA 2 - 这是我最终要使用的最终代码。我想把它传递下去,以防其他人将来看到这个并且有类似的数据/问题。感谢那些评论帮助我解决问题的人 - 非常感谢,如果没有你的帮助,我不会弄明白的。

leg_ord <- levels(with(mydata, reorder(tres, res)))
mydata$tres <- factor(mydata$tres, rev(leg_ord))

ggplot()+
  geom_bar(data = mydata, aes(x = reorder(pop,per), y=per, fill=tres), position="stack", stat="identity")+
  coord_flip() + 
  ggtitle("perception of population change")+
  ylab("Percentage")+
  xlab("population")+
  #  scale_fill_brewer(palette="PRGn")+
  theme(legend.position="bottom")+ 
  scale_fill_manual(breaks=leg_ord, 
                    values=c("Decreased greatly"="#D55E00", 
                             "Decreased moderately"="#CC79A7",
                             "Decreased somewhat"="#E69F00",
                             "Stayed about the same"="#999999",
                             "Increased somewhat"="#0072B2",
                             "Increased moderately"="#56B4E9", 
                             "Increased greatly"="#009E73"))

生成的图表如下所示:

我喜欢它非常清楚多数意见是什么(中性选项向左多远),并且我认为规模两端的数据都非常清楚地呈现出来。

再次感谢!

【问题讨论】:

  • 设置您的数据,使fill 变量成为具有适当水平的因子:mydata$tres &lt;- factor(mydata$tres, rev(leg_ord))
  • 谢谢@Axeman!这很好用。

标签: r ggplot2 tidyverse


【解决方案1】:

这种方法有助于分散条形图。可能与您想要的结果有点不同

mydata <- mutate(mydata, tres = recode(res, "1" = "Decreased greatly",
                                       "2" = "Decreased moderately",
                                       "3" = "Decreased somewhat",
                                       "4" = "Stayed about the same",
                                       "5" = "Increased somewhat",
                                       "6" = "Increased moderately",
                                       "7" = "Increased greatly")) %>% 
  mutate(favorable = case_when(
    tres %in% c('Stayed about the same', 
                'Increased somewhat',  
                'Increased moderately', 
                'Increased greatly') ~ 'y', 
                TRUE ~ 'n'
  )) %>% 
  mutate(diverPercent = case_when(
    favorable == 'y' ~ per, 
    TRUE ~ -per
  ))

leg_ord <- levels(with(mydata, reorder(tres, per)))

ggplot()+
  geom_bar(data = filter(mydata, diverPercent > 0), aes(x = reorder(pop,diverPercent), y=diverPercent, fill=tres), position="stack", stat="identity") +
  geom_bar(data = filter(mydata, diverPercent < 0), aes(x = reorder(pop,diverPercent), y=diverPercent, fill=tres), position="stack", stat="identity") +
  coord_flip() + 
  ggtitle("perception of population change")+
  ylab("Percentage")+
  xlab("population")+
  theme(legend.position="bottom")+ 
  scale_fill_manual(breaks=leg_ord, 
                    values=c("Decreased greatly"="#D55E00", 
                             "Decreased moderately"="#CC79A7",
                             "Decreased somewhat"="#E69F00",
                             "Stayed about the same"="#999999",
                             "Increased somewhat"="#0072B2",
                             "Increased moderately"="#56B4E9", 
                             "Increased greatly"="#009E73")) +
  theme(legend.direction = 'horizontal')+
  guides(fill = guide_legend(nrow = 1))

【讨论】:

  • 谢谢!很有意思。有没有办法让“保持不变”的反应居中?我可以看到该表单将非常有用,但如果“保持不变”居中则更是如此。
猜你喜欢
  • 2023-01-19
  • 1970-01-01
  • 2014-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多