“plot_model() 在主题功能中一直返回 NULL”

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

plot_model() keeps returning NULL in theme features

问题

I am making use of the plot_model() predictive function for a multiple linear regression and am not able to modify aesthetics. I have read both that it is a part of the sjplot package as well as a wrapper for ggplot. While I have gotten pretty proficient in modifying/debugging ggplot aesthetics, I am finding this not working here.

It plots just fine in my code as is. I've put placeholder names for the variables.

  1. library(sjPlot)
  2. library(performance)
  3. library(ggplot2)
  4. library(sjmisc)
  5. setwd("C:/Users/directory/")
  6. df = read.csv('example.csv')
  7. df$id = factor(df$id)
  8. df$course = factor(df$course)
  9. lm_mod = lm(data = df, score ~ var1 + var2 + var3)
  10. summary(lm_mod)
  11. prediction = plot_model(lm_mod,
  12. type = "pred",
  13. show.data = TRUE,
  14. dot.size = 1
  15. )

As soon as I add themes, it returns NULL.

  1. prediction + set_theme(base = theme_minimal())

I want to adjust the axis scale/limits to match my other ggplot-made figures. The text size of the axis I would like to fix also. Image below, with the variable names removed

“plot_model() 在主题功能中一直返回 NULL”

If I try anything from sjplot or ggplot to modify the aesthetics/themes, they all universally return "NULL." Some examples below:

  1. > prediction + font_size(axis_title.x = 28)
  2. NULL
  3. > prediction + font_size(28)
  4. NULL
  5. > prediction + set_theme(base = theme_minimal())
  6. NULL
  7. > prediction + theme_sjplot(base_size = 24, base_family = "")
  8. NULL
  9. > prediction + theme(
  10. + text = element_text(size = 22),
  11. + axis.title = element_text(size = 28, face = "bold"),
  12. + axis.text = element_text(size = 24),
  13. + axis.line = element_line(colour = "black", size = 1, linetype = "solid"),
  14. + plot.title = element_text(size = 28, face = "bold", vjust = 2, hjust = 0.5)
  15. + )
  16. NULL

I have read related posts. Please help! I am very stuck trying to fix this.

英文:

I am making use of the plot_model() predictive function for a multiple linear regression and am not able to modify aesthetics. I have read both that it is a part of the sjplot package as well as a wrapper for ggplot. While I have gotten pretty proficient in modifying/debuggin ggplot aesthetics, I am finding this not working here.

It plots just fine in my code as is. I've put placeholder names for the variables.

  1. library(sjPlot)
  2. library(performance)
  3. library(ggplot2)
  4. library(sjmisc)
  5. setwd("C:/Users/directory/")
  6. df=read.csv('example.csv')
  7. df$id=factor(df$id)
  8. df$course=factor(df$course)
  9. lm_mod=lm(data=df, score~var1+var2+var3)
  10. summary(lm_mod)
  11. prediction = plot_model(lm_mod,
  12. type="pred",
  13. show.data=TRUE,
  14. dot.size = 1,
  15. )
  16. prediction

As soon as I add themes, it returns NULL.

  1. prediction + set_theme(base = theme_minimal())

I want to adjust the axis scale / limits to match my other ggplot made figures. The text size of the axis I would like to fix also. Image below, with the variable names removed

“plot_model() 在主题功能中一直返回 NULL”

If I try anything from sjplot or ggplot to modify the aesthetics/themes, they all universally return "NULL." Some examples below:

  1. > prediction + font_size(axis_title.x=28)
  2. NULL
  3. > prediction + font_size(28)
  4. NULL
  5. > prediction + set_theme(base = theme_minimal())
  6. NULL
  7. > prediction + theme_sjplot(base_size = 24, base_family="")
  8. NULL
  9. > prediction + theme(text=element_text(size=22),
  10. + axis.title = element_text(size=28,
  11. + face="bold" ),
  12. + axis.text = element_text(size=24),
  13. + axis.line = element_line(colour = "black",
  14. + size = 1,
  15. + linetype = "solid"),
  16. + plot.title = element_text(size=28,
  17. + face="bold",
  18. + vjust=2,
  19. + hjust=0.5))
  20. NULL

I have read related posts. Please help! I am very stuck trying to fix this.

答案1

得分: 1

根据帮助页面?plot_model中的数值部分,取决于绘图类型,plot_model()会返回一个ggplot对象或这类对象的列表。

以下是一个简单的示例:

  1. library(sjPlot)
  2. library(ggplot2)
  3. lm1 <- lm(mpg ~ wt, data = mtcars)
  4. p1 <- plot_model(lm1)
  5. p2 <- plot_model(lm1, type = "pred")
  6. class(p1)
  7. [1] "gg" "ggplot"
  8. class(p2)
  9. [1] "list"

因此,p1是一个ggplot对象,你可以这样做:

  1. p1 + theme_bw()

但是p2是一个具有名称的列表。它的名称来自模型中的自变量。如果你尝试为列表添加主题,你将会得到NULL。

  1. names(p2)
  2. [1] "wt"
  3. p2 + theme_bw()
  4. NULL

要访问列表中的ggplot部分,你可以这样做:

  1. p2$wt + theme_bw()

或者这样:

  1. p2[[1]] + theme_bw()

如果有多个自变量,你会得到一个与每个自变量对应的列表元素。在你的情况下:

  1. prediction$var1
  2. prediction$var2
  3. prediction$var3
英文:

If you look at the help page ?plot_model, under Value it states:

>Depending on the plot-type, plot_model() returns a ggplot-object or a list of such objects.

Here's a simple example:

  1. library(sjPlot)
  2. library(ggplot2)
  3. lm1 &lt;- lm(mpg ~ wt, data = mtcars)
  4. p1 &lt;- plot_model(lm1)
  5. p2 &lt;- plot_model(lm1, type = &quot;pred&quot;)
  6. class(p1)
  7. [1] &quot;gg&quot; &quot;ggplot&quot;
  8. class(p2)
  9. [1] &quot;list&quot;

So p1 is a ggplot object and you could do this:

  1. p1 + theme_bw()

But p2 is a named list. It gets its name from the independent variable in the model. You'll get NULL if you try to add themes to a list.

  1. names(p2)
  2. [1] &quot;wt&quot;
  3. p2 + theme_bw()
  4. NULL

So to access the ggplot part of the list, you can do this:

  1. p2$wt + theme_bw()

or this:

  1. p2[[1]] + theme_bw()

With more than one independent variable, you'll get a list element for each one. In your case:

  1. prediction$var1
  2. prediction$var2
  3. prediction$var3

huangapple
  • 本文由 发表于 2023年7月18日 08:55:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76708910.html
匿名

发表评论

匿名网友

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

确定