按观察次数重新排序y轴

时间:2021-08-27 08:31:53

I'm trying to re-order a categorical/non-numeric y-axis by the number of observations associated with each value.

我正在尝试按照与每个值关联的观察次数重新排序分类/非数字y轴。

I import a table that looks like this:

我导入一个如下所示的表:

View(ranges)

ID  | Time | Value | obs
B1  | 1:11 | 1     | 91
B1  | 1:12 | 2     | 91
...
U30 | 2:22 | 99    | 101

Then reorder:

然后重新排序:

ranges <- ranges[with(ranges, order(obs, ID)), ]

Then plot:

然后情节:

p <- ggplot(ranges, aes(x=Time, y=ID, fill=Value))
p <- p + geom_tile(colour = "white")
p

But the order on the y-axis does not reflect the number observations per ID.

但是y轴上的顺序并不反映每个ID的观测数量。

How can I sort the y-axis to reflect the frequency of the ID in the dataframe? Thanks kindly.

如何对y轴进行排序以反映数据帧中ID的频率?谢天谢地。

Sample data, comma delimited:

示例数据,逗号分隔:

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

1 个解决方案

#1


1  

You can do this either manually:

您可以手动执行此操作:

ranges$ID <- factor(ranges$ID, levels=c("S3804", "B8088", "B9148", "F7484", "U1821"))

or more or less automatically:

或多或少自动:

ranges$ID <- factor(ranges$ID, levels=as.character(unique(ranges$ID)))

The basic idea of why this happens is because factor levels are stored separately and are not affected by ordering, whereas ggplot plots level by level.

发生这种情况的基本思路是因为因子水平是单独存储的,不受排序影响,而ggplot逐级绘制。

You can also refer to this question for a demo on this concept.

您也可以参考这个问题来获得有关此概念的演示。

按观察次数重新排序y轴

Is this what you wanted? Note that if you want reverse order, just add rev() before as.character().

这是你想要的吗?请注意,如果您想要反向顺序,只需在as.character()之前添加rev()。

#1


1  

You can do this either manually:

您可以手动执行此操作:

ranges$ID <- factor(ranges$ID, levels=c("S3804", "B8088", "B9148", "F7484", "U1821"))

or more or less automatically:

或多或少自动:

ranges$ID <- factor(ranges$ID, levels=as.character(unique(ranges$ID)))

The basic idea of why this happens is because factor levels are stored separately and are not affected by ordering, whereas ggplot plots level by level.

发生这种情况的基本思路是因为因子水平是单独存储的,不受排序影响,而ggplot逐级绘制。

You can also refer to this question for a demo on this concept.

您也可以参考这个问题来获得有关此概念的演示。

按观察次数重新排序y轴

Is this what you wanted? Note that if you want reverse order, just add rev() before as.character().

这是你想要的吗?请注意,如果您想要反向顺序,只需在as.character()之前添加rev()。