我正在尝试创建一个循环来从先前创建的数组中提取数据,这样我就可以使用提取的数据生成线图。
到目前为止,我一直在使用以下方式手动完成:
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
然后使用绘图代码。
答案 0 :(得分: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")
})
就像这样,你只会看到最后一张图,因为其余图通常会被覆盖。因此,您可能需要添加布局语句,或者在图形之间提供暂停等。
好的,根据您的评论提供更多信息:
a_ply
这里有3个参数:第一个是执行操作的数组,接下来是'margin',意思是:我应该迭代哪个维度(这是'隐藏'循环),最后一个在所有部件上执行的功能。
那么会发生什么:a_ply为数组的第三维获取所有可能的值(因为margin == 3),并在它们上运行(你可以在for循环中看到它作为索引器i
)。然后它为每个值(类似于t_weekmean[,,i]
)获取数组的一部分,并将其提供给作为第三个参数的函数(因此在此函数中,连续的边缘数组将被称为arrayforcurweek )。
这种工作方式的问题在于图表是连续快速生成的,因此如果您只是运行此代码并查看图像窗口,您应该只能看到第三维的最后一个值的图形。如果你想看到彼此相邻的所有这些(虽然这会导致很小的图形),你可以在前面添加如下内容: 布局(矩阵(1:30),nrow = 6) 这将导致屏幕在30中分割,这样每个绘图都会自动显示整个屏幕。
我相信,如果你立即写一个pdf或类似的东西,你不需要这个,但我没有经验。
这对你有帮助吗?
答案 1 :(得分: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"
)
我将您的数据重新创建为:
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"))
)