2011-06-09 58 views
4

我正在嘗試創建一個循環以從之前創建的數組中提取數據,以便我可以使用提取的數據生成一個線圖。如何在R中創建循環以迭代地繪製數組元素?

到目前爲止,我一直在做手工用:

allweek1<-(data.frame(t_weekmean[,,1])) #which selects the date and generates the data frame I want to later format the date using 
week1<-stack(allweek1) #and then plot it using 
plot(week1$values,type="n", xlim=c(0,2),xlab="Weight (gr)",ylab="Rate (umol/L*gr)",main="All individuals and Treatments at all times") 
lines(week1$values[week1$ind=="X9"]~x,type="o",col="red") 
lines(week1$values[week1$ind=="X12"]~x,type="o",col="blue") 
lines(week1$values[week1$ind=="X15"]~x,type="o",col="green") 
lines(week1$values[week1$ind=="X18"]~x,type="o",col="purple"). 

我知道必須有使這變成一個循環,在這個例子中,我給短短兩週的一種方式,但我的數據去最多30個,手動操作會很麻煩,容易出錯。

這是起始陣列我有:

, , Week = 1 

     Temp 
variable  9  12  15  18 
    X0 100.000 100.000 100.000 100.000 
    X0.5 98.855 98.591 98.357 99.003 
    X1 98.004 97.804 97.638 98.299 
    X1.5 95.953 96.999 96.810 97.555 
    X2 95.235 96.078 95.346 96.665 

, , Week = 2 

     Temp 
variable  9  12  15  18 
    X0 100.000 100.000 100.000 100.000 
    X0.5 99.137 99.035 97.883 99.055 
    X1 98.420 98.298 96.459 97.765 
    X1.5 97.939 97.181 94.406 96.546 
    X2 96.998 96.237 91.906 95.263 

以下的數據幀,然後將其轉換爲一個堆版本:

  X9  X12  X15  X18 
X0 100.000 100.000 100.000 100.000 
X0.5 98.855 98.591 98.357 99.003 
X1 98.004 97.804 97.638 98.299 
X1.5 95.953 96.999 96.810 97.555 
X2 95.235 96.078 95.346 96.665 

,然後被用於繪製的代碼。

回答

3

聽起來任務格:

X <- as.data.frame(as.table(t_weekmean), stringsAsFactors=FALSE, responseName="values") 
X$variable <- as.numeric(gsub("^X","",X$variable)) 
X$Temp <- as.numeric(X$Temp) 

require(lattice) 
xyplot(values~variable|Week, groups=Temp, X, type="o", as.table=TRUE, 
    xlab="Weight (gr)", ylab="Rate (umol/L*gr)", main="All individuals and Treatments at all times" 
) 

Multi-plot in Lattice

我重新創建數據:

t_weekmean <- structure(c(100, 98.855, 98.004, 95.953, 95.235, 100, 98.591, 97.804, 96.999, 96.078, 100, 98.357, 97.638, 96.81, 95.346, 100, 99.003, 98.299, 97.555, 96.665, 100, 99.137, 98.42, 97.939, 96.998, 
100, 99.035, 98.298, 97.181, 96.237, 100, 97.883, 96.459, 94.406, 91.906, 100, 99.055, 97.765, 96.546, 95.263, 99.9889679441867, 
98.8470416045204, 98.010997102523, 95.9636806506725, 95.235986063534, 100.00797414162, 98.5968712619705, 97.7984016535804, 96.9904933552904, 
96.0816877686208, 99.9946318131395, 98.3568674165109, 97.6357767063124, 96.8119443900658, 95.3441814383421, 99.989633272252, 99.0037062049508, 
98.3034580102509, 97.5568340624981, 96.6615796074679, 100.000379644977, 99.1375077671092, 98.4187321210541, 97.9350205929782, 97.0006243532971, 
100.003971157774, 99.0316462150477, 98.298322594611, 97.1782003010139, 96.239865449585, 100.002464797458, 97.8810655647218, 96.4592857614756, 
94.4099917372801, 91.9025173998885, 100.003642400375, 99.0529984607268, 97.76302246443, 96.5426428484451, 95.2658935513329), 
.Dim = c(5L, 4L, 4L), .Dimnames = structure(list(variable = c("X0", "X0.5", "X1", "X1.5", "X2"), 
Temp = c("9", "12", "15", "18"), Week = c("1", "2", "3", "4")), .Names = c("variable", "Temp", "Week")) 
) 
3

如果使用plyr,你可以用a_ply做到這一點:

a_ply(t_weekmean, 3, function(arrayforcurweek){ 
allweek1<-(data.frame(arrayforcurweek)) #which selects the date and generates the data frame I want to later format the date using 
week1<-stack(allweek1) #and then plot it using 
plot(week1$values,type="n", xlim=c(0,2),xlab="Weight (gr)",ylab="Rate (umol/L*gr)",main="All individuals and Treatments at all times") 
lines(week1$values[week1$ind=="X9"]~x,type="o",col="red") 
lines(week1$values[week1$ind=="X12"]~x,type="o",col="blue") 
lines(week1$values[week1$ind=="X15"]~x,type="o",col="green") 
lines(week1$values[week1$ind=="X18"]~x,type="o",col="purple") 
}) 

一樣,這一點,你只會看到過的最後一個圖形,因爲剩下的就是典型的覆蓋。所以,你可能要添加一個佈局聲明,或提供的圖表等

OK,一些更多的信息,按您的評論之間的停頓:

a_ply需要3個參數在這裏:首先一個要執行陣列事情,下一個'邊緣',這意味着:我應該迭代哪個維度(這是'隱藏'循環),最後是一個對所有部分執行的函數。

所以會發生什麼:a_ply爲您的數組的第三維(因爲邊距== 3)採取所有可能的值,並運行它們(你可以在for循環中看到這是一個索引器i)。然後它將你的數組的部分作爲這些值中的每一個(類似t_weekmean[,,i]),並將其作爲第三個參數提供給函數(因此在此函數中,連續的邊際數組將稱爲arrayforcurweek)。

這種工作方式的問題在於圖形是快速連續生成的,所以如果您只是運行此代碼並查看圖像窗口,則應該只能看到第三維的最後一個值的圖形。如果你想看到他們所有的相鄰(儘管這會導致微小的圖表),你可以用類似這樣的前綴: 佈局(矩陣(1:30),nrow = 6) 這將導致屏幕將其分成30部分,以便每個小區都能在全屏中獲得自己的一部分。

我相信,如果你立即寫一個PDF或類似的文件,你不需要這個,但我沒有這方面的經驗。

這幫助你嗎?

+0

也許我的編輯更清晰? – 2011-06-09 10:23:37

+0

非常感謝!我將添加一個參數(mfrow = c(2,2)),以便每個圖表具有4周的設置,並瞭解如何保存它們。謝謝!!! – BDM 2011-06-09 10:50:02