如何解决隔离具有显着p值的图
对于初学者来说,数据来自us_contagious_diseases数据集,而软件包是tidyverse和ggpubr
library(dslabs)
library(ggpubr)
library(tidyverse)
data("us_contagious_diseases")
我通过以下代码修改了此数据集:
sdf <- us_contagious_diseases %>% filter(.,disease == 'Rubella' | disease == 'Mumps') %>% transmute(.,disease,count,population,state)
然后,我创建了一个箱形图,比较每个州的风疹和腮腺炎病例数:
sdf_plot <- ggplot(sdf,mapping = aes(x = disease,y = count)) + geom_boxplot(outlier.shape = NA) + facet_wrap('state',scales = 'free') + stat_compare_means(method = 't.test',label.y.npc = 0.8)
问题是,此图中有51个图!!!可以将其包含在我的报告中很大。更重要的是,其中许多比较没有明显的p值。我有办法只提取那些p值小于0.01的图吗?
解决方法
我想您需要预先计算p值:
library(broom)
res = sdf %>% group_by(state) %>% do(tidy(t.test(count~disease,data=.)))
head(res)
# A tibble: 6 x 11
# Groups: state [6]
state estimate estimate1 estimate2 statistic p.value parameter conf.low
<fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Alab… 125. 180. 55.4 2.46 0.0181 42.7 22.4
2 Alas… 66.1 104. 38.2 1.52 0.136 45.4 -21.7
3 Ariz… 78.6 266. 187. 0.657 0.513 68.0 -160.
4 Arka… 84.3 113. 28.4 2.87 0.00628 45.5 25.1
5 Cali… 386. 1915. 1529. 0.540 0.592 59.3 -1046.
6 Colo… 95.0 314. 219. 0.762 0.449 62.6 -154.
keep = res$state[res$p.value<0.01]
[1] Arkansas District Of Columbia Georgia
[4] Kansas Maryland Nevada
[7] Ohio
然后使用此过滤器进行绘图:
sdf_plot <- ggplot(subset(sdf,state %in% keep),aes(x = disease,y = count)) +
geom_boxplot(outlier.shape = NA) +
facet_wrap('state',scales = 'free') +
stat_compare_means(method = 't.test',label.y.npc = 0.8)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。