【问题标题】:Customise the order of bar segments in a ggplot with geom_bar fill使用 geom_bar 填充自定义 ggplot 中条形段的顺序
【发布时间】:2019-11-13 12:44:43
【问题描述】:

我正在尝试制作一个图表,显示不同音乐风格的歌曲文本中的情感比例。我有这张图表,它已经很令人满意了,但是如果我可以选择显示这些情绪的顺序,我可以将积极的东西放在一个颜色中,将消极的东西放在另一个范围内,这样会更容易阅读。颜色。

Chart: Emotions proportions in songtext by music style 我试图在绘图之前重新排列情绪,但 ggplot 似乎重新排列它们,我不知道为什么。

    datasorted <- datasorted %>% arrange(factor(track.lyrics.predominantsentiment,
                                            levels = c("joy","trust","disgust",
                                                       "fear","anger","sadness")))


datasorted %>% filter(track.tag %in% c("Rap","R&B", "Pop", "Rock")) %>% group_by(track.tag) %>%
  ggplot(aes(x=track.tag, fill=track.lyrics.predominantsentiment,
  order=track.lyrics.predominantsentiment)) + 
  geom_bar(position = "fill")

那个订单条件对图表没有影响,我试过有没有。

有人知道如何更改订单吗?以及如何选择条形部分的颜色?

非常感谢

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    如果您提供reproducible example 来解决您的问题,将会更容易为您提供帮助。

    您不仅需要排列数据,还需要更改数据的级别顺序:

          datasorted <- datasorted %>% 
            mutate(track.lyrics.predominantsentiment = 
                   fct_relevel(track.lyrics.predominantsentiment, 
                               c("joy","trust","disgust", "fear","anger","sadness")))
    

    现在可以使用scale_fill_manual 等比例更改颜色。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-23
      • 1970-01-01
      • 1970-01-01
      • 2021-06-15
      • 2019-08-19
      • 1970-01-01
      • 2019-07-09
      • 1970-01-01
      相关资源
      最近更新 更多