0
votes

I would like to stick four plots in a 2x2 plot to fit on a single page. I'm using the package Sweave to generate PDF with Latex.

This is my code, however, it generates each plot to a single page.

<<plot1, fig=TRUE, echo=FALSE>>=
slices <- c(3, 1,1)
lbls <- c("Bien", "Regular", "Mal")
pie(slices, labels = lbls, main="¿Cómo te está sentando la medicación?")
@

<<plot2, fig=TRUE, echo=FALSE>>=
slices <- c(2, 2,1)
lbls <- c("Si", "Puedo mejorarla", "No")
pie(slices, labels = lbls, main="¿Estás llevando una alimentación adecuada?")
@

<<plot3, fig=TRUE, echo=FALSE>>=
slices <- c(1, 1,3)
lbls <- c("Si", "Puedo hacer más", "No")
pie(slices, labels = lbls, main="¿Realizas suficiente ejercicio físico?")
@

<<plot4, fig=TRUE, echo=FALSE>>=
slices <- c(3, 3, 1,4,1)
lbls <- c("Contento", "Tranquilo", "Enfadado", "Bien", "Nervioso", "Deprimido")
pie(slices, labels = lbls, main="¿Cómo te has sentido anímicamente esta semana?")
@
1
use par(mfrow = c(2,2)) and make only one plot with Sweave. - Mikko
Thanks @Mikko, how I create a unique plot with Sweave? - R user
@R_user: do you mean cross-referable plot? I would combine the plots using the suggestion above and add letters for each subplot. See, for example here: seananderson.ca/2013/10/21/panel-letters.html - Mikko

1 Answers

1
votes

Instead of trying to sweave the subfigures together, I would create one plot in Sweave, combine the plots in R using par and add subfigure letters for in-text references. For example:

<<plot1, fig=TRUE, echo=FALSE>>=
par(mfrow = c(2,2))

slices <- c(3, 1,1)
lbls <- c("Bien", "Regular", "Mal")
pie(slices, labels = lbls, main="¿Cómo te está sentando la medicación?")
legend("topleft", letters[1], bty = "n")

slices <- c(2, 2,1)
lbls <- c("Si", "Puedo mejorarla", "No")
pie(slices, labels = lbls, main="¿Estás llevando una alimentación adecuada?")
legend("topleft", letters[2], bty = "n")

slices <- c(1, 1,3)
lbls <- c("Si", "Puedo hacer más", "No")
pie(slices, labels = lbls, main="¿Realizas suficiente ejercicio físico?")
legend("topleft", letters[3], bty = "n")

slices <- c(3, 3, 1,4,1)
lbls <- c("Contento", "Tranquilo", "Enfadado", "Bien", "Nervioso", "Deprimido")
pie(slices, labels = lbls, main="¿Cómo te has sentido anímicamente esta semana?")
legend("topleft", letters[4], bty = "n")
@