我有一个变量,它是tapply 的函数
meanx <- with(dat2, tapply(x, list(type,status), mean))
The out put reads as follows :
Status 1 Status2
Type 1 11.99 9.8
Type 2 88.8 100
我也有 4 种均值的置信区间
xCI <- c(meanx - qnorm(0.975, mean=0, sd=1)*serrorx,
meanx + qnorm(0.975, mean=0, sd=1)*serrorx)
我想绘制一个简单的 gglpot2 条形图,并带有置信区间。由于meanx是一个tapply函数,所以每次尝试使用它时我都会陷入困境。将其保存为具有字符串值的新变量并在普通条形图或 ggplot2 中使用它是非常乏味的。任何帮助将不胜感激。 (我是 gglopt2 的新手)
谢谢!
最佳答案
一种可能性是将表输出转换为 data.frame,并将其传递给 ggplot。另一种可能更简单的替代方法是使用 stat_summary 函数来计算 ggplot 框架中的摘要统计信息。
## Sample data
dat2 <- data.frame(
type=paste("Type ", sample(1:2, 50, TRUE)),
status=paste("Status ", sample(1:2, 50, TRUE)),
x = runif(50)
)
library(ggplot2)
ggplot(dat2, aes(interaction(type, status, sep = "/"), x)) +
stat_summary(geom="bar", fun.y=mean, fill="lightblue") +
stat_summary(geom="errorbar", fun.data=mean_cl_boot, color="red", width=0.4) +
theme_bw() +
scale_x_discrete("type-status")
关于r - 当数据是tapply的函数时如何在R中使用ggplot2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35979592/