Here there,
I would like to create a usual ggplot2 with 2 variables x, y and a grouping factor z.
On top of the plot I would like a mean and an interval for each grouping level (so for both x and y).
I can do that using dplyr and multiple call to ggplot functions (see below) but I find the code rather ugly and I wonder if one cannot use cleverly one of the stat_ functions (or something else) to get the same thing in 3 lines of code without having to do the dplyr steps by hand.
library(ggplot2)
library(dplyr)
iris %>%
group_by(Species) %>%
summarize(mean_Petal.Length = mean(Petal.Length),
mean_Sepal.Length = mean(Sepal.Length),
se_Petal.Length = sd(Petal.Length)/sqrt(length(Petal.Length)),
se_Sepal.Length = sd(Sepal.Length)/sqrt(length(Sepal.Length))) %>%
ggplot(aes(y = mean_Petal.Length, x = mean_Sepal.Length, colour = Species)) +
geom_point() +
geom_errorbarh(aes(xmax = mean_Sepal.Length + 2*se_Sepal.Length,
xmin = mean_Sepal.Length - 2*se_Sepal.Length),
height = 0.1) +
geom_errorbar(aes(ymax = mean_Petal.Length + 2*se_Petal.Length,
ymin = mean_Petal.Length - 2*se_Petal.Length),
width = 0.05) +
geom_point(data = iris, aes(y = Petal.Length, x = Sepal.Length, colour = Species),
shape = 1) +
theme_classic() +
labs(y = "Petal Length", x = "Sepal Length")
Many thanks,
Alex
PS: hopefully the solution would also ensure that vertical and horizontal flat arrow heads have same length without having to make the code even uglier.