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() 

enter image description here

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() 

enter image description here

i can solve problem manually creating interaction column , grouping that:

d$interact <- interaction(d$replicate, d$treatment)  ggplot(d, aes(x=x, y=y, colour=treatment, group=interact)) +    geom_point() + geom_line() 

enter image description here

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

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -