我有10个时间点(行)的300个变量(列),对于每个变量,在任何给定的时间点,我都有温度值A和F.所以在我的数据帧上,我有2行用于A + F的时间点1,所以上
这是我可重复的例子
Timepoint <- c('1','1','2','2','3','3')
Temperature <- c('A','F','A','F','A','F')
Cholesterol <- c('1','2','3','2','3','4')
data.frame(Timepoint,Temperature,Cholesterol)
这是我的实际数据样本
structure(list(Timepoint = c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L,
5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 13L, 13L, 25L, 25L),
Temperature = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("A",
"F"), class = "factor"), Concentration.of.chylomicrons = c(1.29e-11,
1.25e-11, 1.02e-11, 1.1e-11, 1.08e-11, 1.3e-11, 1.28e-11,
1.26e-11, 1.06e-11, 1.32e-11, 8.85e-12, 1.21e-11, 8.83e-12,
1.08e-11, 1.35e-11, 1.12e-11, 8.99e-12, 1.08e-11, 9.55e-12,
1.04e-11, 0, 1.01e-11), Total.lipids = c(0.00268, 0.0026,
0.00208, 0.00225, 0.00222, 0.0027, 0.00268, 0.0026, 0.00219,
0.00273, 0.0018, 0.00247, 0.00179, 0.00221, 0.00276, 0.00229,
0.00182, 0.00222, 0.00195, 0.00212, 0, 0.00204), Phospholipids = c(0.000224,
0.000223, 0.000145, 0.00016, 0.000157, 0.000211, 0.00023,
0.000211, 0.000165, 0.000224, 0.000109, 0.00018, 0.000113,
0.000163, 0.000175, 0.000177, 0.000122, 0.000173, 0.000127,
0.000156, 0, 0.000138)), .Names = c("Timepoint", "Temperature",
"Concentration.of.chylomicrons", "Total.lipids", "Phospholipids"
), class = "data.frame", row.names = c(NA, -22L))
我想绘制一个折线图,以显示每个变量如何随时间变化。在这个折线图上,我希望绘制A和B线
我已设法使用以下代码执行此操作
plot(DATA$Timepoint[DATA$Temperature=="F"],DATA$variable[DATA$Temperature=="F"])
lines(DATA$Timepoint[DATA$Temperature=="F"],DATA$Cvariable[TempMline$Temperature=="A"])
这给出了变量如何随时间变化的曲线图,每个温度A + F都有一条单独的线。
我正在尝试编写一个循环命令来绘制每个变量的图形,尽管我在使用[]子集数据时遇到了问题,并且还编写了循环命令
这是我的尝试
lnames <- names(DATA)
classes<-sapply(DATA,class)
for(name in lnames[classes == 'numeric']) {
plot(DATA$Timepoint[DATA$Temperature=="F"],DATA$[,name][DATA$Temperature=="F"])
lines(DATA$Timepoint[DATA$Temperature=="F"],DATA$[,name][TempMline$Temperature=="A"]) }
答案 0 :(得分:1)
这是你正在寻找的吗?我只显示最后一张图,因为这是循环的结束。如果你正在使用RStudio,你可以在情节查看器中切换左/右箭头键。
这是绘图和循环的代码:
# subset based on temperatures A and F
a_df <- subset(df, Temperature == "A")
f_df <- subset(df, Temperature == "F")
# loop from columns 3:5 (or for the number of variables you have)
for (i in 3:5) {
plot(a_df[, 1],
a_df[, i],
type = "l",
ylab = colnames(a_df[i]),
xlab = "Timepoint",
lwd = 2,
col = "blue")
lines(f_df[, 1],
f_df[, i],
type = "l",
lwd = 2,
col = "red")
legend("bottomleft",
col = c("blue", "red"),
legend = c("Temperature A", "Temperature F"),
lwd = 2,
y.intersp = 0.5,
bty = "n")
}