Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. p1 <- ggplot(mtcars, aes(mpg, cyl)) + geom_point()
  2. p2 <- ggplot(mtcars, aes(mpg, cyl)) + geom_line()
  3. p3 <- ggplot(mtcars, aes(mpg, cyl)) + geom_line(color="blue")
  4. p4 <- ggplot(mtcars, aes(mpg, cyl)) + geom_line(color="red")
  5.  
  6. library(ggplot2)
  7. library(gridExtra)
  8.  
  9. # Crio 4 gráficos
  10. grafico_1 <- qplot(1:5,1:5)
  11. grafico_2 <- qplot(10:1,1:10)
  12. grafico_3 <- qplot(1)
  13. grafico_4 <- qplot(12)
  14.  
  15. # Plotando dois deles (em colunas)
  16. grid.arrange(grafico_1 , grafico_2 , ncol=2)
  17.  
  18. # Plotando outros dois (em linhas)
  19. grid.arrange(grafico_3 , grafico_4 , nrow=2)
  20.  
  21.  
  22. # Plotando todos
  23. grid.arrange(grafico_1 , grafico_2 ,
  24. grafico_3 , grafico_4 ,
  25. ncol=2, nrow=2)
  26.  
  27. # Multiple plot function
  28. #
  29. # ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects)
  30. # - cols: Number of columns in layout
  31. # - layout: A matrix specifying the layout. If present, 'cols' is ignored.
  32. #
  33. # If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE),
  34. # then plot 1 will go in the upper left, 2 will go in the upper right, and
  35. # 3 will go all the way across the bottom.
  36. #
  37. multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
  38. require(grid)
  39.  
  40. # Make a list from the ... arguments and plotlist
  41. plots <- c(list(...), plotlist)
  42.  
  43. numPlots = length(plots)
  44.  
  45. # If layout is NULL, then use 'cols' to determine layout
  46. if (is.null(layout)) {
  47. # Make the panel
  48. # ncol: Number of columns of plots
  49. # nrow: Number of rows needed, calculated from # of cols
  50. layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
  51. ncol = cols, nrow = ceiling(numPlots/cols))
  52. }
  53.  
  54. if (numPlots==1) {
  55. print(plots[[1]])
  56.  
  57. } else {
  58. # Set up the page
  59. grid.newpage()
  60. pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
  61.  
  62. # Make each plot, in the correct location
  63. for (i in 1:numPlots) {
  64. # Get the i,j matrix positions of the regions that contain this subplot
  65. matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
  66.  
  67. print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
  68. layout.pos.col = matchidx$col))
  69. }
  70. }
  71. }
  72.  
  73. library(gridExtra)
  74.  
  75. p1 <- ggplot(mtcars, aes(mpg, cyl)) + geom_point()
  76. p2 <- ggplot(mtcars, aes(mpg, cyl)) + geom_line()
  77. p3 <- ggplot(mtcars, aes(mpg, cyl)) + geom_line(color="blue")
  78. p4 <- ggplot(mtcars, aes(mpg, cyl)) + geom_line(color="red")
  79.  
  80. grid.arrange(p1,p2, p3, p4)
  81.  
  82. library(tips)
  83. library(ggplot2)
  84.  
  85. data(tips)
  86. head(tips)
  87.  
  88. total_bill tip sex smoker day time size
  89. 1 16.99 1.01 Female No Sun Dinner 2
  90. 2 10.34 1.66 Male No Sun Dinner 3
  91. 3 21.01 3.50 Male No Sun Dinner 3
  92. 4 23.68 3.31 Male No Sun Dinner 2
  93. 5 24.59 3.61 Female No Sun Dinner 4
  94. 6 25.29 4.71 Male No Sun Dinner 4
  95.  
  96. g1 <- ggplot(tips, aes(x=total_bill, y=tip/total_bill)) + geom_point(shape=1)
  97.  
  98. g1 + facet_grid(. ~ sex)
  99.  
  100. ggplot(tips, aes(x=total_bill, y=tip/total_bill, fill=sex, col=sex)) + geom_point(shape=1) + geom_smooth()
  101.  
  102. library(ggplot2)
  103.  
  104. p1 <- ggplot(mtcars, aes(mpg, cyl)) + geom_point()
  105. p2 <- ggplot(mtcars, aes(mpg, cyl)) + geom_line()
  106. p3 <- ggplot(mtcars, aes(mpg, cyl)) + geom_line(color="blue")
  107. p4 <- ggplot(mtcars, aes(mpg, cyl)) + geom_line(color="red")
  108.  
  109. library(cowplot)
  110. cowplot::plot_grid(p1, p2, p3, p4,
  111. nrow = 2, align = "hv")
  112.  
  113. library(ggpubr)
  114. ggpubr::ggarrange(p1, p2, p3, p4,
  115. nrow = 2, ncol = 2)
  116.  
  117. library(patchwork)
  118. p1 + p2 + p3 + p4
  119.  
  120. library(egg)
  121. egg::ggarrange(p1, p2, p3, p4, ncol = 2)
  122.  
  123. library(gridExtra)
  124. # Matriz para divisao do plot
  125. lay <- rbind(c(1,2),
  126. c(3,4))
  127.  
  128. gridExtra::grid.arrange(p1, p2, p3, p4, layout_matrix = lay)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement