【问题标题】:Changing linetype and line color with plot_model()使用 plot_model() 更改线型和线条颜色
【发布时间】:2019-08-17 13:27:42
【问题描述】:

我正在尝试使用来自sjPlotplot_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")

不同的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"))

我怎样才能同时获得不同的colors 和不同的linetypes?换句话说,我想要这样的东西

【问题讨论】:

    标签: r ggplot2 sjplot


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      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. 我怎样才能让另一条线变成虚线,而不是自动点的那条?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-07
      • 2017-10-21
      • 2021-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-09
      相关资源
      最近更新 更多