gpt4 book ai didi

How do I add a legend indicating significance levels below a ggplot object?(如何在ggPlot对象下方添加指示重要性级别的图例?)

转载 作者:bug小助手 更新时间:2023-10-25 14:41:35 28 4
gpt4 key购买 nike



I'm using ggforestplot() to plot the results from my several regression models where some of the data have been imputed with mice(). But for the sake of this MWE, I will use the example data of the ggforestplot() package instead:

我正在使用ggForestlot()来绘制我的几个回归模型的结果,其中一些数据被归因于MICE()。但出于这一MWE的目的,我将改用ggForestlot()包的示例数据:


# Load packages
library(ggforestplot)
library(tidyverse)

# Use the example data of the ggforesplot() package (only a few rows of it)
df <- ggforestplot::df_linear_associations %>%
filter(trait == "BMI") %>% slice(26:29)

In real world, I have 1) first run summary(pool(modelFit)) separately for all the outcomes of my (partially imputed) data and 2) then gathered the results from those several models to just one data frame. The structure of my real data frame is basically identical with what the above example code produces. The example data frame:

在现实世界中,我有1)首先对我(部分归因于)数据的所有结果分别运行汇总(池(ModelFit)),2)然后将这几个模型的结果收集到一个数据帧中。我的实际数据帧的结构与上面的示例代码生成的内容基本相同。示例数据框:


# Display the example data
df
#> # A tibble: 4 × 5
#> name trait beta se pvalue
#> <chr> <chr> <dbl> <dbl> <dbl>
#> 1 Omega-3 % BMI -0.0187 0.00906 3.90e- 2
#> 2 DHA % BMI 0.0232 0.00913 1.11e- 2
#> 3 Unsaturation BMI -0.120 0.00906 3.21e-40
#> 4 Sphingomyelins BMI 0.0608 0.00938 9.10e-11

Using ggforestplot() it is extremely easy to make a forest plot of the above results:

使用ggForestlot()可以非常容易地将上述结果绘制成森林图:


# Create plot
ggforestplot::forestplot(
df = df,
name = name,
estimate = beta,
se = se,
pvalue = pvalue,
psignif = 0.002
)


Question: how could I add a legend below the plot that explains the significance levels as indicated by the dots in the plot? Compare with the result of another package ggstats and its function ggcoef_model():

问:我如何在剧情下面添加一个图例,来解释剧情中的点所指示的重要性级别?与另一个程序包ggstats及其函数ggcoef_Model()的结果进行比较:


# Load packages
library(ggstats)
# Load example data
data(tips, package = "reshape")
# Run linear model
linear_model <- lm(tip ~ size + total_bill, data = tips)
# Plot model
ggcoef_model(linear_model)


Created on 2023-09-10 with reprex v2.0.2

创建于2023-09-10,Reprex v2.0.2


更多回答
优秀答案推荐


As with many ggplot extension packages, what you gain in ease of use of the wrapper function forestplot you lose in the ability to customize your plot, including, as far as I can tell, the ability to easily add a legend that represents the filled aesthetic.

与许多ggploy扩展包一样,包装器函数ForestPlot的易用性使您失去了自定义绘图的能力,据我所知,包括轻松添加代表填充美学的图例的能力。


Fortunately, the package also contains the handy low-level functions geom_stripes and theme_forest to make it easy to produce a ggplot which is almost identical to the original but includes a legend for the p values, and gives you full control of the shapes, line thickness, colors, theme elements etc:

幸运的是,该程序包还包含方便的低级函数geom_stripe和heme_lin,以便轻松生成与原始文件几乎相同但包含p值图例的gggraph,并使您可以完全控制形状、线条粗细、颜色、主题元素等:


library(ggplot2)

ggplot(df, aes(x = beta, y = name)) +
ggforestplot::geom_stripes() +
geom_vline(xintercept = 0) +
geom_linerange(aes(xmin = beta - qnorm(0.99) * se,
xmax = beta + qnorm(0.99) * se)) +
geom_point(aes(shape = pvalue < 0.02), fill = 'white', size = 3) +
scale_shape_manual(NULL, values = c(21, 16),
labels = c('Not significant', 'Significant')) +
ggforestplot::theme_forest() +
theme(legend.position = 'bottom')

enter image description here


更多回答

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com