How to put separate ggplot2 graphics, but on the same screen?

5

Suppose I have these 4 graphics:

p1 <- ggplot(mtcars, aes(mpg, cyl)) + geom_point()
p2 <- ggplot(mtcars, aes(mpg, cyl)) + geom_line()
p3 <- ggplot(mtcars, aes(mpg, cyl)) + geom_line(color="blue")
p4 <- ggplot(mtcars, aes(mpg, cyl)) + geom_line(color="red")

How to plot each of these separate charts, but all together on the same screen?

    
asked by anonymous 26.09.2014 / 02:29

3 answers

6

You can also use the grid.arrange functions of the gridExtra package.

    library(ggplot2)
    library(gridExtra)

    # Crio 4 gráficos
    grafico_1 <- qplot(1:5,1:5)
    grafico_2 <- qplot(10:1,1:10)
    grafico_3 <- qplot(1)
    grafico_4 <- qplot(12)

    # Plotando dois deles (em colunas)
    grid.arrange(grafico_1 , grafico_2 , ncol=2)

    # Plotando outros dois (em linhas)
    grid.arrange(grafico_3 , grafico_4 , nrow=2)


    # Plotando todos
    grid.arrange(grafico_1 , grafico_2 ,
                 grafico_3 , grafico_4 ,
                 ncol=2, nrow=2)

Help?

    
26.09.2014 / 03:43
3

For this, I've already used the multiplot function provided on this site .

It may well be that the function is not exactly what it needs, but it is easier to change it than to start from scratch.

Copying the site function below to eternalize:

# Multiple plot function
#
# ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects)
# - cols:   Number of columns in layout
# - layout: A matrix specifying the layout. If present, 'cols' is ignored.
#
# If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE),
# then plot 1 will go in the upper left, 2 will go in the upper right, and
# 3 will go all the way across the bottom.
#
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
  require(grid)

  # Make a list from the ... arguments and plotlist
  plots <- c(list(...), plotlist)

  numPlots = length(plots)

  # If layout is NULL, then use 'cols' to determine layout
  if (is.null(layout)) {
    # Make the panel
    # ncol: Number of columns of plots
    # nrow: Number of rows needed, calculated from # of cols
    layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
                    ncol = cols, nrow = ceiling(numPlots/cols))
  }

 if (numPlots==1) {
    print(plots[[1]])

  } else {
    # Set up the page
    grid.newpage()
    pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))

    # Make each plot, in the correct location
    for (i in 1:numPlots) {
      # Get the i,j matrix positions of the regions that contain this subplot
      matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))

      print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
                                      layout.pos.col = matchidx$col))
    }
  }
}
    
26.09.2014 / 02:39
3

Well, if you are going to put all the SEPARATE graphics on the same screen, this is possible with the gridExtra package. However, depending on the situation, there is the feature of the facets or also put all the curves on the same chart. Let's go to each case:

1) Separate graphics on the same screen

library(gridExtra)

p1 <- ggplot(mtcars, aes(mpg, cyl)) + geom_point()
p2 <- ggplot(mtcars, aes(mpg, cyl)) + geom_line()
p3 <- ggplot(mtcars, aes(mpg, cyl)) + geom_line(color="blue")
p4 <- ggplot(mtcars, aes(mpg, cyl)) + geom_line(color="red")

grid.arrange(p1,p2, p3, p4)

2)Toshowthecaseusingfacets,Iwillusethetipsdatasetfromthereshape2package

library(tips)library(ggplot2)data(tips)head(tips)total_billtipsexsmokerdaytimesize116.991.01FemaleNoSunDinner2210.341.66MaleNoSunDinner3321.013.50MaleNoSunDinner3423.683.31MaleNoSunDinner2524.593.61FemaleNoSunDinner4625.294.71MaleNoSunDinner4

Seethatwehavethevalueofthebill,thetip,thesex,howthedaywas,andsoon.SupposeIwanttoseetherelationshipbetweenthebillandthefractionofthetipinrelationtotheaccount:

g1<-ggplot(tips,aes(x=total_bill,y=tip/total_bill))+geom_point(shape=1)

Let's use the facets and present the same chart, only by separating by gender, that is, we will see who gives the most tip in relation to the value of the account, men or women.

g1 + facet_grid(. ~ sex)

3)Finally,let'sputallthecurvesinthesamegraph.Stillusingthesamedatasettipsandusingasmoothingfunction.

ggplot(tips,aes(x=total_bill,y=tip/total_bill,fill=sex,col=sex))+geom_point(shape=1)+geom_smooth()

The strategy to be used, 1), 2) or 3) depends on each particular case.

    
26.09.2014 / 04:17