组内的R ggplot排序条 [英] R ggplot ordering bars within groups

查看:85
本文介绍了组内的R ggplot排序条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ggplot在R中格式化分组的条形图,以使条形按组的降序排列.这是我目前的情节:

I'm attempting to format a grouped bar plot in R with ggplot such that bars are in decreasing order per group. This is my current plot:

基于此数据框:

> top_categories
                    Category Count Community
1         Singer-Songwriters   151         1
2          Adult Alternative   147         1
3                  Dance Pop    95         1
4                       Folk    89         1
5         Adult Contemporary    88         1
6                    Pop Rap   473         2
7         Gangsta & Hardcore   413         2
8                       Soul   175         2
9                 East Coast   170         2
10                West Coast   135         2
11 Album-Oriented Rock (AOR)   253         3
12        Singer-Songwriters   217         3
13                 Soft Rock   196         3
14                      Folk   145         3
15        Adult Contemporary   106         3
16                      Soul   278         4
17                     Blues   137         4
18                      Funk   119         4
19               Quiet Storm    76         4
20                 Dance Pop    74         4
21             Indie & Lo-Fi   235         5
22                Indie Rock   234         5
23         Adult Alternative   114         5
24          Alternative Rock    49         5
25        Singer-Songwriters    47         5

使用以下代码创建:

ggplot(
  top_categories,
  aes(
    x=Community,
    y=Count,
    group=Category,
    label=Category
  )
) +
  geom_bar(
    stat="identity",
    color="black",
    fill="#9C27B0",
    position="dodge"
  ) +
  geom_text(
    angle=90,
    position=position_dodge(width=0.9),
    hjust=-0.05
  ) +
  ggtitle("Number of Products in each Category in Each Community") +
  guides(fill=FALSE)

根据相关文章的建议,我尝试使用reorder函数并将Count转换为一个因子,两者的结果似乎都破坏了条形与文本的顺序或重新缩放了以这种荒谬的方式(带有因素)进行绘图:

Based on suggestions from related posts, I've attempted to use the reorder function and turn the Count into a factor, both with results that seem to break the ordering of the bars vs. the text or rescale the plot in a nonsensical way such as this (with factors):

关于如何完成组内订购的任何提示?谢谢!

Any tips on how I might accomplish this in-group ordering? Thanks!

推荐答案

按类别分组时,条形根据数据框中类别的出现顺序进行排序.这对于社区1和社区2很好用,因为您已经通过减少计数对行进行了排序.但是在社区3中,由于类别歌手和作曲家"是数据框中第一个出现的类别,因此放在第一位.

When you group by Category, the bars are ordered according to the order of appearance of Categories in the dataframe. This works fine for Community 1 and 2 as your rows are already ordered by decreasing Count. But in Community 3, as Category "Singer-Songwriters" is the first occcurring Category in the dataframe, it is put first.

按ID变量分组可以解决问题:

Grouping instead by an Id variable resolves the problem:

top_categories$Id=rep(c(1:5),5)

ggplot(
  top_categories,
  aes(
    x=Community,
    y=Count,
    group=Id,
    label=Category
  )
) +
  geom_bar(
    stat="identity",
    color="black",
    fill="#9C27B0",
    position="dodge"
  ) +
  geom_text(
    angle=90,
    position=position_dodge(width=0.9),
    hjust=-0.05
  ) +
  ggtitle("Number of Products in each Category in Each Community") +
  guides(fill=FALSE)

这篇关于组内的R ggplot排序条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆