1

I'm having some confusion with the size(s) of the grid.extra output.

I have the following ggplots:

k_uniforme <- covid %>%
  drop_na(PC) %>%
ggplot(aes(x=PC)) + 
  stat_density(kernel = "rectangular",  bw = hsilv, fill="#440154ff") +
  labs(title="Uniform") +
  theme_classic()

k_triangular <-  covid %>%
  drop_na(PC) %>%
ggplot(aes(x=PC)) + 
  stat_density(kernel = "triangular",  bw = hsilv, fill="#7ad151ff") +
  labs(title="Triangular") +
  theme_classic()

k_gaussian <-  covid %>%
  drop_na(PC) %>%
ggplot(aes(x=PC)) + 
  stat_density(kernel = "gaussian",  bw = hsilv, fill="#414487ff") +
  labs(title="Gausiana") +
  theme_classic()

k_epanechnikov <-  covid %>%
  drop_na(PC) %>%
ggplot(aes(x=PC)) + 
  stat_density(kernel = "epanechnikov",  bw = hsilv, fill="#fde725ff") +
  labs(title="Epanechnikov") +
  theme_classic()

k_biweight <-  covid %>%
  drop_na(PC) %>%
ggplot(aes(x=PC)) + 
  stat_density(kernel = "biweight",  bw = hsilv, fill="#fde725ff") +
  labs(title="Epanechnikov") +
  theme_classic()

And I want to print all of them in only one figure, preferably in the size of one entire page.

When I use the grid.arrange function for four of my plots I get the following output after kniting for pdf, which is very acceptable in terms of the size of the plots.

gridExtra::grid.arrange(k_uniforme, k_triangular, k_gaussian, k_epanechnikov, ncol=2, nrow=2)

Screenshot of the PDF output

However, when I want to arrange all of them (5 plots) the size of each plot is completely different and looks bad.

gridExtra::grid.arrange(k_uniforme, k_triangular, k_gaussian, k_epanechnikov, k_biweight, ncol=2, nrow=3)

Screenshot of the PDF output

My questions are the following: What is the default size of each plot in the 4plot-arrange? Is there any way to make the size of each plot in the 5plot-arrange similar to the one of the 4-plot arrange? And, How to center the 5th plot (i.e. the one in the third row) of the 5plot-arrange?

I've been searching for any help of grid.extra but the helps, blogs, etc. are really confusing. The document I'm trying to generate is a pdf using rmarkdown.

Thank you in advance!

1 Answers1

1

I already found the solution to my problems.

I just realized that I was mixing some things.

First, the size of the grid.arrange figure is adjusted by the definition of the height and width options defined in my chunk. Therefore I adjusted it properly using fig.width and fig.height as already explained in the RMarkdown Cookbook by Yuhui (see here and here for additional info).

For my last question about how to center the last plot in the 5-plot arrange I was missing the layout_matrix option of the grid.arrange function. It is partially responded here and here.

In the layout matrix I defined the positions of the plots I wanted to include. Consider the following:

layout_matrix <- matrix(c(1, 1, 2, 2, 3, 3, 4, 4, NA, 5, 5, NA), ncol = 4, byrow = TRUE)

Which returns a matrix like this:

     [,1] [,2] [,3] [,4]
[1,]    1    1    2    2
[2,]    3    3    4    4
[3,]   NA    5    5   NA

The numbers define the plots supplied into the function (i.e. 1 to 5) and their position. The NA can also be substituted for a 6 because I do not have a sixth plot (I prefer NA because is more intuitive).

In the grid.arrange I just defined the matrix as:

grid.arrange(k_uniforme, k_triangular, k_gaussian, k_epanechnikov, k_biweight, layout_matrix = layout_matrix)

My final code looks like this:

{r kernel, fig.cap="Graficas con diferentes densidad Kernel", fig.width=7, fig.height=10}

k_uniforme <- covid %>%
  drop_na(PC) %>%
ggplot(aes(x=PC)) + 
  stat_density(kernel = "rectangular",  bw = hsilv, fill="#440154ff") +
  labs(title="Uniform") +
  theme_classic()

k_triangular <-  covid %>%
  drop_na(PC) %>%
ggplot(aes(x=PC)) + 
  stat_density(kernel = "triangular",  bw = hsilv, fill="#7ad151ff") +
  labs(title="Triangular") +
  theme_classic()

k_gaussian <-  covid %>%
  drop_na(PC) %>%
ggplot(aes(x=PC)) + 
  stat_density(kernel = "gaussian",  bw = hsilv, fill="#414487ff") +
  labs(title="Gausiana") +
  theme_classic()

k_epanechnikov <-  covid %>%
  drop_na(PC) %>%
ggplot(aes(x=PC)) + 
  stat_density(kernel = "epanechnikov",  bw = hsilv, fill="#fde725ff") +
  labs(title="Epanechnikov") +
  theme_classic()

k_biweight <-  covid %>%
  drop_na(PC) %>%
ggplot(aes(x=PC)) + 
  stat_density(kernel = "biweight",  bw = hsilv, fill="#F24D29") +
  labs(title="biweight") +
  theme_classic()

layout_matrix <- matrix(c(1, 1, 2, 2, 3, 3, 4, 4, NA, 5, 5, NA), ncol = 4, byrow = TRUE)

grid.arrange(k_uniforme, k_triangular, k_gaussian, k_epanechnikov, k_biweight, layout_matrix = layout_matrix)

And my pdf document looks perfect:

Screenshot of pdf output