ggplot2 中的手动刻面位置
Manual facet position in ggplot2
我正在使用 ggplot2 绘制以下数据集:
DF <- structure(list(site = c("A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "B", "B", "B", "B", "B", "B", "B", "B", "B",
"B", "B", "B", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C",
"C", "C", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D",
"D", "E", "E", "E", "E", "E", "E", "E", "E", "E", "E", "E", "E"
), month = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 11L, 12L), SWC = c(0.140086734851409, 0.137745990685859,
0.146660019201229, 0.275950971628449, 0.298260250896057, 0.26870029739777,
0.227566661823465, 0.197824137311287, 0.195409734063355, 0.229745648248465,
0.226546607074933, 0.158508782420749, 0.0809095246636771, 0.0804010923965351,
0.0845644708882278, 0.136702248824284, 0.121883242349049, 0.108167424836601,
0.0970784232538687, 0.0860934461299105, 0.0910916878172589, 0.10747642248062,
0.102700195758564, 0.0811833903700756, 0.115733715437788, 0.0631616319005478,
0.0631265153446416, 0.171535848109378, 0.18694684173028, 0.142807562821677,
0.145926108701425, 0.154393702185792, 0.171436382382201, 0.188897212829005,
0.186402403754978, 0.165098945598251, 0.0713685071127924, 0.0436531172429078,
0.0624862109235555, 0.127141665482761, 0.134542260869565, 0.124414092512545,
0.100807230998223, 0.0765214392215714, 0.0798724029741452, 0.103098854664915,
0.116568256944444, 0.1105108739241, 0.108650005144474, 0.0976296689160692,
0.105006219572287, 0.122777662914972, 0.102765292125318, 0.0851933017211099,
0.0566760862577016, 0.056282148272957, 0.0718264626865672, 0.0909327257326783,
0.10461694624978, 0.103895834299474), LE = c(0.946565193060996,
1.56650528637219, 4.45382423672104, 11.1985050677478, 29.1379975402081,
74.6488855786053, 95.5801950803702, 77.4708126488623, 39.6136552461462,
8.01576749720725, 2.6466216369622, 1.1745554117357, 2.13167679680568,
2.98141098231535, 5.69653566874706, 13.8293309632019, 26.1687157009092,
42.2656041113131, 53.2110193016699, 43.9856386693244, 28.6722592758158,
10.8442703054334, 3.66463523523524, 1.97253929316558, 3.12430388517694,
5.48701577036533, 6.95032997537497, 13.7237856698465, 32.2938743570904,
48.7715488919351, 55.6860655989469, 46.0893482005537, 31.0498645245332,
14.7640819905213, 8.98244641061733, 3.43372937817259, 3.69254598741488,
5.03158318364554, 7.73098511689167, 22.4154276282377, 47.1483358705029,
66.5623394397102, 77.0140731034483, 67.5194151592607, 44.6530820096047,
20.4357590631479, 6.56212672069986, 2.8940355016033, 1.69359970210389,
2.61391569682152, 6.3433679665485, 14.9249216033009, 41.1275149717784,
82.0861782662955, 100.382874115357, 81.3020338687151, 54.7966365463682,
21.3347745116999, 5.36748695652174, 1.70811548886738)), .Names = c("site",
"month", "SWC", "LE"), row.names = c(NA, -60L), class = "data.frame")
当我绘制它时,这是默认绘图:
library(ggplot2)
ggplot(data = DF, aes(x = SWC, y = LE)) +
geom_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
geom_point() + facet_wrap(~site)
不过,我想手动控制刻面的位置。例如,我希望空白 space 位于面 B
之后,因此面 C
、D
和 E
位于底行。
有什么办法吗?最好的方法是什么?
编辑:我正在投票重新打开这个问题,因为建议的答案似乎不再有效。例如,names(g$grobs)
returns NULL.
函数 multiplot 在这里非常有用。
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
library(grid)
# Make a list from the ... arguments and plotlist
plots <- c(list(...), plotlist)
numPlots = length(plots)
# If layout is NULL, then use 'cols' to determine layout
if (is.null(layout)) {
# Make the panel
# ncol: Number of columns of plots
# nrow: Number of rows needed, calculated from # of cols
layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
ncol = cols, nrow = ceiling(numPlots/cols))
}
if (numPlots==1) {
print(plots[[1]])
} else {
# Set up the page
grid.newpage()
pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
# Make each plot, in the correct location
for (i in 1:numPlots) {
# Get the i,j matrix positions of the regions that contain this subplot
matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
layout.pos.col = matchidx$col))
}
}
}
试试这个
A<-ggplot(data = subset(DF,site=="A"), aes(x = SWC, y = LE)) +
geom_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
geom_point()
B<-ggplot(data = subset(DF,site=="B"), aes(x = SWC, y = LE)) +
geom_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
geom_point()
C<-ggplot(data = subset(DF,site=="C"), aes(x = SWC, y = LE)) +
geom_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
geom_point()
D<-ggplot(data = subset(DF,site=="D"), aes(x = SWC, y = LE)) +
geom_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
geom_point()
E<-ggplot(data = subset(DF,site=="E"), aes(x = SWC, y = LE)) +
geom_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
geom_point()
Blank<-ggplot()
multiplot(Blank, A, B, C, D, E, cols=3)
试试这个(针对这个特定场景),
library(ggplot2)
ggplot(data = DF, aes(x = SWC, y = LE)) +
geom_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
geom_point() + facet_wrap(~factor(site,levels=c("C","D","E","A","B")), as.table=FALSE)
我最终使用 patchwork
包找到了更通用的问题答案。
这是一个可重现的例子:
library(dplyr)
library(ggplot2)
library(patchwork)
df <- data.frame(group=c(1,1,2,2,3),
name=c('a','b','c','d','e'),
x=c(1,2,3,4,5),
y=c(2,3,4,5,6))
p1 <-
df %>%
filter(name %in% c("a", "b", "c")) %>%
ggplot(aes(x, y)) +
geom_point() +
xlab(NULL) +
facet_wrap(~ name, nrow = 1)
p2 <-
df %>%
filter(name %in% c("d", "e")) %>%
ggplot(aes(x, y)) +
geom_point() +
facet_wrap(~ name, nrow = 1)
p1 + p2 &
plot_layout(design = "AAAAAAA
#######
#BBBBB#",
heights = c(3, 0.3, 3))
更多详细信息,请访问:https://github.com/thomasp85/patchwork/issues/205#issuecomment-867999897
我正在使用 ggplot2 绘制以下数据集:
DF <- structure(list(site = c("A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "B", "B", "B", "B", "B", "B", "B", "B", "B",
"B", "B", "B", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C",
"C", "C", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D",
"D", "E", "E", "E", "E", "E", "E", "E", "E", "E", "E", "E", "E"
), month = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 11L, 12L), SWC = c(0.140086734851409, 0.137745990685859,
0.146660019201229, 0.275950971628449, 0.298260250896057, 0.26870029739777,
0.227566661823465, 0.197824137311287, 0.195409734063355, 0.229745648248465,
0.226546607074933, 0.158508782420749, 0.0809095246636771, 0.0804010923965351,
0.0845644708882278, 0.136702248824284, 0.121883242349049, 0.108167424836601,
0.0970784232538687, 0.0860934461299105, 0.0910916878172589, 0.10747642248062,
0.102700195758564, 0.0811833903700756, 0.115733715437788, 0.0631616319005478,
0.0631265153446416, 0.171535848109378, 0.18694684173028, 0.142807562821677,
0.145926108701425, 0.154393702185792, 0.171436382382201, 0.188897212829005,
0.186402403754978, 0.165098945598251, 0.0713685071127924, 0.0436531172429078,
0.0624862109235555, 0.127141665482761, 0.134542260869565, 0.124414092512545,
0.100807230998223, 0.0765214392215714, 0.0798724029741452, 0.103098854664915,
0.116568256944444, 0.1105108739241, 0.108650005144474, 0.0976296689160692,
0.105006219572287, 0.122777662914972, 0.102765292125318, 0.0851933017211099,
0.0566760862577016, 0.056282148272957, 0.0718264626865672, 0.0909327257326783,
0.10461694624978, 0.103895834299474), LE = c(0.946565193060996,
1.56650528637219, 4.45382423672104, 11.1985050677478, 29.1379975402081,
74.6488855786053, 95.5801950803702, 77.4708126488623, 39.6136552461462,
8.01576749720725, 2.6466216369622, 1.1745554117357, 2.13167679680568,
2.98141098231535, 5.69653566874706, 13.8293309632019, 26.1687157009092,
42.2656041113131, 53.2110193016699, 43.9856386693244, 28.6722592758158,
10.8442703054334, 3.66463523523524, 1.97253929316558, 3.12430388517694,
5.48701577036533, 6.95032997537497, 13.7237856698465, 32.2938743570904,
48.7715488919351, 55.6860655989469, 46.0893482005537, 31.0498645245332,
14.7640819905213, 8.98244641061733, 3.43372937817259, 3.69254598741488,
5.03158318364554, 7.73098511689167, 22.4154276282377, 47.1483358705029,
66.5623394397102, 77.0140731034483, 67.5194151592607, 44.6530820096047,
20.4357590631479, 6.56212672069986, 2.8940355016033, 1.69359970210389,
2.61391569682152, 6.3433679665485, 14.9249216033009, 41.1275149717784,
82.0861782662955, 100.382874115357, 81.3020338687151, 54.7966365463682,
21.3347745116999, 5.36748695652174, 1.70811548886738)), .Names = c("site",
"month", "SWC", "LE"), row.names = c(NA, -60L), class = "data.frame")
当我绘制它时,这是默认绘图:
library(ggplot2)
ggplot(data = DF, aes(x = SWC, y = LE)) +
geom_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
geom_point() + facet_wrap(~site)
不过,我想手动控制刻面的位置。例如,我希望空白 space 位于面 B
之后,因此面 C
、D
和 E
位于底行。
有什么办法吗?最好的方法是什么?
编辑:我正在投票重新打开这个问题,因为建议的答案似乎不再有效。例如,names(g$grobs)
returns NULL.
函数 multiplot 在这里非常有用。
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
library(grid)
# Make a list from the ... arguments and plotlist
plots <- c(list(...), plotlist)
numPlots = length(plots)
# If layout is NULL, then use 'cols' to determine layout
if (is.null(layout)) {
# Make the panel
# ncol: Number of columns of plots
# nrow: Number of rows needed, calculated from # of cols
layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
ncol = cols, nrow = ceiling(numPlots/cols))
}
if (numPlots==1) {
print(plots[[1]])
} else {
# Set up the page
grid.newpage()
pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
# Make each plot, in the correct location
for (i in 1:numPlots) {
# Get the i,j matrix positions of the regions that contain this subplot
matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
layout.pos.col = matchidx$col))
}
}
}
试试这个
A<-ggplot(data = subset(DF,site=="A"), aes(x = SWC, y = LE)) +
geom_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
geom_point()
B<-ggplot(data = subset(DF,site=="B"), aes(x = SWC, y = LE)) +
geom_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
geom_point()
C<-ggplot(data = subset(DF,site=="C"), aes(x = SWC, y = LE)) +
geom_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
geom_point()
D<-ggplot(data = subset(DF,site=="D"), aes(x = SWC, y = LE)) +
geom_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
geom_point()
E<-ggplot(data = subset(DF,site=="E"), aes(x = SWC, y = LE)) +
geom_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
geom_point()
Blank<-ggplot()
multiplot(Blank, A, B, C, D, E, cols=3)
试试这个(针对这个特定场景),
library(ggplot2)
ggplot(data = DF, aes(x = SWC, y = LE)) +
geom_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
geom_point() + facet_wrap(~factor(site,levels=c("C","D","E","A","B")), as.table=FALSE)
我最终使用 patchwork
包找到了更通用的问题答案。
这是一个可重现的例子:
library(dplyr)
library(ggplot2)
library(patchwork)
df <- data.frame(group=c(1,1,2,2,3),
name=c('a','b','c','d','e'),
x=c(1,2,3,4,5),
y=c(2,3,4,5,6))
p1 <-
df %>%
filter(name %in% c("a", "b", "c")) %>%
ggplot(aes(x, y)) +
geom_point() +
xlab(NULL) +
facet_wrap(~ name, nrow = 1)
p2 <-
df %>%
filter(name %in% c("d", "e")) %>%
ggplot(aes(x, y)) +
geom_point() +
facet_wrap(~ name, nrow = 1)
p1 + p2 &
plot_layout(design = "AAAAAAA
#######
#BBBBB#",
heights = c(3, 0.3, 3))
更多详细信息,请访问:https://github.com/thomasp85/patchwork/issues/205#issuecomment-867999897