ggprism包數(shù)據(jù)可視化之添加P值

可以說GraphPad Prism最受歡迎的功能之一是在繪圖中添加p值文兑,
ggprism使用add_pvalue()添加帶括號或不帶括號的p值;喜歡的小伙伴可以關(guān)注我的公眾號R語言數(shù)據(jù)分析指南持續(xù)分析更多優(yōu)質(zhì)資源

library(tidyverse)
library(ggprism)
library(patchwork)
library(magrittr)
p <- ggplot(sleep, aes(x = group, y = extra)) +
  geom_jitter(aes(shape = group), width = 0.1) + 
  stat_summary(geom = "crossbar", fun = mean, 
colour = "red", width = 0.2) + 
  theme_prism() + 
  theme(legend.position = "none")
p

接下來心傀,我們將執(zhí)行t檢驗,并為兩個均值之差獲得p值

result <- t.test(extra ~ group,data = sleep)$p.value %>% 
  signif(digits = 3)

result
# [1] 0.0794

現(xiàn)在绑谣,我們將構(gòu)造一個p值data.frame供add_pvalue()使用

df_p_val <- data.frame(
  group1 = "1",
  group2 = "2",
  label = result,
  y.position = 6
)

有括號版

p1 <- p + add_pvalue(df_p_val,
                     xmin = "group2",
                     xmax = "group1",
                     label = "label",
                     y.position = "y.position")

p1

無括號版

p2 <- p + add_pvalue(df_p_val, label = "p = {label}", 
                     remove.bracket = TRUE, x = 1)
p2

ToothGrowth數(shù)據(jù)集示例

p <- ggplot(ToothGrowth, aes(x = factor(dose), y = len)) +
  geom_boxplot(aes(fill = dose), colour = "black") + 
  theme_prism() + 
  theme(legend.position = "none")
p

接下來對數(shù)據(jù)執(zhí)行兩次t檢驗,并將均值dose = 0.5作為參考組進行比較;并對P值進行多重矯正

result1 <- t.test(len ~ dose, 
                  data = subset(ToothGrowth,
dose %in% c(0.5, 1.0)))$p.value

result2 <- t.test(len ~ dose, 
                  data = subset(ToothGrowth, 
dose %in% c(0.5, 2.0)))$p.value

result <- p.adjust(c(result1, result2), method = "BH")

構(gòu)建P值表

df_p_val <- data.frame(
  group1 = c(0.5, 0.5),
  x = c(2, 3),
  label = signif(result, digits = 3),
  y.position = c(35, 35)
)

添加p值

p1 <- p + add_pvalue(df_p_val, 
               xmin = "group1", 
               x = "x", 
               label = "label",
               y.position = "y.position")

p2 <- p + add_pvalue(df_p_val, 
               xmin = "group1", 
               x = "x", 
               label = "p = {label}",
               y.position = "y.position",
               label.size = 3.2,
               fontface = "bold")

p1 + p2

使用rstatix包計算p值

df_p_val <- rstatix::t_test(ToothGrowth,
len ~ dose, ref.group = "0.5") %>% 
  rstatix::add_xy_position()

p + add_pvalue(df_p_val, 
               label = "p = {p.adj}",
               remove.bracket = TRUE)

繪制誤差線柱狀圖

df_p_val <- rstatix::t_test(ToothGrowth, len ~ supp) %>% 
  rstatix::add_x_position()

p <- ggplot(ToothGrowth, aes(x = factor(supp), y = len)) + 
  stat_summary(geom = "col", fun = mean) + 
  stat_summary(geom = "errorbar", 
               fun = mean,
               fun.min = function(x) mean(x) - sd(x),
               fun.max = function(x) mean(x) + sd(x),
               width = 0.3) + 
  theme_prism() + 
  coord_cartesian(ylim = c(0, 35)) + 
  scale_y_continuous(breaks = seq(0, 35, 5), expand = c(0, 0))

p + add_pvalue(df_p_val, y.position = 30)

將len每個劑量的平均值與dose = 0.5進行比較,誤差線表示與平均值的1個標準偏差

df_p_val <- rstatix::t_test(ToothGrowth, 
len ~ dose, ref.group = "0.5") %>% 
  rstatix::add_xy_position()

p <- ggplot(ToothGrowth, aes(x = factor(dose), y = len)) + 
  stat_summary(geom = "col", fun = mean) + 
  stat_summary(geom = "errorbar", 
               fun = mean,
               fun.min = function(x) mean(x) - sd(x),
               fun.max = function(x) mean(x) + sd(x),
               width = 0.3) + 
  theme_prism() + 
  coord_cartesian(ylim = c(0, 40)) + 
  scale_y_continuous(breaks = seq(0, 40, 5), expand = c(0, 0))

p1 <- p + add_pvalue(df_p_val, label = "p.adj.signif")

p2 <- p + add_pvalue(df_p_val, label = "p.adj.signif",
remove.bracket = TRUE)

p1 + p2

將總體均值len與dose進行比較

df_p_val <- rstatix::t_test(ToothGrowth, len ~ dose,
ref.group = "all") %>% 
  rstatix::add_xy_position()

p <- ggplot(ToothGrowth, aes(x = factor(dose), y = len)) + 
  stat_summary(geom = "col", fun = mean) + 
  stat_summary(geom = "errorbar", 
               fun = mean,
               fun.min = function(x) mean(x) - sd(x),
               fun.max = function(x) mean(x) + sd(x),
               width = 0.3) + 
  theme_prism() + 
  coord_cartesian(ylim = c(0, 40)) + 
  scale_y_continuous(breaks = seq(0, 40, 5), expand = c(0, 0))

p + add_pvalue(df_p_val, label = "p.adj.signif")

多個成對比較

df_p_val <- rstatix::t_test(ToothGrowth, len ~ dose)

p <- ggplot(ToothGrowth, aes(x = factor(dose), y = len)) + 
  geom_violin(trim = FALSE) + 
  geom_boxplot(width = 0.2) +
  theme_prism() + 
  coord_cartesian(ylim = c(0, 45)) + 
  scale_y_continuous(breaks = seq(0, 45, 5), expand = c(0, 0))

p + add_pvalue(df_p_val, 
               y.position = c(44, 41, 44),
               bracket.shorten = c(0.025, 0, 0.025))

分組成對比較

df_p_val <- ToothGrowth %>% 
  rstatix::group_by(supp) %>% 
  rstatix::t_test(len ~ dose) %>% 
  rstatix::add_xy_position()

p <- ggplot(ToothGrowth, aes(x = factor(dose), y = len)) + 
  geom_boxplot(aes(fill = supp)) + 
  theme_prism()

p + add_pvalue(df_p_val, 
               label = "p = {p.adj}",
               colour = "supp",
               fontface = "bold",
               step.group.by = "supp",
               step.increase = 0.1,
               tip.length = 0,
               bracket.colour = "black",
               show.legend = FALSE)
df_p_val1 <- ToothGrowth %>%
  rstatix::group_by(dose) %>%
  rstatix::t_test(len ~ supp) %>%
  rstatix::adjust_pvalue(p.col = "p", method = "bonferroni") %>%
  rstatix::add_significance(p.col = "p.adj") %>% 
  rstatix::add_xy_position(x = "dose", dodge = 0.8) 

df_p_val2 <- rstatix::t_test(ToothGrowth, len ~ dose, 
                             p.adjust.method = "bonferroni") %>% 
  rstatix::add_xy_position()

p <- ggplot(ToothGrowth, aes(x = factor(dose), y = len)) + 
  geom_boxplot(aes(fill = supp)) + 
  theme_prism() + 
  coord_cartesian(ylim = c(0, 45))

p + add_pvalue(df_p_val1, 
               xmin = "xmin", 
               xmax = "xmax",
               label = "p = {p.adj}",
               tip.length = 0) + 
  add_pvalue(df_p_val2,
             label = "p = {p.adj}",
             tip.length = 0.01,
             bracket.nudge.y = 2,
             step.increase = 0.015)

分面

df_p_val <- ToothGrowth %>% 
  rstatix::group_by(dose) %>% 
  rstatix::t_test(len ~ supp) %>% 
  rstatix::add_xy_position()

p <- ggplot(ToothGrowth, aes(x = factor(supp), y = len)) + 
  geom_boxplot(width = 0.2) + 
  facet_wrap(
    ~ dose, scales = "free", 
    labeller = labeller(dose = function(x) paste("dose =", x))
) + 
  theme_prism()

p + add_pvalue(df_p_val)
df_p_val <- ToothGrowth %>% 
  rstatix::group_by(supp) %>% 
  rstatix::t_test(len ~ dose) %>% 
  rstatix::add_xy_position()

p <- ggplot(ToothGrowth, aes(x = factor(dose), y = len)) + 
  geom_boxplot(width = 0.4) + 
  facet_wrap(~ supp, scales = "free") + 
  theme_prism()

p + add_pvalue(df_p_val)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末鳄厌,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子妈踊,更是在濱河造成了極大的恐慌了嚎,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,126評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件响委,死亡現(xiàn)場離奇詭異新思,居然都是意外死亡窖梁,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,254評論 2 382
  • 文/潘曉璐 我一進店門夹囚,熙熙樓的掌柜王于貴愁眉苦臉地迎上來纵刘,“玉大人,你說我怎么就攤上這事荸哟〖侔ィ” “怎么了?”我有些...
    開封第一講書人閱讀 152,445評論 0 341
  • 文/不壞的土叔 我叫張陵鞍历,是天一觀的道長舵抹。 經(jīng)常有香客問我,道長劣砍,這世上最難降的妖魔是什么惧蛹? 我笑而不...
    開封第一講書人閱讀 55,185評論 1 278
  • 正文 為了忘掉前任,我火速辦了婚禮刑枝,結(jié)果婚禮上香嗓,老公的妹妹穿的比我還像新娘。我一直安慰自己装畅,他們只是感情好靠娱,可當我...
    茶點故事閱讀 64,178評論 5 371
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著掠兄,像睡著了一般像云。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上蚂夕,一...
    開封第一講書人閱讀 48,970評論 1 284
  • 那天迅诬,我揣著相機與錄音,去河邊找鬼婿牍。 笑死百框,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的牍汹。 我是一名探鬼主播铐维,決...
    沈念sama閱讀 38,276評論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼慎菲!你這毒婦竟也來了嫁蛇?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 36,927評論 0 259
  • 序言:老撾萬榮一對情侶失蹤露该,失蹤者是張志新(化名)和其女友劉穎睬棚,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,400評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡抑党,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 35,883評論 2 323
  • 正文 我和宋清朗相戀三年包警,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片底靠。...
    茶點故事閱讀 37,997評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡害晦,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出暑中,到底是詐尸還是另有隱情壹瘟,我是刑警寧澤,帶...
    沈念sama閱讀 33,646評論 4 322
  • 正文 年R本政府宣布鳄逾,位于F島的核電站稻轨,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏雕凹。R本人自食惡果不足惜殴俱,卻給世界環(huán)境...
    茶點故事閱讀 39,213評論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望枚抵。 院中可真熱鬧粱挡,春花似錦、人聲如沸俄精。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,204評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽竖慧。三九已至,卻和暖如春逆屡,著一層夾襖步出監(jiān)牢的瞬間圾旨,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,423評論 1 260
  • 我被黑心中介騙來泰國打工魏蔗, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留砍的,地道東北人。 一個月前我還...
    沈念sama閱讀 45,423評論 2 352
  • 正文 我出身青樓莺治,卻偏偏與公主長得像廓鞠,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子谣旁,可洞房花燭夜當晚...
    茶點故事閱讀 42,722評論 2 345

推薦閱讀更多精彩內(nèi)容