我正在尝试按照与每个值相关联的观察次数重新排序分类/非数字y轴。
我导入一个如下所示的表:
View(ranges)
ID | Time | Value | obs
B1 | 1:11 | 1 | 91
B1 | 1:12 | 2 | 91
...
U30 | 2:22 | 99 | 101
然后重新排序:
ranges <- ranges[with(ranges, order(obs, ID)), ]
然后绘制:
p <- ggplot(ranges, aes(x=Time, y=ID, fill=Value))
p <- p + geom_tile(colour = "white")
p
但是y轴上的顺序并不反映每个ID的观察次数。
如何对y轴进行排序以反映数据框中ID的频率?谢天谢地。
示例数据,逗号分隔:
ID,DATE,VALUE,OBS
B9148,41275,0133,7
B9148,41276,3344,7
B9148,41277,7907,7
B9148,41278,2052,7
B9148,41279,6617,7
B9148,41280,9411,7
B9148,41281,6302,7
U1821,41275,7585,11
U1821,41276,5086,11
U1821,41277,5731,11
U1821,41278,5358,11
U1821,41279,9977,11
U1821,41280,1446,11
U1821,41281,2728,11
U1821,41282,5369,11
U1821,41283,8171,11
U1821,41284,5817,11
U1821,41285,0166,11
F7484,41275,8971,10
F7484,41276,5635,10
F7484,41277,8629,10
F7484,41278,5832,10
F7484,41279,4775,10
F7484,41280,4519,10
F7484,41281,1527,10
F7484,41282,7861,10
F7484,41283,5713,10
F7484,41284,0824,10
S3804,41275,0573,4
S3804,41276,0123,4
S3804,41277,8468,4
S3804,41278,6258,4
B8088,41275,9117,7
B8088,41276,8489,7
B8088,41277,0902,7
B8088,41278,8684,7
B8088,41279,7229,7
B8088,41280,3587,7
B8088,41281,0907,7
答案 0 :(得分:1)
您可以手动执行此操作:
ranges$ID <- factor(ranges$ID, levels=c("S3804", "B8088", "B9148", "F7484", "U1821"))
或多或少自动:
ranges$ID <- factor(ranges$ID, levels=as.character(unique(ranges$ID)))
为什么会发生这种情况的基本思路是因为因子水平是单独存储的,不受排序的影响,而ggplot则逐级绘制。
您还可以参考this问题获取有关此概念的演示。
这是你想要的吗?请注意,如果您想要反向顺序,只需在as.character()之前添加rev()。