[英]How can I plot a figure with numerical data of confidence intervals and means, comparing different samples per categories, in R?
我想知道是否可以使用R自動繪制這樣的圖形:
我必須說以前的圖像是使用Inkscape手動完成的。 但是,我想知道是否可以使用R自動構建此類圖像。這將節省大量時間。
我有原始數據:
1 10.73 9.31 10.75 7.72 5.98 7.97
1 9.27 8.61 9.28 7.00 6.25 8.50
1 8.49 7.80 8.50 6.26 6.54 9.11
2 11.45 10.00 11.46 8.47 6.58 8.86
2 10.76 10.00 10.03 7.75 6.88 9.45
2 10.00 9.34 9.25 7.00 7.19 10.12
第一列是類別(在這種情況下,只有兩個類別1和2)。 每個類別的上一行是較高的95%CL,每個類別的下一行是較低的95%CL。 其他行是平均值。
在這種情況下,我有6個不同的示例,這些示例顯示在category列之后的6列中。
您可以通過以下方式在ggplot中執行此操作:首先重塑數據,使每一行都是一個置信區間,並為分組數據獲取適當的x軸間距:
plot.dat <- data.frame(x=c(seq(.8, 1.2, length.out=6), seq(1.8, 2.2, length.out=6)),
lb=unlist(c(dat[3,-1], dat[6,-1])),
mid=unlist(c(dat[2,-1], dat[5,-1])),
ub=unlist(c(dat[1,-1], dat[4,-1])))
plot.dat
# x lb mid ub
# 1 0.80 8.49 9.27 10.73
# 2 0.88 7.80 8.61 9.31
# 3 0.96 8.50 9.28 10.75
# 4 1.04 6.26 7.00 7.72
# 5 1.12 6.54 6.25 5.98
# 6 1.20 9.11 8.50 7.97
# 7 1.80 10.00 10.76 11.45
# 8 1.88 9.34 10.00 10.00
# 9 1.96 9.25 10.03 11.46
# 10 2.04 7.00 7.75 8.47
# 11 2.12 7.19 6.88 6.58
# 12 2.20 10.12 9.45 8.86
然后,您可以使用geom_rect
和geom_segment
進行geom_segment
以獲取形狀:
library(ggplot2)
ggplot(plot.dat, aes(xmin=x-.03, xmax=x+.03, ymin=lb, ymax=ub)) +
geom_rect(fill=NA, color="black") +
geom_segment(aes(x=x-.03, xend=x+.03, y=mid, yend=mid)) +
theme_bw() +
scale_x_continuous(breaks=c(1, 2)) +
ylim(0, max(plot.dat$ub)) +
xlab("Group") +
ylab("Value")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.