如何在R中的gam图中重命名x轴标签?

huangapple go评论53阅读模式
英文:

How to rename x-axis labels in gam plot in R?

问题

我使用以下代码拟合了模型:

mod1 <- gam(severity ~ s(mean_rh, k = 8) + s(mean_temp, k = 10) + s(mean_ws, k =7) + s(avg_daily_rain, k = 7), family = betar(), data = dat_seasonal)

以下是生成图表时使用的代码:

plot(mod1, pages = 1, all.terms = TRUE, rug = TRUE, residuals = TRUE, pch = 1, cex = 1, shade = TRUE, seWithMean = TRUE, shift = coef(mod1)[1])

我想通过在原始数据框中重命名列名来重命名x轴标签,使用新名称 = 旧语法,Mean temperature = mean_temp,Mean wind speed = mean_ws,Mean relative humidity = mean_rh,Mean rain per rainy day = avg_daily_rain

以下是我的代码:

names(dat_seasonal) <- c("disease_severity", "Mean relative humidity", "Mean temperature", "Mean wind speed", "Mean rain per rainy day")

mod1 <- gam(disease_severity ~ s(`Mean relative humidity`, k = 8) + s(`Mean temperature`, k = 10) + s(`Mean wind speed`, k =7) + s(`Mean rain per rainy day`, k = 7), family = betar(), data = dat_seasonal)

summary(mod1)

plot(mod1, pages = 1, all.terms = TRUE, rug = TRUE, residuals = TRUE, pch = 1, cex = 1, shade = TRUE, seWithMean = TRUE, shift = coef(mod1)[1])

我收到以下错误消息:

Error in str2lang(termtext) : <text>:1:6: unexpected symbol
1: Mean relative
         ^

显然,错误消息表明新名称中不应该包含任何空格。如何修复这个问题?谢谢

注意:我可以使用以下代码重命名单个项,但没有一种方法可以一次重命名所有预测变量。

plot(mod1, select = 4, xlab = "Mean rain per rainy day (mm)", rug = TRUE, residuals = TRUE, pch = 1, cex = 1, shade = TRUE, seWithMean = TRUE, shift = coef(mod1)[1])
英文:

I fitted the model using the following code

mod1 &lt;- gam(severity ~  s(mean_rh, k = 8) + s(mean_temp, k = 10) + s(mean_ws, k =7) + s(avg_daily_rain, k = 7), family = betar(),  data = dat_seasonal)

Here is the code used in producing plot

plot(mod1, pages = 1, all.terms = TRUE, rug = TRUE, residuals = TRUE, pch = 1, cex = 1, shade = TRUE, seWithMean = TRUE, shift = coef(mod1)[1])

I wanted to rename x-axis labels by renaming column names in the original data frame, using new name = old syntax, Mean temperature = mean_temp,
Mean wind speed = mean_ws,
Mean relative humidity = mean_rh,
Mean rain per rainy day = avg_daily_rain

Here is my code

names(dat_seasonal) &lt;- c(&quot;disease_severity&quot;, &quot;Mean relative humidity&quot;, &quot;Mean temperature&quot;, &quot;Mean wind speed&quot;, &quot;Mean rain per rainy day&quot;)

mod1 &lt;- gam(disease_severity ~  s(`Mean relative humidity`, k = 8) + s(`Mean temperature`, k = 10) + s(`Mean wind speed`, k =7) + s(`Mean rain per rainy day`, k = 7), family = betar(),  data = dat_seasonal_gam)

summary(mod1)


plot(mod1, pages = 1, all.terms = TRUE, rug = TRUE, residuals = TRUE, pch = 1, cex = 1, shade = TRUE, seWithMean = TRUE, shift = coef(mod1)[1])

I am getting the following error message

Error in str2lang(termtext) : &lt;text&gt;:1:6: unexpected symbol
1: Mean relative
         ^

Apparently, the error message suggests that there should not be any spaces in the new names. Any leads on how to fix this? Thanks

NOTE:
I can rename a single term using the following code, but there is no way rename all predictors at once.

plot(mod1, select = 4, xlab = &quot;Mean rain per rainy day (mm)&quot;, rug = TRUE, residuals = TRUE, pch = 1, cex = 1, shade = TRUE, seWithMean = TRUE, shift = coef(mod1)[1])

答案1

得分: 1

使用gratia包重命名各个图并使用patchwork包组合它们。

英文:

Ended up using gratia package to rename individual figures and then combine them using patchwork package.

p1 &lt;- draw(mod1, select = &quot;s(mean_rh)&quot;) +
      labs(x= &quot;Mean relative humidity (%)&quot;, title = &quot;&quot;)

p2 &lt;- draw(mod1, select = &quot;s(mean_temp)&quot;) +
   labs(x= &quot;Mean temperature (&#176;C)&quot;, title = &quot;&quot;) 

p3 &lt;- draw(mod1, select = &quot;s(mean_ws)&quot;) +
  labs(x= &quot;Mean wind speed (m/s)&quot;, title = &quot;&quot;) 

p4 &lt;- draw(mod1, select = &quot;s(avg_daily_rain)&quot;) +
  labs(x= &quot;Mean rain per rainy day (mm)&quot;, title = &quot;&quot;) 

p1 + p2 + p3 + p4 + plot_layout(ncol = 2, nrow = 2)

huangapple
  • 本文由 发表于 2023年2月14日 22:28:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75449259.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定