This is my first time using this platform. I hope I am doing this right. I am trying to add a group mean on top of individual data in a standard geom_point() graph from two groups. I got that part covered quite fast (see code below). The data I want to show are from two continuous variables.
The problem is that I would like the group mean to represent not only the mean, but also the standard error of the means (from each of the two variables). I thought a "+" sign with different height and width according to each variable would be a good idea, but I don't find anywhere how to change the Height and the Width (separately) of a single point.
So far, the code resemble this:
DataMean <- Data %>%
group_by(Group) %>%
summarise(x = mean(x),
y= mean(y))
ggplot(Data, aes(x = x, y = y, shape = as.factor(Group))) +
geom_point() +
geom_point(data = DataMean, size = 4, shape = 3)
Shape 3 being the "+" sign in R. However, I do not know how to change the height/width of the vertical/horizontal bar of that "+" sign in order to make it correspond to each groups' standard error of the mean. So far, I obtain the graph below.
Thanks and have a great day. Graph
In order to plot a cross with ranges proportional to the data's variability (the standard error of the means), use two geom_linerange
layers with the ranges computed based on the standard errors.
library(dplyr)
library(ggplot2)
DataMean <- Data %>%
group_by(Group) %>%
summarise(across(everything(), list(mean = mean, sd = sd), .names = "{.col}_{.fn}"))
DataMean
## A tibble: 3 x 5
# Group x_mean x_sd y_mean y_sd
# <fct> <dbl> <dbl> <dbl> <dbl>
#1 setosa 1.46 0.174 0.246 0.105
#2 versicolor 4.26 0.470 1.33 0.198
#3 virginica 5.55 0.552 2.03 0.275
ggplot() +
geom_point(data = Data, aes(x, y, group = Group)) +
# vertical bars
geom_linerange(
data = DataMean,
mapping = aes(
x = x_mean,
ymin = y_mean - y_sd, ymax = y_mean + y_sd)) +
# horizontal bars
geom_linerange(
data = DataMean,
mapping = aes(
x = x_mean, xmin = x_mean - x_sd, xmax = x_mean + x_sd,
y = y_mean)
) +
theme_bw()
Data <- iris[3:5]
names(Data) <- c("x", "y", "Group")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.