[英]Proper display of confidence interval in R using ggplot
我正在嘗試繪制一個圖表,以表示按年齡類別分組的不同葯物的不同不良反應的2個測量值(prr和ebgm),如下所示:
library(ggplot2)
strata <- factor(c("Neonates", "Infants", "Children", "Adolescents", "Pediatrics"), levels=c("Neonates", "Infants", "Children", "Adolescents", "Pediatrics"), order=T)
Data <- data.frame(
strata = sample(strata, 200, replace=T),
drug=sample(c("ibuprofen", "clarithromycin", "fluticasone"), 200, replace=T), #20 de medicamente
reaction=sample(c("Liver Injury", "Sepsis", "Acute renal failure", "Anaphylaxis"), 200, replace=T),
measurement=sample(c("prr", "EBGM"), 200, replace=T),
value_measurement=sample(runif(16), 200, replace=T),
lower_CI=sample(runif(6), 200, replace=T),
upper_CI=sample(runif(5), 200, replace=T)
)
g <- ggplot(Data, aes(x=strata, y=value_measurement, fill=measurement, group=measurement))+
geom_histogram(stat="identity", position="dodge")+
facet_wrap(~reaction)+
geom_errorbar(aes(x=strata, ymax=upper_CI, ymin=lower_CI), position="dodge", stat="identity")
ggsave(file="meh.png", plot=g)
CI的上限和下限是測量的置信區間限制。 假設我對每個測量都有一個置信區間,我希望適當的直方圖具有相應的置信區間,但是得到的是s。
有什么想法如何正確放置那些令人討厭的conf間隔嗎? 謝謝!
稍后編輯:在給定葯物的原始數據中,我有很多行,每行包含不良反應,年齡類別,並且這些類別中的每一個都有2個度量值:prr或EBGM以及相應的置信區間。 這沒有反映在數據模擬中。
問題在於您的每個柱形圖實際上都是多個相互重疊的柱形圖,因為對於reaction
, strata
和measurement
每種組合,您都有多於一行的數據。 (由於相同的原因,您將獲得多個錯誤條。)
您可以在下面的代碼中看到這一點,在這里我將geom_histogram
更改為geom_bar
並添加了alpha=0.3
和colour="grey40"
來顯示多個重疊的條。 我也注釋掉了錯誤欄。
ggplot(Data, aes(x=strata, y=value_measurement, fill=measurement, group=measurement)) +
geom_bar(stat="identity", position="dodge", alpha=0.3, colour="grey40") +
facet_wrap(~reaction) #+
# geom_errorbar(aes(x=strata, ymax=upper_CI, ymin=lower_CI),
# position="dodge", stat="identity")
您可以通過在數據中添加另一列來解決此問題,該列會添加分組類別,您可以通過這些類別來分隔這些欄。 例如,在下面的代碼中,我們添加了一個名為count
的新列,該列僅為每個reaction
和strata
組合中的每一行數據分配數字1到n。 我們按measurement
排序,以便將每種度量類型按count
順序保持在一起。
library(dplyr)
Data = Data %>% group_by(reaction, strata) %>%
arrange(measurement) %>%
mutate(count = 1:n())
現在繪制數據:
ggplot(Data, aes(x=strata, y=value_measurement,
fill=measurement, group=count)) +
geom_bar(stat="identity", position=position_dodge(0.7), width=0.6) +
facet_wrap(~reaction, ncol=1) +
geom_errorbar(aes(x=strata, ymax=upper_CI, ymin=lower_CI, group=count),
position=position_dodge(0.7), stat="identity", width=0.3)
現在,您可以看到單獨的條以及它們的錯誤條(這很奇怪,但這僅是因為它們是偽數據)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.