1

I have been using draw() command, from gratia package in R, in order to visualize a multivariate GAM, including a smooth factor for geographical data.

The main problem is that the produced graph is slightly unreadable due to the opaqueness of the points. I am going to be using some made up code from Gavin Simpson's gratia Guide.

Here's the script used:

library(gratia)
library(ggplot2)
library(mgcv)

df2 <- data_sim(2, n = 1000, dist = "normal", scale = 1, seed = 2)
m2 <- gam(y ~ s(x, z, k = 40), data = df2, method = "REML")

draw(m2, contour = FALSE)

The graph produced is not exactly what I am looking for, since I said again, my smooth factors demonstrates geographical data. Here, the points appear to have more space between them, and thus, they are more readable.

Using the draw command again on my GAM, the produced result is this. The basic principles remain the same.

user438383
  • 5,716
  • 8
  • 28
  • 43
helianthus
  • 73
  • 8
  • Hi, can you please add a reproducible extract of data so we can test your code? See https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example and https://stackoverflow.com/help/minimal-reproducible-example if you need tips and tricks about how to do it. – Paul Jun 07 '22 at 06:39

1 Answers1

0

As suggested here, it seems better to use ggplot to make the plot if you need further customizations.

See this paragraph about the sm object: https://gavinsimpson.github.io/gratia/articles/custom-plotting.html#building-your-own-plot-by-hand

See the example below for a quick view. You can edit the alpha argument to make the points less "invasive". Of course you need to change the parameters of the fill scale to mimic the plot made with gratia::draw().

library(gratia)
library(ggplot2)
library(mgcv)
library(magrittr)

df2 <- data_sim(2, n = 1000, dist = "normal", scale = 1, seed = 2)
m2 <- gam(y ~ s(x, z, k = 40), data = df2, method = "REML")
sm <- smooth_estimates(m2)
ggplot() +
  geom_tile(data = sm, aes(x = x, y = z, fill = est)) +
  geom_point(aes(x = x, y = z),
             data = df2, cex = 1.5, colour = "black", alpha = 0.05) +
  scale_fill_gradient2()

Created on 2022-06-07 by the reprex package (v2.0.1)

Paul
  • 2,850
  • 1
  • 12
  • 37