[英]Multiple boxplots on one graph
我從六個文本文件中獲取數據(每個文本文件均包含用換行符分隔的數字):
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")
我想使用相同的y軸並排創建6個箱型圖。 我曾咨詢過類似的問題 ,但沒有成功。
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
當您將已讀入的文件轉換為矩陣時,會發生問題。 考慮:
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)
通過將數據從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)
為了避免所有麻煩,請用scan
替換您的read.table
:
t0 <- scan("t0.txt")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.