0

I'm fairly new to R and I'm trying to stack three plots over each other to study the diversity of each plot from one another. Being a newbie to R I still haven't managed to find a solution. I've tried the ggplot and multiplot but failed to accomplish the task. Maybe the header in my text file is the problem or I'm not even seeing the problem clearly! It'd be of great help if anyone can advise me in this regard..

My script is as follow:

defects <- read.table(file="C:/_____.txt",header=TRUE)
squareX <- c()        
squareY <- c()        

distance <- c(0, 17.0, 17.5, 34.5, 35.0, 52.0, 52.5, 69.5, 70.0, 87.0,
              87.5, 104.5, 105.0, 122.0, 122.5, 139.5)
square_beginning <- distance[seq(1,length(distance),2)] 

for ( i in 1:length(defects$x)){ 

  for (e in square_beginning){
    if (defects$x[i]>e & defects$x[i]<e+17.5) {
      squareX[i] <- e/17.5+1 
    }
    if (defects$y[i]>e & defects$y[i]<e+17.5) {
      squareY[i] <- e/17.5+1
    }
  }
}

defects<- cbind(defects,squareX,squareY)
#plot (defects)

cont <- read.table(file="C:/____.txt",header=TRUE)
squareX <- c()         
squareY <- c()         

distance <- c(0, 17.0, 17.5, 34.5, 35.0, 52.0, 52.5, 69.5, 70.0, 87.0,
              87.5, 104.5, 105.0, 122.0, 122.5, 139.5)
square_beginning <- distance[seq(1,length(distance),2)] 

for ( i in 1:length(cont$x)){ 

  for (e in square_beginning){  
    if (cont$x[i]>e & cont$x[i]<e+17.5) {
      squareX[i] <- e/17.5+1 
    }
    if (cont$y[i]>e & cont$y[i]<e+17.5) {
      squareY[i] <- e/17.5+1
    }
  }
}
par(mo=c(1,2))
plot(defects, main="test 1")
plot(cont, main="test 2")
Backlin
  • 14,612
  • 2
  • 49
  • 81
Ram.V
  • 3
  • 7
  • Welcome to SO! It's good that you provided your script but unfortunately it's not reproducible (e.g. "defects" is missing). See [how to make a reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for some info. If you make it reproducible, you are more likely to get good and quick answers to your question. – talat Mar 25 '15 at 12:27

2 Answers2

1

You can stack plots setting the mfrow or mfcol parameters of par(). For 3 rows and 1 column:

par(mfrow=c(3,1)) #mfrow will fill by rows

or

par(mfcol=c(3,1)) #mfcol will fill by columns

Do this before your plot() statement. No need to use ggplot2 or any other library.

Update:

Apparently I misunderstood your question, and if you want to add three datasets in one plot, you shouldn't need to use par, and do as @Carl Witthoft suggested and use plot() followed by lines() or points().

Plotting this way will create a plot with axis limits based on the data passed to plot(). You can change that setting the limits manually with xlim and ylim:

plot(1:10, (1:10)^2, xlim=c(0, 15), ylim=c(0, 150))
lines(1:15, seq(1, 150, length=15))
points(0:10, seq(1, 150, length=11))
Molx
  • 6,816
  • 2
  • 31
  • 47
  • thanks @Molx ! that was precise and easy :) although the plot does not contain all the coordinates of interest. It kind of looks restricted .. So i used xaxt="n" in the plot line followed by axis(1, 139.5, by = 17, las=1).. but seems to be not working.. could you advise on how to solve this also.. thanks a lot.. – Ram.V Mar 26 '15 at 08:19
1

It isn't clear whether you want multiple plots on one page, as Molx' solution provides, or whether you want to overlay the data in a single plot.

If the latter, you have a few choices. Try plotting one dataset, then par(new=TRUE), then the next plot command will write onto the same graph. Beware of duplicated axes and scaling problems.

Or you might plot the first set, and then explicitly call lines(cont[,1],cont[,2],...) to plot more datasets.

Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73
  • 1
    I'm sorry I didn't make it clear.. I want to overlay the data in a single plot. And so I used par(mfrow=c(1,1)) and that seems to have worked! I've so far managed to get two different sets of coordinates in the same plot. As I mentioned before the problem is that the plot is not complete. Seems to be restricted. For instance, I wanted to plot till 140,140 but the displayed plot is until 120,120 even with coordinates beyond that.. – Ram.V Mar 26 '15 at 08:51
  • @Ram.V Good solution -- now, `plot` by default sets the ranges to the min/max of the first dataset plotted. If you know the range you want, set it explicitly with `plot(x,y, xlim=c(min,max), ylim=c(min,max)` – Carl Witthoft Mar 26 '15 at 11:56