5
votes

I have this simple data frame holding three replicates (value) for each factor (CT). I would like to plot it as geom_point and than the means of the point as geom_line.

gene <- c("Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5")
value <- c(0.86443, 0.79032, 0.86517, 0.79782, 0.79439, 0.89221, 0.93071, 0.87170, 0.86488, 0.91133, 0.87202, 0.84028, 0.83242, 0.74016, 0.86656)
CT <- c("ET","ET","ET", "HP","HP","HP","HT","HT","HT", "LT","LT","LT","P","P","P")
df<- cbind(gene,value,CT)
df<- data.frame(df)

So, I can make the scatter plot.

ggplot(df, aes(x=CT, y=value)) + geom_point()

enter image description here

How do I get a geom_line representing the means for each factor. I have tried the stat_summary:

ggplot(df, aes(x=CT, y=value)) + geom_point() +
stat_summary(aes(y = value,group = CT), fun.y=mean, colour="red", geom="line")

But it does not work. "geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?"

But each group has three observations, what is wrong?

Ps. I am also interested in a smooth line.

2
ggplot(df, aes(x=CT, y=value, group=1)) + geom_smooth(method="loess") + geom_point() perhaps?hrbrmstr
Thanks, very simple:-)Toke Duce Krogager
Does this answer your question? ggplot2: line connecting the means of grouped datacamille

2 Answers

17
votes

You should set the group aes to 1:

ggplot(df, aes(x=CT, y=value)) + geom_point() +
  stat_summary(aes(y = value,group=1), fun.y=mean, colour="red", geom="line",group=1)

enter image description here

0
votes

You can use the dplyr package to get the means of each factor.

library(dplyr)
group_means <- df %>%
  group_by(CT) %>%
  summarise(mean = mean(value))

Then you will need to convert the factors to numeric to let you plot lines on the graph using the geom_segment function. In addition, the scale_x_continuous function will let you set the labels for the x axis.

ggplot(df, aes(x=as.numeric(CT), y=value)) + geom_point() + 
  geom_segment(aes(x=as.numeric(CT)-0.4, xend=as.numeric(CT)+0.4, y=mean, yend=mean), 
               data=group_means, colour="red") +
  scale_x_continuous("name", labels=as.character(df$CT), breaks=as.numeric(df$CT))

Following on from hrbrmstr's comment you can add the smooth line using the following:

ggplot(df, aes(x=as.numeric(CT), y=value, group=1)) + geom_point() + 
  geom_segment(aes(x=as.numeric(CT)-0.4, xend=as.numeric(CT)+0.4, y=mean, yend=mean), 
               data=group_means, colour="red") +
  scale_x_continuous("name", labels=as.character(df$CT), breaks=as.numeric(df$CT)) + 
  geom_smooth()