R: two scatterplots on single graph using ggplot

前端 未结 2 682
礼貌的吻别
礼貌的吻别 2021-02-07 11:43

Please note I am beginner with R. I have merged two data frames with one common column with merge() method. I have obtained data frame like:

 x   y1   y2
 1   3          


        
2条回答
  •  一生所求
    2021-02-07 12:07

    You should melt your data into long format and then map the colour aesthetic to the variable column from the melted data.frame. Something like this:

    dat <- data.frame(x = c(1,2,1,3), y1 = c(3,2,2,5), y2 = c(5,4,2,5))
    
    dat.m <- melt(dat, id.vars = "x")
    
    ggplot(dat.m, aes(x, value, colour = variable)) +
      geom_point() +
      scale_colour_manual(values = c("red", "blue"))
    

    You can manually set the limits with xlim() and ylim() respectively. It's not clear what you're doing with alpha, so I'll leave that one up to you.

提交回复
热议问题