Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you force Rmarkdown plots to be Square instead of Rectangle?

Tags:

r

I have a Generalized Linear Model (GLM) that I'm plotting diagnostics for using the glm.diag.plots function in the MASS package. But it tends to plot rectangular instead of square, which is very ugly for publication.

Below is some sample code that shows the problem in an .Rmd file. In Rstudio, you can just drag the window around until it's square, but not possible in Rmarkdown documents, and I'd like to enforce square manually. enter image description here

I checked in the ggplot documentation for ways to enforce square plotting, but could not find anything. glm.diag.plot() appears to use split.screen(), which doesn't provide any documentation for enforcing aspect ratios, either.

like image 595
TheProletariat Avatar asked Dec 21 '25 09:12

TheProletariat


2 Answers

@rawr's comment is spot-on; this is a knitr/markdown issue, not glm.diag or ggplot or anything else. All you need to do is specify the desired height and width of the output (in inches, by default) using fig.width and fig.height.

enter image description here

like image 187
Aaron left Stack Overflow Avatar answered Dec 23 '25 23:12

Aaron left Stack Overflow


It looks like you are using glm.diag.plots from package boot to acquire plots. You could recreate them using ggplot if you wish. Here is an example:

some model:

data(anorexia, package = "MASS")

anorex.1 <- glm(Postwt ~ Prewt + Treat + offset(Prewt),
                family = gaussian, data = anorexia)

the glm.diag.plots output

library(boot)
glm.diag.plots(anorex.1)

enter image description here

To create each plot in ggplot first get an object from glm.diag.plots

z <- glm.diag.plots(anorex.1, ret = T)

then plot each plot:

library(ggplot2)

plot1 <- ggplot(data.frame(x = predict(anorex.1),
                           y = z$res))+
  geom_point(aes(x, y)) +
  xlab("Linear predictor") +
  ylab("Residuals") +
  theme_bw()+
  theme(aspect.ratio=1)

plot2 <- ggplot(data.frame(x = qnorm(ppoints(length(z$rd)))[rank(z$rd)],
                           y = z$rd)) +
  geom_point(aes(x, y)) +
  xlab("Ordered deviance residuals") +
  ylab("Quantiles of standard normal") +
  geom_abline(intercept = 0, slope = 1, lty =2) +
  theme_bw()+
  theme(aspect.ratio=1)

plot3 <- ggplot(data.frame(x = z$h/(1-z$h),
                           y = z$cook)) +
  geom_point(aes(x, y)) +
  xlab("h/(h-1)") +
  ylab("Cook statistic") +
  theme_bw()+
  theme(aspect.ratio=1)

plot4 <- ggplot(data.frame(x = 1:length(z$cook),
                           y = z$cook)) +
  geom_point(aes(x, y)) +
  xlab("Case") +
  ylab("Cook statistic") +
  theme_bw()+
  theme(aspect.ratio=1)

then combine them

library(cowplot)

plot_grid(plot1, plot2, plot3, plot4, ncol = 2)

enter image description here

Now you can customize each plot the way you wish.

like image 22
missuse Avatar answered Dec 23 '25 23:12

missuse



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!