r - 当数据是tapply的函数时如何在R中使用ggplot2

标签 r ggplot2 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函数,所以每次尝试使用它时我都会陷入困境。将其保存为具有字符串值的新变量并在普通条形图或 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")

enter image description here

关于r - 当数据是tapply的函数时如何在R中使用ggplot2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35979592/

相关文章:

r - 在 R 中使用 flexsurvreg 预测样本外

r - 启动时发生严重错误 R ... ."jvm.dll"丢失!无法再使用库

r - 如何使用 ggplot2 的 geom_dotplot() 与对称间隔和分隔的点

使用 apply 函数对排序后的数据集进行排名

r - 错误: stat_count() must not be used with a y aesthetic

mysql - RMySQL、dbWriteTable 和包含换行符的文本字段

r ggplot geom_bar分组位置

r - 在 ggplot 代码中使用 ifelse() 条件时出错

r - 如何将 na.rm 作为参数传递给 tapply?

R:有没有一种简单的方法可以使 ave() 对矩阵起作用?