Here a solution in the base package. But I am pretty sure that this is a job for reshape
. Here a solution using subsetting by column and to create 2 data.frames and rbind
to aggregate the data.frame.
data <- do.call(rbind,lapply(1:2,function(i)
{
res <- data.frame(dat[,paste0(c('x','y'),i)],group=i)
setNames(res,nm=c('x','y','group'))
}))
rbind(head(data,3),tail(data,3))
x y group
1 0.745779796 0.57283280 1
2 0.008949224 0.83182620 1
3 2.390108913 0.80419990 1
41 1.523942750 0.96091747 2
51 0.005469770 0.03604669 2
61 0.082317780 1.29889103 2
Then I plot it using geom_line
like this :
ggplot(data)+
geom_line(aes(x=x,y=y,col=factor(group),group=group))
EDIT solution with reshape
dat.reshape <- reshape(dat, direction="long", varying=list(c(1, 3), c(2, 4)),
sep="", v.names=c("x", "y"))
ggplot(dat.reshape)+
geom_line(aes(x=x,y=y,col=factor(time),group=time))+
scale_color_discrete(name ="Line Type",
breaks=c(1, 2),
labels=c("Woman", "Man"))