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

8

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

该函数包括一个colors参数,将colors设置为bw将更改linetype,但会将colors设置为灰度。这个问题类似,但没有得到有用的答案:Colored ribbons and different linetypes in sjPlot plot_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")

不同的颜色,但不包括线型

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"))


我该如何同时获得不同的颜色和不同的线型?换句话说,我想要像这样的效果:this
2个回答

7

sjPlot 在定制方面似乎比较死板,但有些方法能够解决这个问题。你可以从 ggeffects 包的 ggpredict 中获取数据,并在 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"))

example


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)

enter image description here


谢谢,它解决了部分问题:使用这个解决方案后,我的一条线变成了点状。1. 我该如何将其改为虚线?2. 我该如何使另一条线变为虚线,而不是自动变成点状的那条线? - Alex M

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接