Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
497 views
in Technique[技术] by (71.8m points)

r - ggplot2: line connecting the means of grouped data

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...