gpt4 book ai didi

r - 如何在 R 中创建循环来迭代绘制数组的元素?

转载 作者:行者123 更新时间:2023-12-02 02:28:47 27 4
gpt4 key购买 nike

我正在尝试创建一个循环来从之前创建的数组中提取数据,以便我可以使用提取的数据来生成线图。

到目前为止,我一直使用以下方法手动完成此操作:

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

然后使用绘图代码。

最佳答案

听起来像是格子的任务:

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"))
)

关于r - 如何在 R 中创建循环来迭代绘制数组的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6290837/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com