本文介绍了ggplot2排序情节第二部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个融化的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 -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 freq1 a Never Heard of 102 a Heard of but Not at all Familiar 243 a Somewhat Familiar 324 a Familiar 905 a Very Familiar 976 a Extremely Familiar 697 b Never Heard of 158 b Heard of but Not at all Familiar 579 b Somewhat Familiar 7910 b Familiar 9411 b Very Familiar 5812 b Extremely Familiar 1913 c Never Heard of 1114 c Heard of but Not at all Familiar 1715 c Somewhat Familiar 3416 c Familiar 8117 c Very Familiar 9418 c Extremely Familiar 8519 d Never Heard of 420 d Heard of but Not at all Familiar 2821 d Somewhat Familiar 5922 d Familiar 11423 d Very Familiar 8224 d Extremely Familiar 35Now, 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")QuestionWhat 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排序情节第二部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
06-20 10:31