本文介绍了修正ggplot中的数据顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我在用ggplot绘制数据时遇到了一个问题。我无法使每个方面内的数据都能正确排序。我的样本数据是: data< - structure(list(Parameter = c({0.1,0.7,0.0,0.2 },{0.2,0.7,0.0,0.1},{0.3,0.7,0.0,0.0},{0.0,0.7,0.0,0.3},{0.0,0.6,0.0} ,0.4},{0.1,0.6,0.0,0.3},{0.2,0.6,0.0,0.2},{0.3,0.6,0.0,0.1}, {0.4,0.6,0.0,0.0},{0.1,0.3,0.2,0.4},{0.1,0.7,0.0,0.2},{0.2,0.7,0.0,0.1} ,{0.3,0.7,0.0,0.0},{0.0,0.7,0.0,0.3},{0.1,0.3,0.2,0.4},{0.1,0.5,0.1,0.3 },{0.2,0.5,0.1,0.2},{0.3,0.3,0.2,0.2},{0.4,0.3,0.2,0.1},{0.5,0.3,0.2} ,0.0,{0.1,0.6,0.0,0.3},{0.2,0.6,0.0,0.2},{0.4,0.6,0.0,0.0}, {0.3,0.6,0.0,0.1},{0.0,0.6,0.0,0.4},{0.1,0.7,0.0,0.2},{0.2,0.7,0.0,0.1} ,{0.3,0.7,0.0,0.0},{0.0,0.7,0.0,0.3},{0.1,0.4,0.1,0.4}),Map = c(0.19608779,0.19608779, 0.19581, 0.19490847,0.18973944,0.18943608,0.18943608,0.18915829,0.18915829, 0.188562 0.20025444,0.220025444,0.199997665,0.199907513,0.119272882, 0.19250154,0.19250154,0.119249977,0.19249977,0.19249977,0.19608779, 0.19608779,0.19585875,0.119581,0.1952106,0.194046278,0.194046278, 0.19023374, Sigma = 370,Sigma = 370,Sigma = 370,Sigma = 370,Sigma = 370,Sigma = 370, b $Sigma = 370,Sigma = 370,Sigma = 370,Sigma = 370,Sigma = 380,Sigma = 380,Sigma = 380 Sigma = 380,Sigma = 380,Sigma = 380,Sigma = 380,Sigma = 380,Sigma = 380,Sigma = 380,Sigma = 390 ,Sigma = 390,Sigma = 390,Sigma = 390,Sigma = 390,Sigma = 390,Sigma = 390,Sigma = 390 ,Sigma = 390,Sigma = 390)),.Names = c(Parameter,Map,Sigma),class =data.frame,row.names = c (NA,-30L )) 数据参数图Sigma 1 {0.1,0.7,0.0,0.2} 0.1960878 Sigma = 370 2 {0.2,0.7, 0.0,0.1} 0.1960878 Sigma = 370 3 {0.3,0.7,0.0,0.0} 0.1958100 Sigma = 370 4 {0.0,0.7,0.0,0.3} 0.1949085 Sigma = 370 5 { 0.0,0.6,0.0,0.4} 0.1897394 Sigma = 370 6 {0.1,0.6,0.0,0.3} 0.1894361 Sigma = 370 7 {0.2,0.6,0.0,0.2} 0.1894361 Sigma = 370 8 {0.3,0.6,0.0,0.1} 0.1891583 Sigma = 370 9 {0.4,0.6,0.0,0.0} 0.1891583 Sigma = 370 10 {0.1,0.3,0.2,0.4} 0.1885622 Sigma = 370 11 {0.1,0.7,0.0,0.2} 0.2002544 Sigma = 380 12 {0.2,0.7,0.0,0.1} 0.2002544 Sigma = 380 13 {0.3,0.7,0.0, 0.1999767 Sigma = 380 14 {0.0,0.7,0.0,0.3} 0.1990751 Sigma = 380 15 {0.1,0.3,0.2,0.4} 0.1927288 Sigma = 380 16 {0.1, 0.5,0.1,0.3} 0.1925015 Sigma = 380 17 {0.2,0.5,0.1,0.2} 0.1925015 Sigma = 380 18 {0.3,0.3,0.2,0.2} 0.1924998 Sigma = 380 19 {0.4,0.3,0.2,0.1} 0.1924998 Sigma = 380 20 {0.5,0.3,0.2,0.0} 0.1924998 Sigma = 380 21 {0.1,0.6,0.0,0.3} 0.1960878 Sigma = 390 22 {0.2,0.6,0.0,0.2} 0.1960878 Sigma = 390 23 {0.4,0.6,0.0,0.0} 0.1958587 Sigma = 390 24 {0.3,0.6,0.0,0.1} 0.1958100 Sigma = 390 25 {0.0,0.6,0.0,0.4} 0.1952106 Sigma = 390 26 {0.1,0.7,0.0,0.2} 0.1904628 Sigma = 390 27 {0.2,0.7, 0.0,0.1} 0.1904628 Sigma = 390 28 {0.3,0.7,0.0,0.0} 0.1902337 Sigma = 390 29 {0.0,0.7,0.0,0.3} 0.1892835 Sigma = 390 30 { 0.1,0.4,0.1,0.4} 0.1883349 Sigma = 390 这是我的R代码: ggplot(data,aes(x = Map,y = reorder(Parameter,Map)))+ geom_segment(aes (Sigma =。,xend = 0,color =grey50)+ geom_point(size = 3)+ xlab()+ ylab()+ facet_grid scale =free_y,space =free_y)+ theme(axis.text.x = element_text(angle = -70,hjust = 0))+ scale_x_continuous(breaks = Br,expand = waiver()) 给我下面的图: 以正确的方式。任何帮助都非常值得赞赏。 $ b $不是很优雅的方式b library(ggplot2) library(grid) library(gridExtra) Br grphs< - lapply(df2,function(dfx){ ggplot(dfx,aes(x = Map,y = reorder(Parameter,Map)) )+ geom_segment(aes(yend = Parameter),xend = 0,color =grey50)+ geom_point(size = 3)+ xlab()+ ylab )+ facet_grid(Sigma〜。,scales =free_y,space =free_y)+ theme(axis.text.x = element_text(angle = -70,hjust = 0)) + scale_x_continuous(break = Br,expand = waiver(), limits = c(0.1880,0.2005))} ) grid。安排(grphs [[1]],grphs [[2]],grphs [[3]],ncol = 1) I have the a problem in ploting the data with ggplot. I couldn't make the data inside each facet to be ordered correctly. my sample data is:data <- structure(list(Parameter = c("{0.1, 0.7, 0.0, 0.2}", "{0.2, 0.7, 0.0, 0.1}", "{0.3, 0.7, 0.0, 0.0}", "{0.0, 0.7, 0.0, 0.3}", "{0.0, 0.6, 0.0, 0.4}", "{0.1, 0.6, 0.0, 0.3}", "{0.2, 0.6, 0.0, 0.2}", "{0.3, 0.6, 0.0, 0.1}", "{0.4, 0.6, 0.0, 0.0}", "{0.1, 0.3, 0.2, 0.4}", "{0.1, 0.7, 0.0, 0.2}", "{0.2, 0.7, 0.0, 0.1}", "{0.3, 0.7, 0.0, 0.0}", "{0.0, 0.7, 0.0, 0.3}", "{0.1, 0.3, 0.2, 0.4}", "{0.1, 0.5, 0.1, 0.3}", "{0.2, 0.5, 0.1, 0.2}", "{0.3, 0.3, 0.2, 0.2}", "{0.4, 0.3, 0.2, 0.1}", "{0.5, 0.3, 0.2, 0.0}", "{0.1, 0.6, 0.0, 0.3}", "{0.2, 0.6, 0.0, 0.2}", "{0.4, 0.6, 0.0, 0.0}", "{0.3, 0.6, 0.0, 0.1}", "{0.0, 0.6, 0.0, 0.4}", "{0.1, 0.7, 0.0, 0.2}", "{0.2, 0.7, 0.0, 0.1}", "{0.3, 0.7, 0.0, 0.0}", "{0.0, 0.7, 0.0, 0.3}", "{0.1, 0.4, 0.1, 0.4}"), Map = c(0.19608779, 0.19608779, 0.19581, 0.19490847, 0.18973944, 0.18943608, 0.18943608, 0.18915829, 0.18915829, 0.18856215, 0.20025444, 0.20025444, 0.19997665, 0.19907513, 0.19272882, 0.19250154, 0.19250154, 0.19249977, 0.19249977, 0.19249977, 0.19608779, 0.19608779, 0.19585875, 0.19581, 0.1952106, 0.19046278, 0.19046278, 0.19023374, 0.18928346, 0.18833488), Sigma = c("Sigma = 370", "Sigma = 370", "Sigma = 370", "Sigma = 370", "Sigma = 370", "Sigma = 370", "Sigma = 370", "Sigma = 370", "Sigma = 370", "Sigma = 370", "Sigma = 380", "Sigma = 380", "Sigma = 380", "Sigma = 380", "Sigma = 380", "Sigma = 380", "Sigma = 380", "Sigma = 380", "Sigma = 380", "Sigma = 380", "Sigma = 390", "Sigma = 390", "Sigma = 390", "Sigma = 390", "Sigma = 390", "Sigma = 390", "Sigma = 390", "Sigma = 390", "Sigma = 390", "Sigma = 390")), .Names = c("Parameter", "Map", "Sigma"), class = "data.frame", row.names = c(NA, -30L))data Parameter Map Sigma1 {0.1, 0.7, 0.0, 0.2} 0.1960878 Sigma = 3702 {0.2, 0.7, 0.0, 0.1} 0.1960878 Sigma = 3703 {0.3, 0.7, 0.0, 0.0} 0.1958100 Sigma = 3704 {0.0, 0.7, 0.0, 0.3} 0.1949085 Sigma = 3705 {0.0, 0.6, 0.0, 0.4} 0.1897394 Sigma = 3706 {0.1, 0.6, 0.0, 0.3} 0.1894361 Sigma = 3707 {0.2, 0.6, 0.0, 0.2} 0.1894361 Sigma = 3708 {0.3, 0.6, 0.0, 0.1} 0.1891583 Sigma = 3709 {0.4, 0.6, 0.0, 0.0} 0.1891583 Sigma = 37010 {0.1, 0.3, 0.2, 0.4} 0.1885622 Sigma = 37011 {0.1, 0.7, 0.0, 0.2} 0.2002544 Sigma = 38012 {0.2, 0.7, 0.0, 0.1} 0.2002544 Sigma = 38013 {0.3, 0.7, 0.0, 0.0} 0.1999767 Sigma = 38014 {0.0, 0.7, 0.0, 0.3} 0.1990751 Sigma = 38015 {0.1, 0.3, 0.2, 0.4} 0.1927288 Sigma = 38016 {0.1, 0.5, 0.1, 0.3} 0.1925015 Sigma = 38017 {0.2, 0.5, 0.1, 0.2} 0.1925015 Sigma = 38018 {0.3, 0.3, 0.2, 0.2} 0.1924998 Sigma = 38019 {0.4, 0.3, 0.2, 0.1} 0.1924998 Sigma = 38020 {0.5, 0.3, 0.2, 0.0} 0.1924998 Sigma = 38021 {0.1, 0.6, 0.0, 0.3} 0.1960878 Sigma = 39022 {0.2, 0.6, 0.0, 0.2} 0.1960878 Sigma = 39023 {0.4, 0.6, 0.0, 0.0} 0.1958587 Sigma = 39024 {0.3, 0.6, 0.0, 0.1} 0.1958100 Sigma = 39025 {0.0, 0.6, 0.0, 0.4} 0.1952106 Sigma = 39026 {0.1, 0.7, 0.0, 0.2} 0.1904628 Sigma = 39027 {0.2, 0.7, 0.0, 0.1} 0.1904628 Sigma = 39028 {0.3, 0.7, 0.0, 0.0} 0.1902337 Sigma = 39029 {0.0, 0.7, 0.0, 0.3} 0.1892835 Sigma = 39030 {0.1, 0.4, 0.1, 0.4} 0.1883349 Sigma = 390And this is my R code:ggplot (data, aes(x=Map, y=reorder(Parameter, Map))) + geom_segment(aes(yend=Parameter), xend=0, colour="grey50") + geom_point(size=3) + xlab("") + ylab("") + facet_grid(Sigma ~ ., scales="free_y", space="free_y")+ theme(axis.text.x=element_text(angle = -70, hjust = 0))+ scale_x_continuous(breaks =Br,expand = waiver())the gives me the following plot: as you can see inside each facet the data is not ordered in the right way. Any help is very much appreciated. 解决方案 A not so elegant way could belibrary(ggplot2)library(grid)library(gridExtra)Br <- seq(0.19, 0.20, by = 0.0025)df1 <- split(data, data$Sigma)df2 <- lapply(df1, function(x) x[order(x$Map), ])grphs <- lapply(df2, function(dfx){ ggplot (dfx, aes(x=Map, y=reorder(Parameter, Map))) + geom_segment(aes(yend = Parameter), xend =0, colour = "grey50") + geom_point(size = 3) + xlab("") + ylab("") + facet_grid(Sigma ~ ., scales="free_y", space="free_y") + theme(axis.text.x=element_text(angle = -70, hjust = 0)) + scale_x_continuous(breaks = Br, expand = waiver(), limits=c(0.1880, 0.2005))}) grid.arrange(grphs[[1]], grphs[[2]], grphs[[3]], ncol = 1) 这篇关于修正ggplot中的数据顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-11 00:02