英文:
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.
library(sjPlot)
library(performance)
library(ggplot2)
library(sjmisc)
setwd("C:/Users/directory/")
df = read.csv('example.csv')
df$id = factor(df$id)
df$course = factor(df$course)
lm_mod = lm(data = df, score ~ var1 + var2 + var3)
summary(lm_mod)
prediction = plot_model(lm_mod,
type = "pred",
show.data = TRUE,
dot.size = 1
)
As soon as I add themes, it returns NULL.
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
If I try anything from sjplot or ggplot to modify the aesthetics/themes, they all universally return "NULL." Some examples below:
> prediction + font_size(axis_title.x = 28)
NULL
> prediction + font_size(28)
NULL
> prediction + set_theme(base = theme_minimal())
NULL
> prediction + theme_sjplot(base_size = 24, base_family = "")
NULL
> prediction + theme(
+ text = element_text(size = 22),
+ axis.title = element_text(size = 28, face = "bold"),
+ axis.text = element_text(size = 24),
+ axis.line = element_line(colour = "black", size = 1, linetype = "solid"),
+ plot.title = element_text(size = 28, face = "bold", vjust = 2, hjust = 0.5)
+ )
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.
library(sjPlot)
library(performance)
library(ggplot2)
library(sjmisc)
setwd("C:/Users/directory/")
df=read.csv('example.csv')
df$id=factor(df$id)
df$course=factor(df$course)
lm_mod=lm(data=df, score~var1+var2+var3)
summary(lm_mod)
prediction = plot_model(lm_mod,
type="pred",
show.data=TRUE,
dot.size = 1,
)
prediction
As soon as I add themes, it returns NULL.
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
If I try anything from sjplot or ggplot to modify the aesthetics/themes, they all universally return "NULL." Some examples below:
> prediction + font_size(axis_title.x=28)
NULL
> prediction + font_size(28)
NULL
> prediction + set_theme(base = theme_minimal())
NULL
> prediction + theme_sjplot(base_size = 24, base_family="")
NULL
> prediction + theme(text=element_text(size=22),
+ axis.title = element_text(size=28,
+ face="bold" ),
+ axis.text = element_text(size=24),
+ axis.line = element_line(colour = "black",
+ size = 1,
+ linetype = "solid"),
+ plot.title = element_text(size=28,
+ face="bold",
+ vjust=2,
+ hjust=0.5))
NULL
I have read related posts. Please help! I am very stuck trying to fix this.
答案1
得分: 1
根据帮助页面?plot_model
中的数值部分,取决于绘图类型,plot_model()
会返回一个ggplot对象或这类对象的列表。
以下是一个简单的示例:
library(sjPlot)
library(ggplot2)
lm1 <- lm(mpg ~ wt, data = mtcars)
p1 <- plot_model(lm1)
p2 <- plot_model(lm1, type = "pred")
class(p1)
[1] "gg" "ggplot"
class(p2)
[1] "list"
因此,p1
是一个ggplot
对象,你可以这样做:
p1 + theme_bw()
但是p2
是一个具有名称的列表。它的名称来自模型中的自变量。如果你尝试为列表添加主题,你将会得到NULL。
names(p2)
[1] "wt"
p2 + theme_bw()
NULL
要访问列表中的ggplot
部分,你可以这样做:
p2$wt + theme_bw()
或者这样:
p2[[1]] + theme_bw()
如果有多个自变量,你会得到一个与每个自变量对应的列表元素。在你的情况下:
prediction$var1
prediction$var2
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:
library(sjPlot)
library(ggplot2)
lm1 <- lm(mpg ~ wt, data = mtcars)
p1 <- plot_model(lm1)
p2 <- plot_model(lm1, type = "pred")
class(p1)
[1] "gg" "ggplot"
class(p2)
[1] "list"
So p1
is a ggplot
object and you could do this:
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.
names(p2)
[1] "wt"
p2 + theme_bw()
NULL
So to access the ggplot
part of the list, you can do this:
p2$wt + theme_bw()
or this:
p2[[1]] + theme_bw()
With more than one independent variable, you'll get a list element for each one. In your case:
prediction$var1
prediction$var2
prediction$var3
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论