r - `geom_line()` connects points mapped to different groups -
i'd group
data based on interaction of 2 variables, map aesthetic 1 of variables. (the other variable represents replicates should, in theory, equivalent each other). can find inelegant ways this, seems there ought more elegant way it.
for example
# data frame 2 continuous variables , 2 factors set.seed(0) x <- rep(1:10, 4) y <- c(rep(1:10, 2)+rnorm(20)/5, rep(6:15, 2) + rnorm(20)/5) treatment <- gl(2, 20, 40, labels=letters[1:2]) replicate <- gl(2, 10, 40) d <- data.frame(x=x, y=y, treatment=treatment, replicate=replicate) ggplot(d, aes(x=x, y=y, colour=treatment, shape=replicate)) + geom_point() + geom_line()
this gets right, except don't want represent points different shapes. seems group=interaction(treatment, replicate)
(e.g based on this question, geom_line()
still connects points in different groups:
ggplot(d, aes(x=x, y=y, colour=treatment, group=interaction("treatment", "replicate"))) + geom_point() + geom_line()
i can solve problem manually creating interaction column , group
ing that:
d$interact <- interaction(d$replicate, d$treatment) ggplot(d, aes(x=x, y=y, colour=treatment, group=interact)) + geom_point() + geom_line()
but seems there ought more ggplot2
-native way of getting geom_line
connect points same group.
your code works if following. think had problem because aes
treated "treat"
, "replicate"
vectors, equivalent group = 1
.
ggplot(d, aes(x=x, y=y, colour=treatment, group=interaction(treatment, replicate))) + geom_point() + geom_line()
Comments
Post a Comment