Without a reproducible example, I can't be sure what you're trying to do, but here's a generic example of how you can use stat_summary
to calculate and plot means of your data within ggplot itself. The example below also includes 95% confidence intervals.
library(tidyverse)
ggplot(iris %>% gather(key, value, -Species), aes(Species, value)) +
stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.1, colour="red") +
stat_summary(fun.y=mean, geom="point", colour="blue") +
facet_grid(. ~ key) +
theme_classic()
If you want to calculate means outside of ggplot, here's a tidyverse
approach:
iris %>%
gather(Variable, value, -Species) %>%
group_by(Species, Variable) %>%
summarise(N=n(),
mean=mean(value))
Species Variable N mean
1 setosa Petal.Length 50 1.462
2 setosa Petal.Width 50 0.246
3 setosa Sepal.Length 50 5.006
4 setosa Sepal.Width 50 3.428
5 versicolor Petal.Length 50 4.260
6 versicolor Petal.Width 50 1.326
7 versicolor Sepal.Length 50 5.936
8 versicolor Sepal.Width 50 2.770
9 virginica Petal.Length 50 5.552
10 virginica Petal.Width 50 2.026
11 virginica Sepal.Length 50 6.588
12 virginica Sepal.Width 50 2.974
Using aggregate
, you could do:
aggregate(. ~ Species, data=iris, mean)
Species Sepal.Length Sepal.Width Petal.Length Petal.Width
1 setosa 5.006 3.428 1.462 0.246
2 versicolor 5.936 2.770 4.260 1.326
3 virginica 6.588 2.974 5.552 2.026