我想繪製兩個面板的晶格stripplot
中兩個不同條件的個別主題手段。我還想添加我已經計算並存儲在單獨數據框中的主題置信區間。我試圖用latticeExtra的layer
函數覆蓋這些置信區間。當我添加圖層時,如果我在layer
命令中將[subscripts]
添加到x和y中,則兩個面板上都會顯示兩組間隔(如代碼和第一張圖像所示),或者兩組間隔僅顯示在第一個面板上在下面的第二個代碼片段和圖像中說明)。我如何獲得適當的時間間隔以在適當的面板上顯示?晶格中面板的條件層
library(latticeExtra)
raw_data <- data.frame(subject = rep(1:6, 4), cond1 = as.factor(rep(1:2, each = 12)), cond2 = rep(rep(c("A", "B"), each = 6), 2), response = c(2:7, 6:11, 3:8, 7:12))
summary_data <- data.frame(cond1 = as.factor(rep(1:2, each = 2)), cond2 = rep(c("A", "B"), times = 2), mean = aggregate(response ~ cond2 * cond1, raw_data, mean)$response, within_ci = c(0.57, 0.54, 0.6, 0.63))
summary_data$lci <- summary_data$mean - summary_data$within_ci
summary_data$uci <- summary_data$mean + summary_data$within_ci
subject_stripplot <- stripplot(response ~ cond1 | cond2, groups = subject, data = raw_data,
panel = function(x, y, ...) {
panel.stripplot(x, y, type = "b", lty = 2, ...)
panel.average(x, y, fun = mean, lwd = 2, col = "black", ...) # plot line connecting means
}
)
addWithinCI <- layer(panel.segments(x0 = cond1, y0 = lci, x1 = cond1, y1 = uci, subscripts = TRUE), data = summary_data, under = FALSE)
plot(subject_stripplot + addWithinCI)
在兩個面板與兩組間隔的Stripplot:
addWithinCI2 <- layer(panel.segments(x0 = cond1[subscripts], y0 = lci[subscripts], x1 = cond1[subscripts], y1 = uci[subscripts], subscripts = TRUE), data = summary_data, under = FALSE)
plot(subject_stripplot + addWithinCI2)
Stripplot僅與第一面板