1

I am getting data from six text files (each containing numbers separated by newlines):

args <- commandArgs(trailingOnly = TRUE)

t0 <- read.table(paste("2_",args[1],".txt",sep=""), header=FALSE, sep="\n")
t1 <- read.table(paste("4_",args[1],".txt",sep=""), header=FALSE, sep="\n")
t2 <- read.table(paste("6_",args[1],".txt",sep=""), header=FALSE, sep="\n")
t3 <- read.table(paste("8_",args[1],".txt",sep=""), header=FALSE, sep="\n")
t4 <- read.table(paste("10_",args[1],".txt",sep=""), header=FALSE, sep="\n")
t5 <- read.table(paste("12_",args[1],".txt",sep=""), header=FALSE, sep="\n")

I want to create one plot with 6 boxplots side-by-side using the same y-axis. I have consulted a similar question, but without success.

times <- matrix(c(t0,t1,t2,t3,t4,t5), ncol=6)

png(paste(args[1],".png",sep=""))
boxplot(x = as.list(as.data.frame(times)))
dev.off()

This produces the following error:

Error in sort.int(x, na.last = na.last, decreasing = decreasing, ...) : 
  'x' must be atomic
Calls: boxplot ... boxplot.stats -> <Anonymous> -> sort -> sort.default -> sort.int

I'm having a hard time understanding where I am going wrong. If someone could direct me in the write path or present an alternative way of achieving my goal, it would be greatly appreciated.

Thank you.

Edit

Below is a reproducible example, upon request.

c_graph.r:

#!/usr/bin/env Rscript

t0 <- read.table("t0.txt",header=FALSE, sep="\n")
t1 <- read.table("t1.txt",header=FALSE, sep="\n")
t2 <- read.table("t2.txt",header=FALSE, sep="\n")

times <- matrix(c(t0,t1,t2), ncol=3)

png("test.png")
boxplot(x = as.list(as.data.frame(times)))
dev.off()

t0.txt, t1.txt, t2.txt (all with the same contents):

5287
5287
58
2
525
8
758
7587
587

Running the code:

Rscript c_graph.r

Results:

Error in sort.int(x, na.last = na.last, decreasing = decreasing, ...) : 
  'x' must be atomic
Calls: boxplot ... boxplot.stats -> <Anonymous> -> sort -> sort.default -> sort.int
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Alex Brooks
  • 1,151
  • 1
  • 10
  • 39
  • What is in your `t0` ... `t5`? – sgibb Jul 24 '13 at 17:00
  • Sorry, I should have specified that. The files contain numbers separated by newlines. – Alex Brooks Jul 24 '13 at 17:05
  • check out this post http://stackoverflow.com/questions/7738510/plotting-several-groups-of-box-plots-side-by-side-in-r – user2510479 Jul 24 '13 at 17:08
  • You create a list of data.frames `c(t0,t1,t2)` but you want only the numeric values `matrix(unlist(c(t0,t1,t2)), ncol=3)`. Maybe you want to replace your `read.table` by `scan` to avoid these problems. – sgibb Jul 24 '13 at 17:53
  • @sgibb That worked. I replaced `read.table(...)` with `scan(...)` and the plots were successfully created. If you add it as an answer, I will accept. – Alex Brooks Jul 24 '13 at 17:57

2 Answers2

1

The problem occurs when you convert the files you've read in to a matrix. Consider:

set.seed(90)

t0 <- rnorm(9)
t1 <- rnorm(9)
t2 <- rnorm(9)

times1 <- matrix(c(t0,t1,t2), ncol=6)
> times1
           [,1]       [,2]        [,3]        [,4]       [,5]       [,6]
[1,]  0.0771813  0.4425903 -0.80517064 -0.05976005 -0.5882710  0.1291539
[2,] -0.1510609  1.0055101 -0.08230689 -0.34302853 -0.1315423 -0.3980679
[3,] -0.8840764  0.9144189  0.86718542  0.87410829  1.3159242  0.0771813
[4,] -0.7205931 -0.5663887  1.65919765  0.97977040 -1.2910153 -0.1510609
[5,]  0.7407430  2.3930961 -0.24084853 -0.76047498 -0.3720799 -0.8840764


t0 <- read.table("t0.txt",header=FALSE, sep="\n")
t1 <- read.table("t1.txt",header=FALSE, sep="\n")
t2 <- read.table("t2.txt",header=FALSE, sep="\n")

times2 <- matrix(c(t0,t1,t2), ncol=3)
> times2
     [,1]      [,2]      [,3]     
[1,] Integer,9 Integer,9 Integer,9

This works:

times3 <- data.frame(t0,t1,t2)
windows()
  boxplot(times3)

enter image description here

gung - Reinstate Monica
  • 11,583
  • 7
  • 60
  • 79
1

It seems you get trouble by converting your data from data.frame to matrix. You create a list of data.frames c(t0,t1,t2) but you want only the numeric values.

Therefore you have to extract each element from the data.frame by accessing the column directly:

matrix(c(t0$V1, t1$V1, t2$V1), ncol=3)

Or you could use unlist:

matrix(unlist(c(t0, t1, t2)), ncol=3)

Or to circumvent all this trouble replace your read.table by scan:

t0 <- scan("t0.txt")
sgibb
  • 25,396
  • 3
  • 68
  • 74