Reputation: 23
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.
Upvotes: 0
Views: 276
Reputation: 33802
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
Upvotes: 1