我在用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.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
这是我的R代码:
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())
给我以下情节:
正如您可以在每个方面内看到的那样,数据没有以正确的方式排序。非常感谢任何帮助。
答案 0 :(得分:1)
不太优雅的方式可能是
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)