Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (7.3k points)

I would like to place two plots side by side using the ggplot2 package, i.e. do the equivalent of 

par(mfrow=c(1,2)).

For example, I would like to have the following two plots show side-by-side on the same scale.

x <- rnorm(100)

eps <- rnorm(100,0,.2)

qplot(x,3*x+eps)

qplot(x,2*x+eps)

Do I need to put them in the same data.frame?

qplot(displ, hwy, data=mpg, facets = . ~ year) + geom_smooth()

1 Answer

0 votes
by

You can use the plot_grid() function included in the cowplot package.

For example:

install.packages("cowplot")

library(cowplot) 

mtcars1 <- ggplot(mtcars, aes(mpg, color = cyl)) + geom_freqpoly(binwidth = 1)+

  theme_bw()+

  labs(title = "Miles per Gallon by Cylinders",x = "Miles per    Gallon",y = "Count",fill = "Cylinders")

mtcars2 <- ggplot(mtcars, aes(x = wt, y = mpg, col = cyl)) +

  geom_point(size = 4, shape = 1, alpha = 0.6) +

  labs(x = "Weight",y = "Miles per Gallon", color =         "Cylinders")+

  theme(legend.position = c(0.65, 0.85))

plot_grid(mtcars1, mtcars2, labels = "AUTO")

Output:

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...