在R中为多个箱形图创建循环 [英] Creating a loop in r for multiple box plots
问题描述
我设计了一个实验,以观察自进餐后血清标志物如何随时间变化.我有一个包含72个观测值和23o个变量的数据框,这称为BreakfastM.
I have designed an experiment to see how serum markers change with time since eating a meal. I have a data frame consisting of 72 observations and 23o variables this is called BreakfastM.
有229个变量是血清标志物,还有1个是时间点.观察结果是不同的样本
There are 229 variables which are serum markers and 1 which is timepoint. The observations are different samples
我正在寻找血清标志物(即胆固醇)如何随时间变化的数据趋势.我创建了一个箱线图,可以很好地显示特定血清标志物相对于时间点的趋势
Iam looking for trends in the data of how the serum markers (ie cholestrol) change with the timepoint. I have created a boxplot which shows nicely the trends in a particular serum marker in relation to timepoint
这是我使用的代码
boxplot((BreakfastM$Variable~BreakfastM$Timepoint))
是否有一种通过在R中编写循环代码来针对时间点测试数据帧中所有变量的快速方法?
Is there a quick way to test all the variables in the dataframe against the timepoint by writing a loop code in R?
推荐答案
您还可以使用循环将许多图表写入工作目录中的图像文件.让我们制作一个10列的矩阵,代表10个测量变量,每个变量均按3个因子水平进行划分:
You can also use a loop to write many plots to image files in your working directory. Let's make a 10 column matrix representing 10 measured variables, each split by 3 factor levels:
data <- matrix(rnorm(150), nrow=15)
grps <- factor(c(rep("group1", 5), rep("group2", 5), rep("group3", 5)))
循环将每个箱形图写入名为var_1.png
,var_2.png
等的文件.这会将10个png
放入您的工作目录中.
The loop writes each boxplot to files called var_1.png
, var_2.png
, etc. This will put 10 png
s in your working directory.
for (i in 1:ncol(data)) {
png(file = paste("var_", i, ".png", sep=""))
boxplot(data[, i] ~ grps)
dev.off()
}
文件很小,您可以使用简单的图像查看器快速浏览它们.
The files are very small and you can flick through them quickly with a simple image viewer.
这篇关于在R中为多个箱形图创建循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!