Gggplot grid.将所有内容排列成一行,看起来就像一个无缝绘图 [英] Gggplot grid.arrange all in one row to look like a seamless plot
问题描述
我正在尝试从
我建议使用 Patchwork
打包方法.您可以通过实用的方式定义所需的行数和列数.接下来,我们的数据为 dat
的代码:
我们为数据创建一个列表:
库(ggplot2)图书馆(dplyr)图书馆(拼凑而成)#创建列表列表<-split(dat,dat $ Order)
我们为绘图构建了一个函数:
#现在绘制函数myplotfun<-函数(x){G<-ggplot(x,aes(x = Family,y = B))+geom_point()+facet_grid(.〜家庭,比例=免费",空间=免费")+ylim(范围(x $ B))+xlab(")+ ggtitle(唯一(x $ Order))回报(G)}
最后,我们使用 patchwork
函数应用该函数并进行绘制:
#申请地块列表2<-lapply(列表,myplotfun)#包装最终情节wrap_plots(List2,nrow = 1)
输出:
您可以根据需要添加其他详细信息.
I am attempting to implement a solution from here. Unfortunately I cant get nested facets package uploaded and attempting to format a the facet titles by using the solution to use gridextra package.
However, what I cant seem to figure out how to do is put all of the graphs on one row, as in it would look like one single bar plot. Need to modify this line somehow: grid.arrange(., ncol = 1)
. In my data all plots will be of equal size with two data points each.
Code borrowed is:
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)
I would suggest a patchwork
package approach. You can define the number of rows and columns you want in a practical way. Next the code with our data being dat
:
We create a list for our data:
library(ggplot2)
library(dplyr)
library(patchwork)
#Create list
List <- split(dat,dat$Order)
We build a function for plots:
#Now function to plot
myplotfun <- function(x)
{
G <- ggplot(x,aes(x=Family,y=B))+
geom_point() +
facet_grid(. ~ Family,
scales = "free", space = "free") +
ylim(range(x$B)) +
xlab("")+ggtitle(unique(x$Order))
return(G)
}
Finally we apply the function and plot using patchwork
functions:
#Apply for plots
List2 <- lapply(List,myplotfun)
#Wrap final plot
wrap_plots(List2,nrow = 1)
Output:
You can add other details according to what you want.
这篇关于Gggplot grid.将所有内容排列成一行,看起来就像一个无缝绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!