使用plot_model()更改线型和线颜色

Jam*_*rus 8 r ggplot2 sjplot

plot_model()我正在尝试使用中的函数创建预测值图sjPlot。我希望我的预测线具有不同的线型和不同的颜色。

该函数包含一个colors参数,设置colorsbw将更改linetype,但设置colors为灰度。这个问题类似,但没有收到有用的答案:Coloredribsand differentlinetypesinsjPlotplot_model()

例子:

不同linetypes,但不是colors

data(iris)
toy_model <- lm( Sepal.Length ~ Sepal.Width + Species, data=iris)

my_plot <- plot_model(toy_model, type=("pred"),
terms=c("Sepal.Width","Species"),
colors="bw")

Run Code Online (Sandbox Code Playgroud)

不同colors,但不是linetypes

data(iris)
toy_model <- lm( Sepal.Length ~ Sepal.Width + Species, data=iris)

my_plot <- plot_model(toy_model, type=("pred"),
terms=c("Sepal.Width","Species"))

Run Code Online (Sandbox Code Playgroud)

怎样才能既不同colors又不同linetypes呢?换句话说,我想要这样的东西

这

Ari*_*hod 7

sjPlot在定制方面似乎相当僵化,但有一些方法可以解决它。ggpredict您可以从(从包中)获取数据ggeffects并像往常一样在ggplot.

df <- ggpredict(toy_model, terms = c("Sepal.Width","Species"))
ggplot(df, aes(x, predicted)) + 
    geom_line(aes(linetype=group, color=group)) +
    geom_ribbon(aes(ymin=conf.low, ymax=conf.high, fill=group), alpha=0.15) +
    scale_linetype_manual(values = c("solid", "dashed", "dotted"))
Run Code Online (Sandbox Code Playgroud)

例子


小智 5

plot_model确实允许ggplot2函数调整绘图的特征。您可以轻松更改颜色或线型。

library(sjPlot)
library(ggplot2)

data(iris)
toy_model <- lm( Sepal.Length ~ Sepal.Width + Species, data=iris)

#Use aes to change color or linetype
plot_model(toy_model, type=("pred"),
                      terms=c("Sepal.Width","Species")) + aes(linetype=group, color=group)

#Change color
plot_model(toy_model, type=("pred"),
                      terms=c("Sepal.Width","Species"), colors = "Set2") + aes(linetype=group, color=group)

Run Code Online (Sandbox Code Playgroud)

在此输入图像描述