在 R 中,如何使用 Count 的列值绘制频率分布图

in R how do you graph frequency distribution using column value for Count

我有一个数据框,其中的指标作为列的值

X     Y Ind 
1  10000  N
2  10000  N
3  10000  G
4  10000  L

我想使用 ggplot 创建一个条形图,它将根据指标值并排显示总计数和总 Y。 我试图弄清楚如何在数据帧上实现聚合而不对其进行汇总并为每个 Ind

的分类值创建一个计数值

更新:这个

一个选项是在按 'Ind'、gather(来自 tidyr 将其重塑为 'long' 格式)并使用 geom_bar 获取条形图(来自 ggplot2)。

library(dplyr)
library(tidyr)
library(ggplot2)
df1 %>%
  group_by(Ind) %>% 
  summarise(Count=n(), TotalY = sum(Y)) %>% 
  gather(Var, Val, -Ind) %>% 
  ggplot(., aes(x=Ind, y = Val, fill=Var)) +
            geom_bar(stat="identity", position="dodge")