我从六个文本文件(通过换行分隔每个包含数字)获取数据:
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")
我想创建一个情节6个箱图并排侧使用相同的y轴。我曾咨询过similar question,但没有成功。
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()
这将产生以下错误:
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
我在我要去的地方错了很难理解。如果有人能告诉我在写路径或呈现实现我的目标的替代方式,将不胜感激。
谢谢。
编辑
下面是一个可再现的示例中,在请求。
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
(都具有相同的内容):
5287
5287
58
2
525
8
758
7587
587
运行代码:
Rscript c_graph.r
结果:
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
看来你从data.frame
到matrix
将您的数据得到的麻烦。创建data.frames c(t0,t1,t2)
的列表,但你只想要的数值。
因此,你必须通过直接访问柱以提取从data.frame每个元素:
matrix(c(t0$V1, t1$V1, t2$V1), ncol=3)
或者你可以使用unlist
:
matrix(unlist(c(t0, t1, t2)), ncol=3)
或规避一切麻烦的read.table
取代你scan
:
t0 <- scan("t0.txt")
当你转换你的矩阵已经阅读文件时出现问题。考虑:
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
这工作:
times3 <- data.frame(t0,t1,t2)
windows()
boxplot(times3)