本文介绍了ggplot2:排序图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个data.frame,从最高到最低排序。例如: x 9L,5L,1L,7L),.Label = c(a,b,c,d,e,f,g,$ (0.990683229813665, 0.975155279503106,0.928571428571429,0.807453416149068,0.717391304347826,$ b(b $ bh,i,j),class = c(ordered,factor $ b 0.388198757763975,0.357142857142857,0.20863354037267,0.1173913043478261 0.0496894409937888)),.Names = c(variable,value),row.names = c(10L, 6L,3L,4L, 2L,8L,9L,5L,1L,7L),class =data.frame) ggplot(x,aes(x = variable,y = value))+ geom_bar()+ scale_y_continuous(,formatter =percent)+ coord_flip() 现在,数据是很好的和排序的,但是当我绘制时,它会按照因子排序。这很烦人,我该如何解决它?解决方案以下是几种方法。 x $ variable 第一个将根据在数据框中看到的顺序排序: - 因子(x $ variable,levels = unique(as.character(x $ variable))) 第二个命令基于另一个变量(在这种情况下为值)的级别: x I have a data.frame, that is sorted from highest to lowest. For example: x <- structure(list(variable = structure(c(10L, 6L, 3L, 4L, 2L, 8L,9L, 5L, 1L, 7L), .Label = c("a", "b", "c", "d", "e", "f", "g","h", "i", "j"), class = c("ordered", "factor")), value = c(0.990683229813665,0.975155279503106, 0.928571428571429, 0.807453416149068, 0.717391304347826,0.388198757763975, 0.357142857142857, 0.201863354037267, 0.173913043478261,0.0496894409937888)), .Names = c("variable", "value"), row.names = c(10L,6L, 3L, 4L, 2L, 8L, 9L, 5L, 1L, 7L), class = "data.frame")ggplot(x, aes(x=variable,y=value)) + geom_bar() + scale_y_continuous("",formatter="percent") + coord_flip()Now, the data is nice and sorted, but when I plot, it comes out sorted by factor. It's annoying, how do I fix it? 解决方案 Here are a couple of ways.The first will order things based on the order seen in the data frame:x$variable <- factor(x$variable, levels=unique(as.character(x$variable)) )The second orders the levels based on another variable (value in this case):x <- transform(x, variable=reorder(variable, -value) ) 这篇关于ggplot2:排序图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-18 09:29