r - 如何在 R 中使用 ggplot 绘制排序的频率条形图?

标签 r ggplot2 bar-chart frequency

<分区>

我有一张看起来像这样的学生 table ,

   StudentID SectorID ClassID
1          A   Team_1 Class_1
2          A   Team_1 Class_1
3          B   Team_1 Class_1
4          B   Team_2 Class_1
5          B   Team_2 Class_1
6          A   Team_2 Class_1
7          A   Team_3 Class_1
8          C   Team_3 Class_2
9          C   Team_3 Class_2
10         C   Team_3 Class_2
11         C   Team_3 Class_2
12         C   Team_1 Class_2
13         D   Team_1 Class_2
14         D   Team_1 Class_2

你可以通过这个看到,

stg <- data.frame(StudentID = c( rep("A", 2), rep("B", 3), rep("A", 2), rep("C", 5), rep("D", 2)  ),
                  SectorID  = c(rep("Team_1", 3), rep("Team_2", 3), rep("Team_3", 5), rep("Team_1", 3)),               
                  ClassID     = c(rep("Class_1", 7), rep("Class_2", 7) )            
)


stg

ggplot(stg, aes( x = stg$StudentID) ) + geom_bar()

现在,我想实现一个排序的条形图。其中 C(频率为 5)将首先出现,依此类推。你能在这上面遮一些灯吗?感谢您的时间。

最佳答案

你可以这样做..

ggplot(stg, aes( x = reorder(StudentID,StudentID,function(x)-length(x)))) + geom_bar()

enter image description here

关于r - 如何在 R 中使用 ggplot 绘制排序的频率条形图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37853539/

相关文章:

r - annotation_custom 与 ggplot2 中的 npc 坐标

r - 如何避免在函数中重复代码以使用ggplot2绘制密度直方图和简单直方图?

python - 如何在条形图上添加值标签

r - 标准化 ggplot 中覆盖密度图的 x 尺度

python - 使用 python/pandas/seaborn 将分类数据绘制成相对频率的单个条形图

php - 如何使用 fpdf php 创建双条形图?

r - loop_apply.o : file not recognized: File format not recognized

r - 为什么 `[` 比 `subset` 更好?

r - 按行有效地应用或映射多个矩阵参数

r - UMAP 图中的凸包 - R