0

I want to plot two graphs on one figure. In matlab i plot the first graph and then use hold on and then ask it to plot the second graph. This is the code I have written so far. The retrieval of the data is correct. Each list has 6 numbers. I tried using the ''' par''' but i it plots one plot on a graph but overlaps the names.

tempLength <- dim(R_to_C_03M)
tempLength <- tempLength ## gives me the width of R_to_C_03M
tempnames <- colnames(R1)
if (tempLength>2){
  for( i in 2:tempLength){
    lipid1<-as.numeric(R_to_C_03M[1,i]) # Gets the column number of the first lipid
    lipid2<-as.numeric(R_to_C_03M[2,i]) # Gets the column number of the     seccond lipid
    list1<- R1_table[,lipid1]           #Retrives the data from the colum.
    list2<- R1_table[,lipid2]
    list3<- C1_table[,lipid1]
    list4<- C1_table[,lipid2]
    plot(lipid1,lipid2,xlab = tempnames[lipid1], ylab =tempnames[lipid2])
    par(new=TRUE)
    plot(list3,list4)

  }
}

I want two plots to be displayed on one graph, using the same axis if possible. First plot being list2 against list1, and the second being list4 against list3. The x axis values for the different graphs are different however i want to the axis to be the same, other questions and answers have used the same x axis values

Solution:

if (tempLength>2){
  for( i in 2:tempLength){
    lipid1<-as.numeric(R_to_C_03M[1,i]) 
    lipid2<-as.numeric(R_to_C_03M[2,i])
    list1<- R1_table[,lipid1]           #Retrives the data from the colum.
    list2<- R1_table[,lipid2]
    list3<- C1_table[,lipid1]
    list4<- C1_table[,lipid2]
    lipid1name <- tempnames[lipid1]
    lipid2name <-tempnames[lipid2]
    plot(list1,list2,xlab = lipid1name, ylab =lipid2name)
    par(new=TRUE)
    plot(list3,list4,xlab='', ylab='',axes=FALSE)
    par(new=FALSE)
  }
}
Nima Hojat
  • 11
  • 3
  • 3
    Possible duplicate of [Plot two graphs in same plot in R](https://stackoverflow.com/questions/2564258/plot-two-graphs-in-same-plot-in-r) – AkselA Aug 28 '19 at 10:52
  • Thank you, but "Plot two graphs in same plot in R" uses the same x axis values for both graphs, i have different x axis values – Nima Hojat Aug 28 '19 at 10:56
  • It's more common to have two y-axis, and you can easily adapt a solution for that to work with to x-axis, eg: https://stackoverflow.com/questions/6142944/how-can-i-plot-with-2-different-y-axes – AkselA Aug 28 '19 at 11:00
  • Here's an example of putting the two axis on the same side, it that's what you want: https://stackoverflow.com/questions/8443820/r-multiple-x-axis-with-annotations – AkselA Aug 28 '19 at 11:04

0 Answers0