2

I am trying to present a graph plotting the results of three regression equations using ggplot and gridArrange. However, I have noticed a problem when trying to plot the residuals of these regressions. I am trying to plot these graphs on a fixed coordinate system with an aspect ratio of 1, given they are both residuals of a dataset are proportional residuals relative to the original measurement. However, when I print the combined graph in ggExtra I end up with one graph being much shorter than the other and the titles of the graphs are not aligned.

I know this is due to the fact that both graphs are set to different width because of the coord_fixed call. However, I do not know how to adjust this in the final graph so both graphs are the same size. What I want to do is increase the white space of the smaller graph to make the two the same dimensions but keep the fixed 1:1: aspect ratio the same. I have found that including the respect=TRUE does not work to fix this.

Below is code that reproduces my example.

library(gridExtra)
library(ggplot2)
data(mtcars)
lm1<-lm(disp~drat,data=mtcars)
lm2<-lm(hp~drat,data=mtcars)
lm3<-lm(disp~hp,data=mtcars)
residuals<-data.frame(lm1=residuals(lm1),lm2=residuals(lm2),lm3=residuals(lm3))
(resid2<-grid.arrange(
  ggplot(residuals,aes(lm1,lm2))+
    geom_point(size=3,shape=21,fill="gray")+
    theme_classic()+
    ggtitle("Plot1")+
    coord_fixed(),
  ggplot(residuals,aes(lm1,lm3))+
    geom_point(size=3,shape=21,fill="gray")+
    ggtitle("Plot2")+
    theme_classic()+
    coord_fixed(),
  nrow=1))
user2352714
  • 314
  • 1
  • 15
  • add parentheses around theme_classic. Does this question help you? https://stackoverflow.com/questions/26088771/combine-ggplots-but-fix-the-size-ratio-of-the-plots – ecology Mar 22 '21 at 18:55
  • 1
    @Blacklivesmatter I had seen the previous question, which was where I tried respect=TRUE from. Unfortunately that question's answer did not seem to work. – user2352714 Mar 22 '21 at 19:20

1 Answers1

1

EDIT after clarification: We can use ylim(-150, 200) in plot1

library(gridExtra)
library(ggplot2)
data(mtcars)
lm1<-lm(disp~drat,data=mtcars)
lm2<-lm(hp~drat,data=mtcars)
lm3<-lm(disp~hp,data=mtcars)
residuals<-data.frame(lm1=residuals(lm1),lm2=residuals(lm2),lm3=residuals(lm3))
(resid2<-grid.arrange(
  ggplot(residuals,aes(lm1,lm2))+
    geom_point(size=3,shape=21,fill="gray")+
    ylim(-150, 200) +
    theme_classic()+
    ggtitle("Plot1")+
    coord_fixed(),
  ggplot(residuals,aes(lm1,lm3))+
    geom_point(size=3,shape=21,fill="gray")+
    ggtitle("Plot2")+
    theme_classic()+
    coord_fixed(),
  nrow=1))

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66
  • 1
    This doesn't work for me. I ran the exact code in the answer and got the error message "can't add `p2` to a ggplot object". Additionally when I ran the gridExtra code in the answer I got the same plot as before. Do you know what I might be doing wrong? – user2352714 Mar 22 '21 at 19:28
  • Ok. you are right. After starting `R` new I got the same error. See my edit. Now it should work. – TarJae Mar 22 '21 at 19:49
  • It's still not doing what I am trying to do. I noticed by calling the two functions back-to-back the new code is taking the entire plot and stretching the y-axis. I am trying to extend the blank area of the first y axis up such that its height is equal to the second while retaining the aspect ratio of 1. The code here is also causing the plot axis to disappear. – user2352714 Mar 22 '21 at 20:00
  • I am sorry but for me it is not clear what you want. What do you mean by extend the blank area (which part of the blank area of y?) Do you want to have the same y axis scale? – TarJae Mar 22 '21 at 21:31
  • basically what I am trying to do is keep the aspect scaling ratio as 1:1 for length and width, but just extend the y axis up and down so the two plots are the same height. So for the original left figure instead of the y axis extending from (150,-100) it might go to (200,-150), such that the two graphs are the same height but both have the same aspect ratio. – user2352714 Mar 23 '21 at 04:27