[英]How do I show individual points of a boxplot in R?
我有 df1:
Name Y_N FIPS score1 score2
1: Alabama 0 1 2633 8
2: Alaska 0 2 382 1
3: Arizona 1 4 2695 41
4: Arkansas 1 5 2039 10
5: California 1 6 27813 524
6: Colorado 0 8 8609 133
7: Connecticut 1 9 5390 111
8: Delaware 0 10 858 3
9: Florida 1 12 14172 215
10: Georgia 1 13 9847 308
11: Hawaii 0 15 720 0
12: Idaho 1 16 845 7
我想進行T檢驗,看是否score1
基於不同Y_N
。 然后我想將這兩個相互作圖。 我制作了一個如下所示的箱線圖:
相反,我希望我的圖表看起來像除了置信條: 我現在想從箱線圖更改為顯示所有單個點的圖,然后是具有 95% 置信區間的平均水平線。 這是怎么做的? 我還想在圖表的一角添加 p 值的文本。
我可能會嘗試:
text(x = max(df1$Y_N)+1,
y = min(df1$score1)+20000,
labels = paste0(
"\np-value = ",
round(coef_lm[2,4],5),
pos = 4)
但我意識到coef_lm[2,4],5
是來自線性模型的檢驗統計量。 如何訪問 t 檢驗的輸出?
我不確定您為什么在代碼中添加了額外的點。 但是在您的原始數據上,您可能會使用ggplot2
和ggpubr
。
立即編輯更像是您的繪畫作品。
ggplot(df1,aes(x = as.factor(Y_N), y = score1)) +
geom_jitter(position = position_jitter(0.1)) +
stat_summary(fun.data = "mean_cl_normal", geom = "errorbar", width = 0.3) +
stat_summary(fun = "mean", geom = "errorbar", aes(ymax = ..y.., ymin = ..y..), col = "red", width = 0.5) +
stat_compare_means(method="t.test") +
xlab("Group") + ylab("Score 1")
原始數據
df1 <- structure(list(Name = structure(1:12, .Label = c("Alabama", "Alaska",
"Arizona", "Arkansas", "California", "Colorado", "Connecticut",
"Delaware", "Florida", "Georgia", "Hawaii", "Idaho"), class = "factor"),
Y_N = c(0L, 0L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 1L),
FIPS = c(1L, 2L, 4L, 5L, 6L, 8L, 9L, 10L, 12L, 13L, 15L,
16L), score1 = c(2633L, 382L, 2695L, 2039L, 27813L, 8609L,
5390L, 858L, 14172L, 9847L, 720L, 845L), score2 = c(8L, 1L,
41L, 10L, 524L, 133L, 111L, 3L, 215L, 308L, 0L, 7L)), class = "data.frame", row.names = c("1:",
"2:", "3:", "4:", "5:", "6:", "7:", "8:", "9:", "10:", "11:",
"12:"))
或者,無需安裝ggpubr
您可以在ggplot2
之外計算 p 值並使用annotate
函數將 pvalue 添加到圖中:
pval <- t.test(score1~Y_N,data = df)$p.value
library(ggplot2)
ggplot(df, aes(x = as.factor(Y_N), y = score1, fill = as.factor(Y_N), color = as.factor(Y_N)))+
geom_boxplot(alpha = 0.3, color = "black", outlier.shape = NA)+
geom_jitter(show.legend = FALSE)+
annotate(geom = "text", label = paste("p.value: ",round(pval,3)), x = 1.5, y = max(df$score1)*0.9)
編輯:沒有箱線圖
作為箱線圖的替代方案,如果您想要單個點和一個代表平均值的條形圖,您可以首先計算 ne 數據集中每組的平均值(這里我使用dplyr
包來做這件事):
library(dplyr)
Mean_df <- df %>% group_by(Y_N) %>% summarise(Mean = mean(score1))
# A tibble: 2 x 2
Y_N Mean
<int> <dbl>
1 0 2640.
2 1 8972.
然后,您可以使用繪制單個點geom_jitter
和平均使用geom_errobar
通過調用新的數據集Mean_df
:
library(ggplot2)
ggplot(df, aes(x = as.factor(Y_N), y = score1))+
geom_jitter(show.legend = FALSE, width = 0.2)+
geom_errorbar(inherit.aes = FALSE, data = Mean_df,
aes(x = as.factor(Y_N),ymin = Mean, ymax = Mean),
color = "red",width = 0.2)+
annotate(geom = "text", label = paste("p.value: ",round(pval,3)),
x = 1.5, y = max(df$score1)*0.9)
可重現的例子
structure(list(Name = c("Alabama", "Alaska", "Arizona", "Arkansas",
"California", "Colorado", "Connecticut", "Delaware", "Florida",
"Georgia", "Hawaii", "Idaho"), Y_N = c(0L, 0L, 1L, 1L, 1L, 0L,
1L, 0L, 1L, 1L, 0L, 1L), FIPS = c(1L, 2L, 4L, 5L, 6L, 8L, 9L,
10L, 12L, 13L, 15L, 16L), score1 = c(2633L, 382L, 2695L, 2039L,
27813L, 8609L, 5390L, 858L, 14172L, 9847L, 720L, 845L), score2 = c(8L,
1L, 41L, 10L, 524L, 133L, 111L, 3L, 215L, 308L, 0L, 7L)), row.names = c(NA,
-12L), class = c("data.table", "data.frame"))
dd <- structure(list(Name = c("Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho"), Y_N = c(0L, 0L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 1L), FIPS = c(1L, 2L, 4L, 5L, 6L, 8L, 9L, 10L, 12L, 13L, 15L, 16L), score1 = c(2633L, 382L, 2695L, 2039L, 27813L, 8609L, 5390L, 858L, 14172L, 9847L, 720L, 845L), score2 = c(8L, 1L, 41L, 10L, 524L, 133L, 111L, 3L, 215L, 308L, 0L, 7L)), row.names = c(NA, -12L), class = c("data.table", "data.frame"))
## frame
boxplot(score1 ~ Y_N, dd, border = NA)
## 95% ci, medians
sp <- split(dd$score1, dd$Y_N)
sapply(seq_along(sp), function(ii) {
x <- sp[[ii]]
arrows(ii, quantile(x, 0.025), ii, quantile(x, 0.975), code = 3, angle = 90, length = 0.1)
segments(ii - 0.05, median(x), ii + 0.05, col = 'red', lwd = 2)
})
points(dd$Y_N + 1, dd$score1, col = dd$Y_N + 1)
## t-test
lbl <- sprintf('p = %s', format.pval(t.test(score1 ~ Y_N, dd)$p.value, digits = 2))
mtext(lbl, at = par('usr')[2], adj = 1)
您的問題之一與如何訪問 t.test 統計數據有關。 這是這個問題的答案。 假設您有這種類型的數據:
set.seed(12)
YN <- sample(0:1, 100, replace = T)
score1 <- sample(500:1500, 100, replace = T)
df <- data.frame(YN, score1)
並進一步假設您像這樣運行和存儲 t.test:
test <- tapply(df$score1, df$YN, t.test)
然后,您可以像這樣一點一點地訪問測試統計信息,此處針對因子級別0
進行了說明:
test$`0`$p.value # p-value
test$`0`$conf.int # confidence interval
test$`0`$estimate # estimate
test$`0`$statistic # statistic
現在顯然你不想一點一點地手動完成,而是以更自動化和系統的方式來完成。 這是您可以實現的方法:
df1 <- do.call(rbind, lapply(test, function(x) c(
statistic = unname(x$statistic),
ci = unname(x$conf.int),
est = unname(x$estimate),
pval = unname(x$p.value))))
輸出是這樣的:
statistic ci1 ci2 est pval
0 22.31155 837.3901 1003.263 920.3265 5.484012e-27
1 22.91558 870.5426 1037.810 954.1765 3.543693e-28
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.