Search code examples
rggplot2melt

How to organize and plot multiple series of data out of one data frame using ggplot2?


I have a data frame (df1) in the form:

           x1         y1         x2          y2
1 0.745779796  0.5728328 2.04029482 -0.71989471
2 0.008949224  0.8318262 1.01426596  1.20956795
3 2.390108913  0.8041999 1.63621459  0.19979352
4 1.478310218 -0.7179949 1.52394275  0.96091747
5 1.051357060  0.9700232 0.00546977  0.03604669
6 0.123499864  2.0340036 0.08231778  1.29889103

I'm having the hardest time using ggpplot 2 to create a scatter plot that has both series 1 (y1 vs x1) and 2 (y2 vs x2). I've tried to melt the data frame in order to have a "factor" with which to use in aes(), but I'm sure I'm using melt wrong, and can't figure out why:

df<-melt(df,id.var)

My main question is this though: are there any easier ways to organize this data such that, in one ggplot command, I can graph each x-y pair as a separate series on a scatter plot?


Solution

  • 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"))
    

    enter image description here