在 R 中获取 sjPlot 以显示和排序估计值

发布于 2025-01-12 13:48:44 字数 1263 浏览 6 评论 0原文

我正在尝试在 sjPlot 中制作一个交互图,显示在我的预测变量的两种条件下我的结果的百分比概率。一切都很完美,除了 show.values = T 和 sort.est = T 参数,它们似乎没有做任何事情。有办法让它发挥作用吗?或者,如果没有,我如何提取 sjPlot 用于创建该图的数据帧?寻找某种方法来标记或制表显示的概率值。谢谢你!

以下是一些示例数据以及迄今为止我所拥有的数据:

set.seed(100)
dat <- data.frame(Species = rep(letters[1:10], each = 5),
                  threat_cat = rep(c("recreation", "climate", "pollution", "fire", "invasive_spp"), 10),
                  impact.pres = sample(0:1, size = 50, replace = T),
                  threat.pres = sample(0:1, size = 50, replace = T))

mod <- glm(impact.pres ~ 0 + threat_cat/threat.pres, 
           data = dat, family = "binomial")

library(sjPlot)
library(ggpubr)
plot_model(mod, type = "int", 
           title = "", 
           axis.title = c("Threat category", "Predicted probabilities of threat being observed"), 
           legend.title = "Threat predicted",
           colors = c("#f2bf10",
                      "#4445ad"),
           line.size = 2,
           dot.size = 4,
           sort.est = T,
           show.values = T)+
  coord_flip()+
  theme_pubr(legend = "right", base_size = 30)

在此处输入图像描述

I am trying to make an interaction plot in sjPlot showing percent probabiliites of my outcome under two conditions of my predictive variable. Everything works perfectly, except the show.values = T and sort.est = T arguments, which don't seem to do anything. Is there a way to get this to work? Or, if not, how can I extract the dataframe sjPlot is using to create this figure? Looking for some way to either label or tabulate the displayed probability values. Thank you!

Here is some example data and what I have so far:

set.seed(100)
dat <- data.frame(Species = rep(letters[1:10], each = 5),
                  threat_cat = rep(c("recreation", "climate", "pollution", "fire", "invasive_spp"), 10),
                  impact.pres = sample(0:1, size = 50, replace = T),
                  threat.pres = sample(0:1, size = 50, replace = T))

mod <- glm(impact.pres ~ 0 + threat_cat/threat.pres, 
           data = dat, family = "binomial")

library(sjPlot)
library(ggpubr)
plot_model(mod, type = "int", 
           title = "", 
           axis.title = c("Threat category", "Predicted probabilities of threat being observed"), 
           legend.title = "Threat predicted",
           colors = c("#f2bf10",
                      "#4445ad"),
           line.size = 2,
           dot.size = 4,
           sort.est = T,
           show.values = T)+
  coord_flip()+
  theme_pubr(legend = "right", base_size = 30)

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半枫 2025-01-19 13:48:44

sjPlot 生成一个 ggplot 对象,因此您可以检查美学映射和基础数据。经过一番挖掘后,您会发现文本标签的 x、y 位置的默认映射已经是正确的,因此您需要做的就是向绘图添加一个 geom_text ,并且只需要将标签指定为美学映射。您可以从存储在 ggplot 对象中的名为 predicted 的列中获取标签。

结果是,如果您将以下图层添加到绘图中:

geom_text(aes(label = scales::percent(predicted)), 
          position = position_dodge(width = 1), size = 8)

您将得到

在此处输入图像描述

按顺序获取标签比较棘手。你必须摆弄情节的内部组件才能做到这一点。假设我们将上面的图存储为 p,那么我们可以通过执行以下操作按预测百分比进行排序:

p$data <- as.data.frame(p$data)
ord <- p$data$x[p$data$group == 1][order(p$data$predicted[p$data$group == 1])]
p$data$x <-  match(p$data$x, ord)
p$scales$scales[[1]]$labels <- p$scales$scales[[1]]$labels[ord]

p

在此处输入图像描述

sjPlot produces a ggplot object, so you can examine the aesthetic mappings and underlying data. After a bit of digging around you will find the default mapping is already correct for the x, y placements of text labels, so all you need to do is add a geom_text to the plot, and only need to specify the labels as an aesthetic mapping. You can get the labels from a column called predicted stored in the ggplot object.

The upshot is that if you add the following layer to your plot:

geom_text(aes(label = scales::percent(predicted)), 
          position = position_dodge(width = 1), size = 8)

You get

enter image description here

Getting the labels in order is trickier. You have to fiddle with the internal components of the plot to do this. Suppose we store the above plot as p, then we can sort by the predicted percentages by doing:

p$data <- as.data.frame(p$data)
ord <- p$data$x[p$data$group == 1][order(p$data$predicted[p$data$group == 1])]
p$data$x <-  match(p$data$x, ord)
p$scales$scales[[1]]$labels <- p$scales$scales[[1]]$labels[ord]

p

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文