r - 隔离具有显着 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 值。有没有办法可以只提取那些 ap 值小于 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)
推荐阅读
- java - 为什么 IntelliJ 将注释标头放在错误的位置……仅用于测试?
- javascript - 如何在 Highcharter 中使用 JavaScript?
- javascript - 为什么使用 React Hooks、MomentJS 对象和间隔/超时更新状态不会触发重新渲染?
- mustache - 如何修复以'm_-'开头的随机字符串,在用小胡子渲染模板后附加到类
- android - 我可以在 Android 的混合应用程序中(通过 Cordova)使用 navigator.mediaDevices.getUserMedia 来使用相机吗?
- c++ - 如何使用 llvm 框架执行函数参数寄存器分配?
- powerbi - 获取所有设备,无论是否有最后借用它的用户
- c# - 优化复杂对象比较
- angular - Highcharts不显示数据
- java - 在 main() 方法中通过子对象调用覆盖的方法