简体   繁体   中英

Fixing the data order in facets in ggplot

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       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.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

And 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 be

library(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)

用grid.arrange绘制

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM