r - 使用 gratia::draw() 函数在 R 中创建一个可编辑的部分效果图,该函数也有一个 rugplot

标签 r ggplot2 draw gam mgcv

我的问题主要由以下帖子回答:Cannot update/edit ggplot2 object exported from a package (`gratia`) in R .当我提到 mydraw.gam 函数时,它来自该帖子中的代码。我想要做的是将 mydraw.gam 函数与看起来像 gratia::draw() 函数的 rugplot 结合使用。

这是我的数据:

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")

这是我的 gam 的样子(使用 mgcv):

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

当我在 gratia 包中使用 draw(LMB.gam) 命令时,部分效果图如下所示:

enter image description here

当我使用 mydraw.gam 命令(参见上一篇文章)尝试添加地毯图(参见下面的代码)时,我的图如下所示:

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

enter image description here

我需要一些帮助来弄清楚如何正确地将地毯图添加到与实际数据相对应的可编辑 gratia::draw ggplot 部分效果图中。

谢谢!

最佳答案

我只会使用 smooth_estimates() 及其 draw() 方法从模型中绘制单个平滑图。然后,您可以使用标准的 ggplot2 功能向其中添加...

# 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)

产生

enter image description here

关于r - 使用 gratia::draw() 函数在 R 中创建一个可编辑的部分效果图,该函数也有一个 rugplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70204560/

相关文章:

r - 如何将函数映射到 R 中 tibble 中的每一行?

r - ddply/transform 不会将函数应用于字符向量的每个元素

r - 如何在堆积条形图中使用百分比作为标签?

r - 有没有一种方法可以使用geom_raster在ggplot2中的矩形周围创建边框?

android - 单击已注册但未在 canvas android 中绘制

c++ - Direct2D 加载和绘制位图

python - 为什么我的 curses box 不绘制?

css - shinydashboard 的选项卡框 CSS

r - R 中的错​​误 : could not find function . ..

r - ggplot 不再在组之间绘制连接线意味着什么?