2016-11-30 86 views
0

我已300個變量在10個時間點(行),在任何給定時間點對各變量I具有溫度值A(列)和F.R線的曲線圖,值的範圍之外繪圖區

附上的樣品數據幀

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和F線。我已經成功地爲此編寫了循環代碼。

# subset based on temperatures A and F 
a_df <- subset(df, Temperature == "A") 
f_df <- subset(df, Temperature == "F") 

# loop from columns 3:x 
for (i in 3:x) { 
    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") 
} 

然而,對於某些變量,某些地方的繪圖區之外,像下面附

Please click on this link for image 我怎樣才能確保在這個循環中的命令,我可以有可見的所有點graghs。我確定有一個快速的方法來解決這個問題,任何人都可以幫忙嗎?

我曾嘗試下面的行,請建議 ylim = C(分鐘(f_df [, - 1],最大值(f_df [, - 1]),

我得到以下錯誤消息

for (i in 3:229) { + plot(a_df[, 1], + a_df[, i], + type = "b", + ylim = c(min(f_df[,-1] ,max(f_df[,-1]), + ylab = colnames(f_df[i]), + main = colnames(f_df[i]), + xlab = "Timepoint", + lwd = 2, + col = "red") + lines(f_df[, 1], Error: unexpected symbol in: " col = "red") lines" f_df[, i], Error: unexpected ',' in " f_df[, i]," type = "b", Error: unexpected ',' in " type = "b"," lwd = 2, Error: unexpected ',' in " lwd = 2," col = "blue") Error: unexpected ')' in " col = "blue")" legend("bottomleft", + col = c("red", "blue"), + legend = c("Ambient", "Fridge"), + lwd = 2, + y.intersp = 0.5, + bty = "n") Error in strwidth(legend, units = "user", cex = cex, font = text.font) : plot.new has not been called yet } Error: unexpected '}' in "}"

Lakmal

+0

在你的繪圖命令中嘗試'ylim = c(min(f_df [, - 1],max(f_df [, - 1])? – dww

+0

在我寫入該行的命令中,我得到錯誤沒有任何地塊 – NLM09

+0

它是一個想法將plot限制添加到for循環中的「plot」函數以匹配數據的範圍嗎?例如:'ylim = c(min(a_df [,i],f_df [ ,i]),max(a_df [,i],f_df [,i])),編輯:與dww相同的建議,但我認爲你需要在兩個數據集中指定範圍作爲最小值和最大值。把這個命令放到'plot'函數中就可以工作了。 – Niek

回答

0

要在回答中回顧一下。設置ylim解決問題

# loop from columns 3:x 
for (i in 3:x) { 
    plot(a_df[, 1], 
     a_df[, i], 
     type = "l", 
     ylab = colnames(a_df[i]), 
     xlab = "Timepoint", 
     ylim = c(min(df[,-1]) ,max(df[,-1])), 
     lwd = 2, 
     col = "blue") 
... 

將每個地塊的地塊邊界設置爲相等,如果您想比較地塊,那麼地塊面積可能會比您的數據大得多。

# loop from columns 3:x 
for (i in 3:x) { 
    plot(a_df[, 1], 
     a_df[, i], 
     type = "l", 
     ylab = colnames(a_df[i]), 
     xlab = "Timepoint", 
     ylim = c(min(df[,i]) ,max(df[,i])), 
     lwd = 2, 
     col = "blue") 
... 

集對於每個小區,這是更差作比較,但減少了不必要的空積空間的新邊界。我已將min(a_df[, i],f_df[, i])替換爲min(df[,i]),因爲它們應該是相同的。