Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shapes on line graph using stat_summary

Tags:

r

ggplot2

I'm sure the answer is very simple but at the moment it eludes me. I want to make a line graph using stat_summary(), with different shapes for each group (representing an experimental condition) at each x-axis tick (representing a separate time point).

Here's the data

set.seed(124)
ID <- rep(1:12, times = 3)
Group <- rep(c("A", "B", "C"), times = 12)
score <- rnorm(36, 25, 3)
session <- rep(c("s1","s2", "s3"), each = 12)

df <- data.frame(ID, Group, session, score)

Now I can get there by making a table of means for each time point. Like so.

gMeans <- aggregate(score ~ session + Group, data = df, mean)

And then graphing it like so.

pMeans <- ggplot(data = gMeans, aes(x = session, y = score, group = Group, shape = Group)) +
  geom_line(aes(linetype = Group), size = 1) +
  geom_point(size = 5, fill = "white") +
  scale_color_hue(name = "Group", l = 30) +
  scale_shape_manual(name = "Group", values = c(23,22, 21)) +
  scale_linetype_discrete(name = "Group") +
  theme_bw() 

pMeans

However I would like to be able to skip the step of having to make the table of means by using stat_summary(). I can get a similar graph with different line types, but I can't work out how to get the different shapes on each axis tick for each group. I tried the code below and many different permutations of geom_point() and geom_line(), but to no avail. How do I alter the code below to get output that looks like the output derived from the code above?

pline <- ggplot(df, aes(x=session, y=score, group = Group, shape = Group)) +
                stat_summary(fun.y="mean", geom="line", size=1.1, aes(linetype=Group, shape = Group)) +
                scale_shape_manual(values=c(1:3)) 

pline
like image 560
llewmills Avatar asked Sep 11 '25 03:09

llewmills


1 Answers

This should help and also clean up the legend:

library(ggplot2)

set.seed(124)
ID <- rep(1:12, times = 3)
Group <- rep(c("A", "B", "C"), times = 12)
score <- rnorm(36, 25, 3)
session <- rep(c("s1","s2", "s3"), each = 12)

df <- data.frame(ID, Group, session, score)

gg <- ggplot(df, aes(x=session, y=score, group = Group, shape = Group))
gg <- gg + stat_summary(fun.y="mean", geom="line", size=1.1, 
                        aes(linetype = Group), show.legend=FALSE)
gg <- gg + stat_summary(fun.y="mean", geom="point", size=5, 
                        aes(shape = Group), fill="white")
gg <- gg + scale_shape_manual(name = "Group", values = c(23, 22, 21))
gg <- gg + theme_bw() 
gg <- gg + theme(legend.key=element_blank())
gg

The lines were obscured, so it makes little sense to keep them in the legend. Since you used stat_summary() for the line (vs geom_line() with an embedded stat="summary" it's best to keep the idiom for the point geom as well IMO).

enter image description here

like image 174
hrbrmstr Avatar answered Sep 13 '25 18:09

hrbrmstr