grouping by two factors (geom_line)

10,669 views
Skip to first unread message

Christian Rummey

unread,
May 23, 2012, 11:21:08 AM5/23/12
to ggp...@googlegroups.com

dear all!

I have data grouped by two factors, let's say gear and carb (see below).
To connect the points per group, I can either use

group=carb
OR
group=gear

But what I need is something like group=c(carb + gear) to connect only the dots that have both factors in common.

I could create a new column, but there must be another way to do this...
Many thanks
- Christian

mtcars$gear = factor(mtcars$gear)
mtcars$carb = factor(mtcars$carb)

ggplot(mtcars) +
aes(hp,disp)+
aes(shape = gear) +
aes(color = carb) +
aes(group=carb) +
geom_point()+
geom_line()


Winston Chang

unread,
May 23, 2012, 12:31:49 PM5/23/12
to Christian Rummey, ggp...@googlegroups.com
You can use interaction(), like this:

mtcars$gear = factor(mtcars$gear)
mtcars$carb = factor(mtcars$carb)

ggplot(mtcars, aes(x=hp, y=disp, shape=gear, colour=carb)) +
    geom_point() + 
    geom_line(aes(group=interaction(carb,gear)))

-Winston




--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Christian Rummey

unread,
May 23, 2012, 3:36:20 PM5/23/12
to ggp...@googlegroups.com, Christian Rummey

Winston - that did the trick.
thanks a lot!
Christian

Tushar Garg

unread,
May 22, 2018, 4:29:46 PM5/22/18
to ggplot2

How can I give some name to a combination of factors.

Thanks
Tushar
Reply all
Reply to author
Forward
0 new messages