Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combined plot of ggplot2 (Not in a single Plot), using par() or layout() function? [duplicate]

Tags:

I've been thinking of using par() or layout() functions for combining ggplots. Will it be possible to use those functions?

Say I want to plot ggplot for scatterplot and ggplot for histogram. And I want to combine the two plots (NOT IN A SINGLE PLOT). Is it applicable?

I tried it with simple plotting in R, without using the ggplot functions. And it works actually.

Here's a sample from Quick-R, Link: http://www.statmethods.net/advgraphs/layout.html

# 4 figures arranged in 2 rows and 2 columns attach(mtcars) par(mfrow=c(2,2)) plot(wt,mpg, main="Scatterplot of wt vs. mpg") plot(wt,disp, main="Scatterplot of wt vs disp") hist(wt, main="Histogram of wt") boxplot(wt, main="Boxplot of wt")  # One figure in row 1 and two figures in row 2 attach(mtcars) layout(matrix(c(1,1,2,3), 2, 2, byrow = TRUE)) hist(wt) hist(mpg) hist(disp) 

But when I try to use ggplot, and combine the plot, I don't get an output.

like image 536
Al-Ahmadgaid Asaad Avatar asked Feb 28 '12 22:02

Al-Ahmadgaid Asaad


People also ask

Can you use par with Ggplot?

To arrange multiple ggplot2 graphs on the same page, the standard R functions - par() and layout() - cannot be used. The basic solution is to use the gridExtra R package, which comes with the following functions: grid.

How do I put two Ggplots on top of each other?

Combine multiple ggplot on one page.Use the function ggarrange() [ggpubr package], a wrapper around the function plot_grid() [cowplot package]. Compared to plot_grid(), ggarange() can arrange multiple ggplots over multiple pages.

What does Ggplot () do?

ggplot() initializes a ggplot object. It can be used to declare the input data frame for a graphic and to specify the set of plot aesthetics intended to be common throughout all subsequent layers unless specifically overridden.


1 Answers

library(ggplot2) library(grid)   vplayout <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y)   plot1 <- qplot(mtcars,x=wt,y=mpg,geom="point",main="Scatterplot of wt vs. mpg") plot2 <- qplot(mtcars,x=wt,y=disp,geom="point",main="Scatterplot of wt vs disp") plot3 <- qplot(wt,data=mtcars) plot4 <- qplot(wt,mpg,data=mtcars,geom="boxplot") plot5 <- qplot(wt,data=mtcars) plot6 <- qplot(mpg,data=mtcars) plot7 <- qplot(disp,data=mtcars)  # 4 figures arranged in 2 rows and 2 columns grid.newpage() pushViewport(viewport(layout = grid.layout(2, 2))) print(plot1, vp = vplayout(1, 1)) print(plot2, vp = vplayout(1, 2)) print(plot3, vp = vplayout(2, 1)) print(plot4, vp = vplayout(2, 2))   # One figure in row 1 and two figures in row 2 grid.newpage() pushViewport(viewport(layout = grid.layout(2, 2))) print(plot5, vp = vplayout(1, 1:2)) print(plot6, vp = vplayout(2, 1)) print(plot7, vp = vplayout(2, 2)) 
like image 104
Maiasaura Avatar answered Sep 19 '22 01:09

Maiasaura