ggplot2嵌套的小平面图 [英] Nested facet plot with ggplot2
问题描述
如果我有一个嵌套因子,在这种情况下,我有多个包含在因子Order中的Family级别,我想创建一个
If I have a nested factor, in this case I have multiple "Family" levels that are contained in the factor "Order", I would like to potentially create a
facet_grid(Family / Order ~.)
当前
instead of the current
facet_grid(Family + Order ~.)
基本上 - 每条订单都有一条 - 它包含在该订单内的每个家族的所有条带的旁边。我知道facet_grid(Family / Order〜。)目前是不可能的,但我将如何实现这一效果?可以用主题()完成吗?非常感谢。 --SB
Basically -- ONE strip for every Order -- that contains next to it all strips for each family inside that Order. I know that facet_grid(Family / Order ~.) is currently not possible, but how would I achieve this effect? Could it be done with a theme()? Thank you so much. --SB
我应该在上面指出家庭和订单都是因素。数据值B由具有它们所属的族级和订单级别的物种组成。这是我的情节的代码:
I should have specified above that both Family and Order are factors. The data values B are by Species which have a Family level and Order level they belong to. Here is the code for my plot:
p <- ggplot(models, aes(B,Species)) + geom_point() + facet_grid(Family + Order ~
.,scales="free",space="free")
以下是一些示例数据:
Here is some sample data:
structure(list(Species = c("Acanthocyclops robustus", "Acroperus harpae",
"Alona affinis", "Ascaphus truei", "Bosmina longirostris"), Intercept = c(-36.1182388331068,
-27.2140776216155, -25.7920464721491, -39.2233884219763, -31.4301301084581
), B = c(0.919397836908493, 0.716601987210452, 0.685455190113372,
1.04159758611351, 0.81077051300147), Bconf = c(0.407917065756464,
0.181611850119198, 0.254101713856315, 0.708582768458448, 0.234313394549538
), Order = c("Cyclopoida", "Diplostraca", "Diplostraca", "Anura",
"Diplostraca"), Family = c("Cyclopidae", "Chydoridae", "Chydoridae",
"Leiopelmatidae", "Bosminidae")), .Names = c("Species", "Intercept",
"B", "Bconf", "Order", "Family"), row.names = c(NA, 5L), class = "data.frame")
推荐答案
使用 facet_grid
或 facet_wrap
不会构建您尝试的图形建立。但是,您可以创建一个图形列表,然后通过 gridEtrax :: grid.arrange
来绘制它们。这里是一个例子
Using facet_grid
or facet_wrap
will not build the graphic you are trying to build. You can, however, build a list of graphics and then plot them via gridEtrax::grid.arrange
. Here is an example
library(ggplot2)
library(gridExtra)
library(dplyr)
dat <-
structure(list(Species = c("Acanthocyclops robustus", "Acroperus harpae",
"Alona affinis", "Ascaphus truei", "Bosmina longirostris"), Intercept = c(-36.1182388331068,
-27.2140776216155, -25.7920464721491, -39.2233884219763, -31.4301301084581
), B = c(0.919397836908493, 0.716601987210452, 0.685455190113372,
1.04159758611351, 0.81077051300147), Bconf = c(0.407917065756464,
0.181611850119198, 0.254101713856315, 0.708582768458448, 0.234313394549538
), Order = c("Cyclopoida", "Diplostraca", "Diplostraca", "Anura",
"Diplostraca"), Family = c("Cyclopidae", "Chydoridae", "Chydoridae",
"Leiopelmatidae", "Bosminidae")), .Names = c("Species", "Intercept",
"B", "Bconf", "Order", "Family"), row.names = c(NA, 5L), class = "data.frame")
dat
# A ggplot object with NO data. Omit the order from the facet_grid call
g <-
ggplot() +
aes(Species, B) +
geom_point() +
facet_grid(. ~ Family,
scales = "free", space = "free") +
ylim(range(dat$B)) +
xlab("")
# Build a seperate graphic for each Order and title
plots <-
lapply(unique(dat$Order), function(o) {
g %+% dplyr::filter_(dat, ~ Order == o) + ggtitle(o)
})
# build as Grobs and plot via gridExtra::grid.arrange
plots %>%
lapply(ggplotGrob) %>%
arrangeGrob(grobs = .) %>%
grid.arrange(., ncol = 1)
这篇关于ggplot2嵌套的小平面图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!