Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2: line connecting the means of grouped data

Tags:

I'm trying to group one variable of my data by another and then plot a line through the means. It works fine when both variables are numbers. However, I'm having a problem when the grouping variable is a factor. I have a mock up below.

x <- sample(1:3, 40, replace=T) y <- rnorm(40)  df1 <- data.frame(x, y) qplot(x, y, data=df1) + stat_summary(fun.y=mean, colour="red", geom="line") 

alt text

This is looks great. However if the x variable is a factor I don't get the line.

df2 <- data.frame(x=as.factor(x), y) qplot(x, y, data=df2) + stat_summary(fun.y=mean, colour="red", geom="line") 

alt text

Is there something I can do to get this line displayed?

Ps. geom="point" works but not geom="line"

qplot(x, y, data=df2) + stat_summary(fun.y=mean, colour="red", geom="point") 

alt text

like image 958
Sameer Avatar asked Sep 01 '10 20:09

Sameer


1 Answers

ggplot2 intentionally makes it a little tricky to draw lines across x-axis factors, because you need to make sure that it's meaningful. If your x-axis was "New York", "Philadelphia" and "Boston", it wouldn't be a good idea to draw a line connecting them.

However, assuming that your x variable has a meaningful order, you have to define the group aesthetic to draw the line you want. Here, group = 1.

qplot(x, y, data=df2) +    stat_summary(fun.y=mean, colour="red", geom="line", aes(group = 1)) 

If you just add geom_line() to a plot like this, you will have to define the grouping variable in a similar way.

like image 150
JoFrhwld Avatar answered Oct 15 '22 08:10

JoFrhwld