gavinsimpson / gratia

ggplot-based graphics and useful functions for GAMs fitted using the mgcv package
https://gavinsimpson.github.io/gratia/
Other
206 stars 28 forks source link

draw.gam() should set widths = 1 #190

Closed gavinsimpson closed 2 years ago

gavinsimpson commented 2 years ago

The default behaviour in patchwork when aligning patches of plots where one or more has a fixed/equal coordinate system is not desirable for draw.gam() and related plotting functions as it reduces the widths of the columns to align the axes. In these situations we don't want to align the axes as we are specifically trying to preserve the aspect ratio for select plots.

The fix in this situation seems to be to set the widths argument such that all patches are the same size. See https://patchwork.data-imaginist.com/articles/guides/layout.html#fixed-aspect-plots

Skogur commented 2 years ago

I'm not sure if this is related or not, but will share in case it is helpful...

I have consistently been having issues when plotting factor-grouped* 2D smooths via gratia::draw(). I was (& remain) unsure of if this was on gratia's end or mine (perhaps multiple ggplot-related packages not playing nicely with one another?). However, after wrestling with this issue for some time, I recently discovered that simply adding + theme(aspect.ratio = 1) fixed the problem. Graphical output examples below.

Figured i should share in case it were related here or others are seeking solution for this as well....

### Before...

gam1 <- gam(y ~ factor1 + s(x1, x2, by = factor1), ...)  
draw(gam1, select = smooths(gam1)[2] ) 

image

### After...

  draw(gam1, select = smooths(gam1)[2] ) + 
      theme(aspect.ratio = 1)

image

PS major thanks for all that you do!

edit* : neglected to mention this was only an issue for 2D smooths grouped by factor (e.g., s(x1, x2, by = factor1))

gavinsimpson commented 2 years ago

@Skogur Looks like you shouldn't be using s() for this smooth as it assumes isotropy. Instead use te(x1, x2, by = f).

Skogur commented 2 years ago

Good catch! -___- this must be why taking breaks is important...