Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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")
- 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)
- # 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))
- }
- }
- }
- 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)
- library(tips)
- library(ggplot2)
- data(tips)
- head(tips)
- total_bill tip sex smoker day time size
- 1 16.99 1.01 Female No Sun Dinner 2
- 2 10.34 1.66 Male No Sun Dinner 3
- 3 21.01 3.50 Male No Sun Dinner 3
- 4 23.68 3.31 Male No Sun Dinner 2
- 5 24.59 3.61 Female No Sun Dinner 4
- 6 25.29 4.71 Male No Sun Dinner 4
- g1 <- ggplot(tips, aes(x=total_bill, y=tip/total_bill)) + geom_point(shape=1)
- g1 + facet_grid(. ~ sex)
- ggplot(tips, aes(x=total_bill, y=tip/total_bill, fill=sex, col=sex)) + geom_point(shape=1) + geom_smooth()
- library(ggplot2)
- 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")
- library(cowplot)
- cowplot::plot_grid(p1, p2, p3, p4,
- nrow = 2, align = "hv")
- library(ggpubr)
- ggpubr::ggarrange(p1, p2, p3, p4,
- nrow = 2, ncol = 2)
- library(patchwork)
- p1 + p2 + p3 + p4
- library(egg)
- egg::ggarrange(p1, p2, p3, p4, ncol = 2)
- library(gridExtra)
- # Matriz para divisao do plot
- lay <- rbind(c(1,2),
- c(3,4))
- gridExtra::grid.arrange(p1, p2, p3, p4, layout_matrix = lay)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement