R: Arranging Multiple Plots Together Using Gridextra

R: arranging multiple plots together using gridExtra

If you want to keep the approach you are using just add

par(mfrow=c(2,2))

before all four plots.

If you want everything on the same line add instead

par(mfrow=c(1,4))

Efficient way to plot multiple ggplots from list using gridextra?

grid.arrange works with lists of plots. Just specify it with grobs = .

In your case:

plot_collection <- grid.arrange(grobs = DBScan_plots, nrow = 5, ncol = 5)

multiple different sized plots in same window in R?

EDIT -- fixed to shrink third chart per OP.

library(patchwork)
design <- c( # we specify the top, left, bottom, and right
area(1, 1, 4, 4), # coordinates for each of the three plots
area(1, 5, 2, 6),
area(3, 5)
)
allPlots[[1]] + allPlots[[2]] + allPlots[[3]] +
plot_layout(design = design)

Sample Image

Arrange many plots using gridExtra

I had to change the qplot loop call slightly (i.e. put the factors in the data frame) as it was throwing a mismatched size error. I'm not including that bit since that part is obviously working in your environment or it was an errant paste.

Try adjusting your widths units like this:

widths=unit(c(1000,50),"pt")

And you'll get something a bit closer to what you were probably expecting:

And, I can paste code now a few months later :-)

library(ggplot2)
library(gridExtra)

df <- data.frame(price=matrix(sample(1:1000, 100, replace = TRUE), ncol = 1))

df$size1 = 1:nrow(df)
df$size1 = cut(df$size1, breaks=11)
df=df[sample(nrow(df)),]
df$size2 = 1:nrow(df)
df$size2 = cut(df$size2, breaks=11)
df=df[sample(nrow(df)),]
df$clarity = 1:nrow(df)
df$clarity = cut(df$clarity, breaks=6)

# Create one graph for each size1, plotting the median price vs. the size2 by clarity:
for (c in 1:length(table(df$size1))) {

mydf = df[df$size1==names(table(df$size1))[c],]
mydf = aggregate(mydf$price, by=list(mydf$size2, mydf$clarity),median);
names(mydf)[1] = 'size2'
names(mydf)[2] = 'clarity'
names(mydf)[3] = 'median_price'
mydf$clarity <- factor(mydf$clarity)

assign(paste("p", c, sep=""),
qplot(data=mydf,
x=as.numeric(size2),
y=median_price,
group=clarity,
geom="line", colour=clarity,
xlab = "number of samples",
ylab = "median price",
main = paste("region number is ",c, sep=''),
plot.title=element_text(size=10)) +
scale_colour_discrete(name = "clarity") +
theme_bw() + theme(axis.title.x=element_text(size = rel(0.8)),
axis.title.y=element_text(size = rel(0.8)),
axis.text.x=element_text(size=8),
axis.text.y=element_text(size=8) ))
}

# Use gridExtra to arrange the 11 plots:

g_legend<-function(a.gplot){
tmp <- ggplot_gtable(ggplot_build(a.gplot))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
return(legend)}

mylegend<-g_legend(p1)

grid.arrange(arrangeGrob(p1 + theme(legend.position="none"),
p2 + theme(legend.position="none"),
p3 + theme(legend.position="none"),
p4 + theme(legend.position="none"),
p5 + theme(legend.position="none"),
p6 + theme(legend.position="none"),
p7 + theme(legend.position="none"),
p8 + theme(legend.position="none"),
p9 + theme(legend.position="none"),
p10 + theme(legend.position="none"),
p11 + theme(legend.position="none"),
top ="Main title",
left = ""), mylegend,
widths=unit(c(1000,50),"pt"), nrow=1)

preview here

Edit (16/07/2015): with gridExtra >= 2.0.0, the main parameter has been renamed top.

add title using grid.arrange for multiple plots made with gridExtra::grid.arrange

I figured out how to do it, since the use of main in plot was fixed for the eulerr package. Now I can use:

gridExtra::grid.arrange(plot(euler1, main = 'title1'),
plot(euler2, main = 'title2'))

Thanks everyone for the feedback.

Graphs side by side using `gridExtra` and multiple facets

Does this do what you are looking for grid.arrange(gt, gt2, ncol = 2)?

(g1and g2 in your code are both NULL because you they are created by calling grid.draw, which doesn´t return anything)

To use facet_wrap you'll need to get all your data into one dataframe with long format:

library(tidyr)
df <- cbind.data.frame(dt, dt1)
df <- gather(df, key = "db", value = "value")

Then plot:

p <- ggplot(df, aes(x = value)) + 
geom_histogram(aes(y = ..density..),
binwidth = .5,
breaks = seq(-2, 2, by = .1)) +
facet_wrap(~ db)

Grid of multiple ggplot2 plots which have been made in a for loop

I would be inclined to agree with Richie, but if you want to arrange them yourself:

library(gridExtra)
library(ggplot2)
p <- list()
for(i in 1:4){
p[[i]] <- qplot(1:10,10:1,main=i)
}
do.call(grid.arrange,p)

take a look at the examples at the end of ?arrangeGrob for ways to eliminate the for loop altogether:

plots = lapply(1:5, function(.x) qplot(1:10,rnorm(10),main=paste("plot",.x)))
require(gridExtra)
do.call(grid.arrange, plots)

How do I arrange a variable list of plots using grid.arrange?

How about this:

library(gridExtra)
n <- length(plist)
nCol <- floor(sqrt(n))
do.call("grid.arrange", c(plist, ncol=nCol))

Sample Image

How to use a greek symbol in grid.arrange()

I am using the iris dataset as an example. You can use Unicode Characters for in this case Delta \u0394. You can use the following code:

library(ggplot2)
plot1 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +geom_point()
plot2 <- ggplot(iris, aes(x = Petal.Length, y = Petal.Width)) +geom_point()

library(gridExtra)
library(grid)
grid.arrange(
plot1,
plot2,
#legend,
#ncol = 3,
top = "Change in Score",
left = "\u0394 Score"
)

Output:

Sample Image



Related Topics



Leave a reply



Submit