【问题标题】:Stacked bar graph with only top/bottom results只有顶部/底部结果的堆叠条形图
【发布时间】:2018-03-20 03:31:08
【问题描述】:

我有一个 Excel 文件,我正在尝试创建一个条形图,用于对类别进行分组并显示该类别的平均评分。因为有很多类别,我还想在生成的水平条形图中只显示前 10 名或后 10 名。

 category rating
 A        10
 A        8
 A        9
 B        1
 B        4
 B        9
 C        6
 C        7
 D        9

类似这样的东西(代表栏而不是数字):

A 9
D 9
...
C 6.5
B 4.66

我知道这看起来非常简单,但是在这里尝试了各种答案后,我似乎无法得到任何工作。到目前为止,使用 ggplot2 似乎是最有希望的。我得到的最接近的是显示每个类别的评分数量...

编辑:没有保存我之前所做的工作,因为它不是我想要的结果,但它是这样的(没有使用 ggplot)

dat[,c(1,12)]
category = dat[,1] //selecting column from sheet
rating = dat[,12] //selecting column from sheet
rating<-as.numeric(unlist(dat[,12]))
dat<-table(dat$rating,dat$category)
barplot(dat, main="Overall Ratings",
        xlab="Ratings", col=c("skyblue","red"), horiz=TRUE,
        legend = rownames(dat))

【问题讨论】:

  • 首先,展示你到目前为止所拥有的东西。您提到您可以显示评分数量,向我们展示您是如何做到的。此外,此示例不可重现。请参阅此内容:stackoverflow.com/questions/5963269/…
  • @RicardoFernandesCampos 我没有保存该作品,因为它不是我想要的结果,但我添加了它的样子。

标签: r ggplot2


【解决方案1】:

这是使用dplyr 和tidyr 的链接解决方案。首先,我们需要加载数据。

library(dplyr)
library(tidyr)
library(ggplot2)
df <- read.table(text="category,rating
 A,10
 A,8
 A,9
 B,1
 B,4
 B,9
 C,6
 C,7
 D,9
", sep=",", header=TRUE)

现在来解决问题。在将数据按category 分组后,我们计算每个类别的平均评分。

means.df <- 
df %>%
    group_by(category) %>%
    summarise(mean = mean(rating))

top_n 从数据集中选择顶部(正数)或底部(负数)n 行。我们通过手段将其应用于我们的数据集。在您的真实数据中,将前 10 个类别的 2 调整为 10,将后 10 个类别调整为 -10。

means.df %>%
    top_n(2, mean) %>%
    ggplot(aes(x = category, y = mean)) +
    geom_bar(stat = 'identity')

以下代码将顶部/底部cutoff_number 类别绘制到一个图中。根据需要调整变量cutoff_number。

cutoff_number <- 2
means.df %>%
    arrange(-mean) %>%
    mutate(
        topbottom = ifelse(row_number() <=  cutoff_number, "top", NA),
        topbottom = ifelse(row_number() > nrow(.) - cutoff_number, "bottom", topbottom)
    ) %>%
    ggplot(aes(x = category, y = mean)) +
    geom_bar(stat = 'identity') +
    facet_wrap(~topbottom, scales = 'free_x')

【讨论】:

  • 谢谢!我刚刚在我的数据中注意到某些类别只有一个评分,所以我想排除评分低于 5 个的类别。我如何过滤掉那些?搜索告诉我我可以执行类似 dat %>% group_by("category") %>% filter(length(category)>=5) 的操作,但由于某种原因它仍然包含所有内容。
  • 过滤工作正常。我还尝试使用 coord_flip() 将结果图水平翻转,但它还包括作为 NA 一部分的顶部/底部图中的类别(因此有一堆空白)。如何仅显示水平图中顶部/底部的那些? (我使用 drop_na() 完全删除了 NA)
  • 你如何过滤?如果你使用这样的东西,数据中不应该有 NA:means.df2 &lt;- df %&gt;% group_by(category) %&gt;% summarise( mean = mean(rating), n = n() ) %&gt;% filter(n &gt; 1) %&gt;% select(-n)。当然,如果您执行coord_flip(),则两个图表将共享 y 轴,因此包括所有类别。
  • 我在 group_by 之后添加了 filter(length(airline_name)>=10) %>%。有没有不包括所有类别的翻转方法?
  • 在这种情况下,您可以使用scales = 'free_y' 作为facet_wrap() 的参数。
【解决方案2】:

此方案使用 data.table 汇总数据,然后将结果传递给 ggplot:

library(data.table);library(ggplot2)
category=c("A","A","A","B","B","B","C","C","D")
rating=c(10,9,8,1,4,9,6,7,9)
dt=as.data.table(cbind(category,rating))
ggplot(dt[,mean(as.numeric(rating)),by=category],aes(category,V1))+geom_col()+ylab("Mean")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-11
    • 2021-11-20
    • 2023-02-10
    • 1970-01-01
    • 2023-02-21
    • 1970-01-01
    • 2022-08-19
    相关资源
    最近更新 更多