0

I want to plot several files in the same figure; each file has two-column data.

The problem is that each file has a different number of rows (529,567,660, etc)

For data with same number of rows I did the following:

  data1 <- read.table(file="ro0.2/T0.1/sq_Ave.dat")
  x1 <- data1[1]
  y1 <- data1[2]
  data2 <- read.table(file="ro0.4/T0.1/sq_Ave.dat")
  x2 <- data2[1]
  y2 <- data2[2]

  max_valuex = max(x1,x2,x3,x4,x5)
  max_valuey = max(y1,y2,y3,y4,y5)
  matplot(x1,cbind(y1,y2,y3,y4,y5),type="l", 
       col=c("black","red","green","blue","orange"),
       lwd = 2,xlab = expression(q*sigma), ylab="S(q)", col.lab="black",
       cex.lab=1.5,font.lab=4, xaxt = "n", yaxt = "n", xlim = c(0,max_valuex), 
       ylim = c(0,max_valuey), xaxs = "i", yaxs = "i")

However, this does not work for files with different number of rows.

R complains with:

Error in data.frame(..., check.names = FALSE) : 
arguments imply differing number of rows: 529, 567, 661
Calls: matplot -> ncol -> as.matrix -> cbind -> cbind -> data.frame

Any idea or suggestion would be greatly appreciated!

Thanks a lot in advance

S H-V

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • 1
    If it is going to "work" then you need to say how the data should be arranged when unequal length items are being displayed. (You will need to either align your vectors in a matrix or use `lines`. – IRTFM Oct 09 '13 at 19:59
  • 1
    Hi and Welcome to stackoverflow! As you are new on SO, please take some time to read [about Stackoverflow](http://stackoverflow.com/about) and [how to ask](http://meta.stackoverflow.com/help/how-to-ask). You are much more likely to receive a helpful answer if you provide a [minimal, reproducible data set](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) together with the code you have tried. Thanks! – Henrik Oct 09 '13 at 20:12

2 Answers2

1

I guess you could enlarg your vectors with NAs. I believe this won't matter in next handling your data. E.g.:

    a= 1:10
    b=1:5
    d=1:7

    data.frame(a,b,d)   #different length
    #Error in data.frame(a, b, d) : 
    #arguments imply differing number of rows: 10, 5, 7

    length(b) = length(d) = length(a)

    data.frame(a,b,d)  # no error now

        a  b  d
    1   1  1  1
    2   2  2  2
    3   3  3  3
    4   4  4  4
    5   5  5  5
    6   6 NA  6
    7   7 NA  7
    8   8 NA NA
    9   9 NA NA
    10 10 NA NA
alexis_laz
  • 12,884
  • 4
  • 27
  • 37
0

Even if you manage to load the y-axis data into a list object which would be the natural data type in R for storing vectors of variable length. In the next step you will get something like:

> matplot (matrix(1:100, nrow=10, ncol=10)[1], matrix(1:100, nrow=12, ncol=10))
Error in matplot(matrix(1:100, nrow = 10, ncol = 10)[, 1], matrix(1:100,  : 
'x' and 'y' must have same number of rows

A scatterplot like plot or matplot needs complete x,y tuples, but in your code you are only using x1 as the x-values. Your code example is not complete. You are also loading x2,x3,... but only use them to calculate xlim. Why would you calculate xlim including the maxima of all x if you do not intend to plot them?

It is therefore not clear to me what the final plot should look like, and wether or not a scatterplot is the correct visualization of the data. You might want to give more details about what your data in fact consists of and how incomplete data should be handled.

Could it be that you want to plot several line-plots into a single figure, using add=TRUE:

matplot(data1[,1:2], xlim = c(0,max_valuex), ylim = c(0,max_valuey))
matplot(data2[,1:2], add=TRUE)
Michael
  • 232
  • 2
  • 12
  • Hi Michael, Thanks for your reply! In fact I would like to know how I can plot several line-plots into a single figure. My problem is that I do not know how to handle x-data for each file (x1,x2,x3,x4,x5). Each file has the data of a physical observable. The y-values are quite different. The x-values are very similar [0,~16] but the partition is different. The partitioning of x-axis is the reason of having different number of rows for each file. I just want to visualize each graph (xi,yi) in a single figure. But I can not figure out how to do that. Thanks! – Dicastelgandolfo Chava Oct 09 '13 at 21:36