Search code examples
rggplot2drawgammgcv

Creating an editable partial effect plot in R with the gratia::draw() function that also has a rugplot


The question I have has mostly been answered by the following post: Cannot update/edit ggplot2 object exported from a package (`gratia`) in R. When I refer to the mydraw.gam function, it comes from code in that post. What I am trying to do is use the mydraw.gam function with a rugplot that looks like the gratia::draw() function.

This is my data:

dput(LMB.stack)
structure(list(X1 = c(0.0541887294548451, 0.0721473880136936, 
0.0175421164050594, 0.0215182766921787, 0.0440735967747106, 0.046669040060852, 
0.0526230550013067, 0.112833597945919, 0.063812034754301, 0.0940158338572872, 
0.0506721208894938, 0.0127474420783362, 0.0657879523145501, 0.0541887294548451, 
0.0721473880136936, 0.0175421164050594, 0.0215182766921787, 0.0440735967747106, 
0.046669040060852, 0.0526230550013067, 0.112833597945919, 0.063812034754301, 
0.0940158338572872, 0.0506721208894938, 0.0127474420783362, 0.0382272328188603, 
0.0541887294548451, 0.0721473880136936, 0.0175421164050594, 0.0215182766921787, 
0.0440735967747106, 0.046669040060852, 0.0526230550013067, 0.112833597945919, 
0.063812034754301, 0.0940158338572872, 0.0506721208894938, 0.0127474420783362, 
0.0657879523145501, 0.0382272328188603, 0.0541887294548451, 0.0721473880136936, 
0.0175421164050594, 0.0215182766921787, 0.0440735967747106, 0.046669040060852, 
0.0526230550013067, 0.0056727211129064, 0.063812034754301, 0.0940158338572872, 
0.106570293080958, 0.116604915677637, 0.0422424508991219, 0.109071218434758, 
0.0666150693773212, 0.108073813949563, 0.0394885672397296, 0.0688845434754768, 
0.0530021292114909, 0.106570293080958, 0.116604915677637, 0.0422424508991219, 
0.109071218434758, 0.0666150693773212, 0.108073813949563, 0.0411444155997384, 
0.0394885672397296, 0.0688845434754768, 0.0530021292114909, 0.106570293080958, 
0.116604915677637, 0.0422424508991219, 0.109071218434758, 0.0666150693773212, 
0.108073813949563, 0.0411444155997384, 0.0394885672397296, 0.0688845434754768, 
0.0530021292114909, 0.0578017962016202, 0.106570293080958, 0.116604915677637, 
0.0422424508991219, 0.109071218434758, 0.0666150693773212, 0.174633119183298, 
0.0645268299068541, 0.0709485215243274, 0.0682173756351461, 0.0643514854635756, 
0.014808611175444, 0.163637352944664, 0.0599393459014399, 0.134349635442672, 
0.214544784680364, 0.0460287439577173, 0.0692001626120574, 0.0682173756351461, 
0.0643514854635756, 0.014808611175444), X2 = c(0.64, 0.47, 0.598, 
0.52, 0.41, 1.38, 0.53, 0.73, 0.367, 0.58, 0.75, 0.38, 0.227, 
0.39, 0.36, 0.35, 0.41, 0.84, 0.53, 0.55, 0.33, 0.33, 0.356, 
0.58, 0.33, 0.52, 0.43, 0.53, 0.45, 0.37, 0.54, 0.98, 0.789, 
0.44, 0.23, 0.21, 0.67144, 0.37, 0.38, 0.18, 0.24, 0.36, 0.37, 
0.16, 0.58, 0.44, 0.41, 0.16, 0.13, 0.55, 0.99, 2.31, 1.264, 
1.005, 1.345, 1.24, 1.665, 1.545, 0.799, 0.736, 1.237, 0.776, 
0.742, 1.0259, 0.66, 1.17, 0.864, 1.191, 0.631, 0.745, 0.866, 
0.917, 1.105, 1.04, 0.517, 1.236, 1.066, 1.35, 0.947, 0.74, 0.62, 
1.572, 0.56, 1.189, 0.645, 0.9, 0.74, 0.568, 1.14, 1.159, 1.325, 
1.217, 1.37, 1.147, 1.89, 1.19, 1.3, 0.73, 0.693, 1.06)), row.names = c(NA, 
100L), class = "data.frame")

This is what my gam looks like (using mgcv):

LMB.gam<-gam(X2~s(X1), data = LMB.stack)

When I use the draw(LMB.gam) command in the package gratia, this is what the partial effect plot looks like:

enter image description here

When I use the mydraw.gam command (see previous post) while trying to add a rug plot (see code below), this is what my plot looks like:

p<-mydraw.gam(LMB.gam)
p[[1]] +  geom_rug(position = "jitter",sides="b")

enter image description here

I need some help figuring out how to properly add a rug plot to an editable gratia::draw ggplot partial effect plot that corresponds to the actual data.

Thanks!


Solution

  • I would just use smooth_estimates() and its draw() method to plot a single smooth from the model. You can then add to it using standard ggplot2 functionality...

    # using your data in `df`
    m <- gam(X2 ~ s(X1), data = df)
    sm <- smooth_estimates(m, smooth = "s(X1)")
    
    draw(sm) +
      labs(title = "My title", y = "foo") +
      geom_rug(data = df,
               mapping = aes(x = X1),
               sides = "b",
               inherit.aes = FALSE)
    

    produces

    enter image description here