4

I would like to combine multiple plots in one plot using mfrow function.

Then, I would like to add the number of row and column to this plot.

For example,

par(mfrow=c(3,3))
plot(rnorm(10,1,3))
plot(rnorm(10,2,4))
plot(rnorm(10,3,3))
plot(rnorm(10,4,4))
plot(rnorm(10,5,2))
plot(rnorm(10,3,0.5))

I would like to have similar to this:This photo

Any help, please?

alistaire
  • 42,459
  • 4
  • 77
  • 117
  • Cannot find anything like that in the documentation of par / layout / ggplot. Closest I can think of is including a plot title with the number, e.g. `plot(rnorm(10,1,3),main="1")` – CIAndrews Nov 19 '18 at 06:40
  • @CIAndrews Thank you so much for your comment. I have about 13 plots combined in one plot with their own title. However, some of these plots are very similar and hence, I tried to make it is easy to refer to the individual plot. –  Nov 19 '18 at 06:56
  • 4
    Easiest: `ggplot::facet_grid`, tons of posts on SO. `base`R: [Plot with multiple graphs](https://stackoverflow.com/questions/23213135/plot-with-multiple-graphs); [Title key on each panel of a plot generated with par(mfrow=c(x,y))](https://stackoverflow.com/questions/19690175/title-key-on-each-panel-of-a-plot-generated-with-parmfrow-cx-y); [How to add annotations to a 28 multi-plot case in R?](https://stackoverflow.com/questions/44818419/how-to-add-annotations-to-a-28-multi-plot-case-in-r). [A nice cheat sheet](https://www.rstudio.com/wp-content/uploads/2016/10/how-big-is-your-graph.pdf) – Henrik Nov 19 '18 at 07:44
  • 1
    One vote for `facet_grid`. Would be much easier – Tung Nov 19 '18 at 09:27

1 Answers1

0

I think you can consider GGally package. It draws pairwise scatter plot using ggplot2. I guess this is what you want.

library(tidyverse)
(mu_sd <- # parameters
  tribble(
    ~m, ~s,
    #--/--/
    1, 3,
    2, 4,
    3, 3,
    4, 4,
    5, 2,
    3, .5
  ))
#> # A tibble: 6 x 2
#>       m     s
#>   <dbl> <dbl>
#> 1     1   3  
#> 2     2   4  
#> 3     3   3  
#> 4     4   4  
#> 5     5   2  
#> 6     3   0.5

Applying rnorm to this data frame, we can get another data frame to draw a plot

set.seed(10)
(rand_norm <-
  mu_sd %>% 
  apply(1, rnorm, n = 10) %>% # to each row
  as_tibble())
#> # A tibble: 10 x 6
#>        V1    V2    V3    V4    V5    V6
#>     <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1  1.02   3.10 2.40   2.15  6.09 2.60 
#>  2  2.82   4.76 0.815  3.92  1.24 0.165
#>  3 -0.371  1.76 2.33   4.97  4.17 4.37 
#>  4  2.40   4.99 0.881  4.18  2.83 2.64 
#>  5  1.29   2.74 1.73   2.62  4.03 3.51 
#>  6  3.39   4.09 2.63   2.56  1.97 1.29 
#>  7 -0.208  1.05 2.31   4.36  5.23 2.10 
#>  8  2.64   3.80 2.13   2.24  1.70 1.03 
#>  9 -0.627  2.93 2.90   3.68  4.32 2.35 
#> 10  2.74   4.48 2.75   3.35  2.66 0.791

Here, each column came from each population.

You can now use ggpairs() in GGally.

GGally::ggpairs(rand_norm)

enter image description here

You can specify what to draw in each upper, lower, and diag side of the plot. This is the default result. In the lower part, you might see the result what you want.

If you are interested in the detail, then you can go to this link GGally. However in this situation I think the default option is enough to solve the problem.

younggeun
  • 923
  • 1
  • 12
  • 19