当数据是 tapply 的函数时如何在 R 中使用 ggplot2
How to use ggplot2 in R when the data is a function of tapply
我有一个变量是 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 函数,每次我尝试使用它时都会卡住。将它保存为一个带有一串值的新变量并在普通的 barplot 或 ggplot2 中使用它是非常繁琐的。任何帮助将不胜感激。
(我是 gglopt2 的新手)
谢谢!
一种可能是将您的 table 输出转换为 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")
我有一个变量是 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 函数,每次我尝试使用它时都会卡住。将它保存为一个带有一串值的新变量并在普通的 barplot 或 ggplot2 中使用它是非常繁琐的。任何帮助将不胜感激。 (我是 gglopt2 的新手)
谢谢!
一种可能是将您的 table 输出转换为 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")