[R] Multiple ggplot in a single plot

Joshua Wiley jwiley.psych at gmail.com
Sat Jul 16 03:13:41 CEST 2011


Hi,

Here is one option, though this may be a bit tricky for you if you are
new.  ggplot2 is based on grid graphics, so using grid you can obtain
more customization.  There may be easier ways and even within grid it
may be possible to do more simply than I am demonstrating, I am still
finding my footing with that package.  Example code follows that
should reproduce the attached PDF.

Cheers,

Josh

###########################################

require(ggplot2)

## some plots using the built in "mtcars" dataset
p1 <- ggplot(melt(as.data.frame(lapply(mtcars, scale))),
  aes(x = variable, y = value)) +
  geom_boxplot() +
  geom_jitter()

p2 <- ggplot(melt(abs(cor(mtcars))), aes(x = X1, y = X2, fill = value)) +
  geom_tile()

p3 <- ggplot(mtcars, aes(x = wt, y = disp, colour = factor(am))) +
  geom_point() +
  geom_smooth(method = "lm", aes(group = 1), se = FALSE)

p4 <- ggplot(mtcars, aes(x = hp, y = mpg, size = factor(cyl))) +
  geom_point()

## Start a new device with a specified size
dev.new(width = 11, height = 8.5)

## use the grid package to customize the layout
pushViewport(vpList(
  viewport(x = 0, y = .45, width = .5, height = .45,
    just = c("left", "bottom"), name = "p1"),
  viewport(x = .5, y = .45, width = .5, height = .45,
    just = c("left", "bottom"), name = "p2"),
  viewport(x = 0, y = 0, width = .5, height = .4,
    just = c("left", "bottom"), name = "p3"),
  viewport(x = .5, y = 0, width = .5, height = .45,
    just = c("left", "bottom"), name = "p4"),
  viewport(x = 0, y = .9, width = 1, height = .1,
    just = c("left", "bottom"), name = "title")))

## Add the plots from ggplot2
upViewport()
downViewport("p1")
print(p1, newpage = FALSE)

upViewport()
downViewport("p2")
print(p2, newpage = FALSE)

upViewport()
downViewport("p3")
print(p3, newpage = FALSE)

upViewport()
downViewport("p4")
print(p4, newpage = FALSE)

## add an overall title (note I left space for it and gave it its own viewport)
upViewport()
downViewport("title")
grid.text("Four Plots created by the Excellent ggplot2 package",
  x = .5,  gp = gpar(fontsize = 18))

########################################

On Fri, Jul 15, 2011 at 4:46 PM, hrishi <hrishi27n at gmail.com> wrote:
> Hello friends i have to created several ggplots.
> I have to combine them together to a new plot.
> any ideas ?? I am new to R.
> I am attaching a sample plot
> ----------------------------------
> http://r.789695.n4.nabble.com/file/n3671184/1A2.jpeg
> Thanks in Advance.
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Multiple-ggplot-in-a-single-plot-tp3671184p3671184.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
https://joshuawiley.com/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: multiggplot2.pdf
Type: application/pdf
Size: 12876 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110715/9a15d3d2/attachment.pdf>


More information about the R-help mailing list