【问题标题】:Different line colors in forest plot output from sjPlot R packagesjPlot R 包的森林图输出中的不同线条颜色
【发布时间】:2019-08-31 02:35:00
【问题描述】:

我正在使用 sjPlot 包准备带有回归系数的森林图。如何自定义回归系数的线条颜色以使每个系数具有一种颜色?

我尝试在plot_model function 中使用参数colors = c("blue", "red", "black"),但它不起作用。我也尝试过将不同的调色板与scale_color_manual 结合使用,但也没有用。

这是来自sjPlot 包的示例:

library(sjPlot)
library(sjmisc)
data(efc)

#I used log before each predictor to have an example of confidence interval
fit <- lm(tot_sc_e ~ log(c161sex) + log(e17age) + log(c160age), data = efc)

plot_model(fit, colors = c("blue", "red", "black"))

它给了我一个带有两条蓝线和一条红线的情节。剧情中没有黑线!

尝试使用其他方法没有帮助:

plot_model(fit, colors = NULL)+
  scale_fill_sjplot(palette = "viridis", discrete = TRUE)+
  scale_color_viridis(discrete = TRUE)

它实际上使用了 viridis 调色板,但同样是两条紫色线和一条黄色线。它返回以下消息:

“'fill' 的比例已经存在。为 'fill' 添加另一个比例,它将替换现有的比例。” “'color' 的比例已经存在。为 'color' 添加另一个比例,它将替换现有的比例。”

如果我在代码中设置colors = NULL,它不应该返回这些消息,不是吗?

对于为每个预测变量获得一种不同颜色的任何帮助,我将不胜感激。 注意:此图将与具有相同预测变量的其他图相结合。因此,我希望它们在两个图中使用相同的颜色以提高可读性。

【问题讨论】:

    标签: r ggplot2 lm sjplot


    【解决方案1】:

    默认情况下,颜色/填充美学被映射到每个系数是正数还是负数。要覆盖此并告诉 sjPlot 将 3 个系数中的每一个视为自己的组,您可以将 group.terms = c(1, 2, 3) 指定为 plot_model 中的参数:

    plot_model(fit, 
               group.terms = c(1, 2, 3), 
               colors = c("blue", "red", "black"))
    

    该函数的默认行为还按字母顺序对系数进行排序,这会打乱蓝-红-黑序列。为了保持系数的原始顺序(即fit中的顺序),我们可以在plot_model中额外指定order.terms

    plot_model(fit, 
               group.terms = c(1, 2, 3), 
               order.terms = c(1, 2, 3),
               colors = c("blue", "red", "black"))
    

    【讨论】:

    • 非常感谢@Z.Lin 的回答。很高兴您展示了order.terms 参数,因为我尝试使用group.termssort.est = TRUE,但出现错误:Error: fun must return a single number per group。当我将sort.est = TRUE 替换为order.terms 时,它运行良好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多