Jean_N
Jean_N

Reputation: 529

How to add means to a ggplot + geom_point plot

I have 10 groups of data points and I am trying to add the mean to for each group to be displayed on the plot (e.g. by a different symbol such as a big triangle or a star or something similar). Here is a reproducible example

library(ggplot2)
library(reshape2)
set.seed(1234)

x <- matrix(rnorm(100),10,10)
varnames <- paste("var", seq(1,10))

df <- data.frame(x)
colnames(df) <- varnames
melt(df)

ggplot(data = melt(df)) + geom_point(mapping = aes(x = variable, y = value))
mymeans <- colMeans(df)

Basically I now want to have the values in mymeans plotted in their respective variable location, would anybody have an idea how to quickly do this?

Upvotes: 11

Views: 37828

Answers (5)

John
John

Reputation: 23768

An alternative not mentioned yet is...

ggplot(df,  aes(x = variable, y = value)) + 
  geom_point() +
  geom_point(stat = 'summary', fun = 'mean',)

One can set any feature of the second point like size shape and colour to differentiate it they wish. It's equivalent to the stat_summary version. In selecting you decide where you want your code to highlight that you are adding an additional geom or that you're adding a summary. It's a matter of emphasis.

Upvotes: 1

phargart
phargart

Reputation: 729

Updated code to reflect changes in tidyverse from previous comments.

As tidyverse has updated its syntax, below is the updated versions for dplyr and ggplot2. Thank you, @Vincent Bonhomme and @markus.

For reproducibility, I will copy their examples.

library(tidyverse)

# Dataset Generation
set.seed(1234)
df <- replicate(10, rnorm(10)) %>%
  as_data_frame() %>%
  pivot_longer(cols = everything(), names_to = "variable", values_to = "value") %>% # ** Change here   
  mutate(group = as.factor(rep(1:5, 20)))

#Option 1: Use stat_summary() for a cleaner version (@Vincent Bonhomme)
ggplot(df,  aes(x = variable, y = value)) + 
  geom_point() +
  stat_summary(
    fun = "mean",        #argument updated in new version.
    geom = "point",
    col = "black",
    size = 3,
    shape = 24,
    fill = "red"
  ) + 
ggtitle("Example")


#Option 2 -- Creating a means dataset (@ markus)
df_means <- df %>% group_by(variable) %>% summarise(value=mean(value))
ggplot(data = df) + 
  aes(x = variable, y = value) +
  geom_point() + 
  geom_point(data=df_means, 
col="red",  
size = 3,
    shape = 24,
    fill = "red") +
  ggtitle("Example")

Both create the same graph

enter image description here

Here are the versions used

dplyr       * 1.0.3 
ggplot2     * 3.3.3 

Upvotes: 2

markus
markus

Reputation: 26373

Or we can use stat_summary

ggplot(data = reshape2::melt(df), aes(x = variable, y = value)) + 
  geom_point() +
  stat_summary(
    geom = "point",
    fun.y = "mean",
    col = "black",
    size = 3,
    shape = 24,
    fill = "red"
  )

enter image description here


An overview about possible shapes can be found here: www.cookbook-r.com

Upvotes: 25

Make42
Make42

Reputation: 13118

Instead of using two different frames, I find it often cleaner to bring all the data together.

library(ggplot2)
library(tidyr)
library(dplyr)

set.seed(1234)

x <- matrix(rnorm(100),10,10)
varnames <- paste("var", seq(1,10))

df <- data.frame(x)
colnames(df) <- varnames

melt_data = df %>% gather
mymeans = melt_data %>% group_by(key) %>% summarize(value = mean(value))
mymeans$type = 'mean'
melt_data$type = 'points'

ggplot(data = bind_rows(melt_data, mymeans)) +
  geom_point(mapping = aes(x = key, y = value, color=type))

Upvotes: 0

Vincent Bonhomme
Vincent Bonhomme

Reputation: 7453

You can pass another geom_point with another data.frame:

Try the following:

df_means <- melt(summarise_all(df, mean))
ggplot(data = melt(df)) + 
    geom_point(mapping = aes(x = variable, y = value)) + 
    geom_point(data=df_means,  mapping=aes(x = variable, y = value), col="red")

enter image description here

I shtat what you were looking for?


By the way a more compact/modern/tidyversy way would be:

library(tidyverse)
set.seed(1234)

df <- replicate(10, rnorm(10)) %>% as_data_frame() %>% gather()
df_means <- df %>% group_by(key) %>% summarise(value=mean(value))

ggplot(data = df) + 
   aes(x = key, y = value) +
   geom_point() + 
   geom_point(data=df_means, col="red")

Upvotes: 3

Related Questions