简体   繁体   English

R 将频率计数标签添加到 ggplot geombar

[英]R add frequency count labels to ggplot geombar

I have a plot like this:我有一个像这样的 plot :

在此处输入图像描述

with the following code:使用以下代码:

df.m <- reshape2::melt(ngli,id.vars = "Hugo_Symbol") df.m <- reshape2::melt(ngli,id.vars = "Hugo_Symbol")

plot <- ggplot(df.m, aes(x = Hugo_Symbol, y = value, fill=variable)) +
  geom_bar(stat='identity') + 
  #geom_text(aes(label=len), vjust=1.6, color="white", size=3.5)+
  #geom_text(stat = "count", aes(label = after_stat(count)), vjust = -1)
  theme(axis.text.x=element_text(angle = 90, vjust = 0.5))

And I have tried, with the commented code, add the count for each label, something like that:我已经尝试使用注释代码添加每个 label 的计数,如下所示:

在此处输入图像描述

but it does not work.但它不起作用。 Any ideas?有任何想法吗? thanks!谢谢!

Without a reproducible example of your dataset, it it hard to be sure of what will be the solution to your question.如果没有可重现的数据集示例,很难确定您的问题的解决方案是什么。

However, based on your code, it looks like you are trying to get a barchart representing the sum of all values observed for each x values (with geom_bar(stat = "identity") ) and add the number of observation for each categories and each x values with geom_text .但是,根据您的代码,您似乎正在尝试获取一个条形图,表示为每个 x 值观察到的所有值的总和(使用geom_bar(stat = "identity") )并添加每个类别的观察次数和每个x 值与geom_text

A possible solution is to calculate those values outside of ggplot2 .一种可能的解决方案是在ggplot2之外计算这些值。 Here, an example using iris dataset.这里,一个使用iris数据集的例子。 You can first reshape your data in a longer format (as you did with melt ) and then calculate the sum and the number of observations of grouped variables and categories (I add the mean just for fun)您可以首先以更长的格式重塑您的数据(就像您对melt所做的那样),然后计算分组变量和类别的总和和观察次数(我添加平均值只是为了好玩)

library(tidyr)
library(dplyr)

df <- iris %>% pivot_longer(-Species, names_to = "variable", values_to = "val") %>%
  group_by(Species, variable) %>%
  summarise(Mean = mean(val),
            Sum = sum(val),
            count = n())

# A tibble: 12 x 5
# Groups:   Species [3]
   Species    variable      Mean   Sum count
   <fct>      <chr>        <dbl> <dbl> <int>
 1 setosa     Petal.Length 1.46   73.1    50
 2 setosa     Petal.Width  0.246  12.3    50
 3 setosa     Sepal.Length 5.01  250.     50
 4 setosa     Sepal.Width  3.43  171.     50
 5 versicolor Petal.Length 4.26  213      50
 6 versicolor Petal.Width  1.33   66.3    50
 7 versicolor Sepal.Length 5.94  297.     50
 8 versicolor Sepal.Width  2.77  138.     50
 9 virginica  Petal.Length 5.55  278.     50
10 virginica  Petal.Width  2.03  101.     50
11 virginica  Sepal.Length 6.59  329.     50
12 virginica  Sepal.Width  2.97  149.     50

Then, you can plot your bargraph as follow by using geom_col (which is the same than writing geom_bar(stat = "identity") and place your label on the stacked barchart by using position_stack function:然后,您可以 plot 您的条形图如下使用geom_col (这与编写geom_bar(stat = "identity")相同)并使用position_stack ZC1C425268E68A974 将您的 label 放在堆叠条形图上

library(ggplot2)

ggplot(df, aes(x = variable, y = Sum, fill = Species))+
  geom_col()+
  geom_text(aes(label = count),position = position_stack(0.5))

在此处输入图像描述

Does it answer your question?它回答了你的问题吗?

If not, please provide a reproducible example of your dataset by folloing this guide: How to make a great R reproducible example如果没有,请按照本指南提供数据集的可重现示例: 如何制作出色的 R 可重现示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM