ggplot2排序情节第二部分 [英] ggplot2 sorting a plot Part II
本文介绍了ggplot2排序情节第二部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个融化的data.frame,dput(x),如下所示:
## dput(x)$ b (1L,1L,1L,1L,1L,1L,
2L,2L,2L,2L,2L,2L,3L,3L,3L,3L ,3L,3L,4L,4L,4L,4L,
4L,4L),.Label = c(a,b,c,d),class =factor) ,
值=结构(c(1L,
2L,3L,4L,5L,6L,1L,2L,3L,4L,5L,6L,1L,2L,3L,4L,5L,
6L,1L,2L,3L,4L,5L,6L),.Label = c(从来没有听说过,
听过但根本不熟悉,
有点熟悉(10L,24L,32L,90L,97L,69L,
15L, 57L,79L,94L,58L,19L,11L,17L,34L,81L,94L,85L,4L,
28L,59L,114L,82L,35L)),.Name = c(variable值,freq
),row.names = c(NA,-24L),class =data.frame)
看起来像这样(对于那些不需要测试集的人):
变量价值频率
1 a从未听过10
2听到但根本不熟悉24
3有点熟悉32
4 a熟悉90
5 a非常熟悉97
6非常熟悉69
7 b从未听过15
8 b听过但根本不熟悉57
9 b有点熟悉79
10 b熟悉94
11 b非常熟悉58
12 b非常熟悉19
13 c从未听过11
14 c听说过,但根本不熟悉17
15 c有点熟悉34
16 c熟悉81
17 c非常熟悉94
18 c极值ely熟悉85
19 d从未听说过4
20 d听说过,但根本不熟悉28
21 d有点熟悉59
22 d熟悉114
23 d非常熟悉82
24 d非常熟悉35
现在,我可以做一个漂亮而漂亮的与此类似:
ggplot(x,aes(variable,freq,fill = value))+
geom_bar (position =fill)+
coord_flip()+
scale_y_continuous(,formatter =percent)
问题
我想要做的是按a,b,c,d由最高到最低的freq 非常熟悉
?relevel
和?reorder
没有提供这种用法的任何建设性的例子。
您的帮助,我们将不胜感激。
干杯,
BEB
解决方法
以下是一种方法:
tmpfun< - function(i){
tmp < - x [i,]
-tmp [tmp $ value =='非常熟悉','freq']
}
x $变量< - reorder( x $变量,1:nrow(x),tmpfun)
I have a melted data.frame, dput(x), below:
## dput(x)
x <- structure(list(variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L,
4L, 4L), .Label = c("a", "b", "c", "d"), class = "factor"),
value = structure(c(1L,
2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L,
6L, 1L, 2L, 3L, 4L, 5L, 6L), .Label = c("Never Heard of",
"Heard of but Not at all Familiar",
"Somewhat Familiar", "Familiar", "Very Familiar", "Extremely Familiar"
), class = "factor"), freq = c(10L, 24L, 32L, 90L, 97L, 69L,
15L, 57L, 79L, 94L, 58L, 19L, 11L, 17L, 34L, 81L, 94L, 85L, 4L,
28L, 59L, 114L, 82L, 35L)), .Names = c("variable", "value", "freq"
), row.names = c(NA, -24L), class = "data.frame")
Which looks like this (for those of you who don't need a test set):
variable value freq
1 a Never Heard of 10
2 a Heard of but Not at all Familiar 24
3 a Somewhat Familiar 32
4 a Familiar 90
5 a Very Familiar 97
6 a Extremely Familiar 69
7 b Never Heard of 15
8 b Heard of but Not at all Familiar 57
9 b Somewhat Familiar 79
10 b Familiar 94
11 b Very Familiar 58
12 b Extremely Familiar 19
13 c Never Heard of 11
14 c Heard of but Not at all Familiar 17
15 c Somewhat Familiar 34
16 c Familiar 81
17 c Very Familiar 94
18 c Extremely Familiar 85
19 d Never Heard of 4
20 d Heard of but Not at all Familiar 28
21 d Somewhat Familiar 59
22 d Familiar 114
23 d Very Familiar 82
24 d Extremely Familiar 35
Now, I can make a nice and pretty plot akin to this:
ggplot(x, aes(variable, freq, fill = value)) +
geom_bar(position = "fill") +
coord_flip() +
scale_y_continuous("", formatter="percent")
Question
What I would like to do is sort a,b,c,d by the highest to lowest "freq" of "Extremely Familiar"
?relevel
and ?reorder
haven't provided any constructive examples for this usage.
Your help, is always appreciated.
Cheers,
BEB
解决方案
Here is one way:
tmpfun <- function(i) {
tmp <- x[i,]
-tmp[ tmp$value=='Extremely Familiar', 'freq' ]
}
x$variable <- reorder( x$variable, 1:nrow(x), tmpfun )
这篇关于ggplot2排序情节第二部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文