Overlaying two graphs using ggplot2 in R

后端 未结 2 1111
长发绾君心
长发绾君心 2020-12-05 10:25

I have two graphs and I am trying to overlay one on top of the other:

An example of the data frame \"ge\" looks like this. In actuality there are 10 Genes with 200

相关标签:
2条回答
  • 2020-12-05 10:49

    One way is to add the geom_line command for the second plot to the first plot. You need to tell ggplot that this geom is based on a different data set:

    ggplot(avg, aes(x=Gene, y=mean)) + 
      geom_point() + 
      geom_line() + 
      geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width=.1) +
      geom_line(data = ge, aes(x=Gene, y=Exp, group=Sample, colour="#000099"),
                show_guide = FALSE)
    

    The last geom_line command is for creating the lines based on the raw data. enter image description here

    0 讨论(0)
  • 2020-12-05 10:59

    The workaround which I found was that instead of merging the two plots, I merged the data. I added an additional column at the end of the two dataframes and then performed the rbind operation on them.The using either the fill or color aesthetics to separate the two plots. Of course in my case the scale used for the axis were to be the same.

    0 讨论(0)
提交回复
热议问题