R线图,绘图区域外的值

时间:2016-11-30 15:10:14

标签: r for-loop graph

我有10个时间点(行)的300个变量(列),对于任何给定时间点的每个变量,我都有温度值A和F.

附件是数据框的样本

 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 我怎样才能确保在这个循环命令中我能看到所有点都可见的格雷格。我确定有一种快速解决方法,任何人都可以帮忙吗?

我已经尝试了以下这一行,请提出建议      ylim = c(min(f_df [, - 1],max(f_df [, - 1]),

我收到以下错误消息

  

for(i in 3:229){   + plot(a_df [,1],   + a_df [,i],   + type =&#34; b&#34;,   + ylim = c(min(f_df [, - 1],max(f_df [, - 1]),   + ylab = colnames(f_df [i]),   + main = colnames(f_df [i]),   + xlab =&#34; Timepoint&#34;,   + lwd = 2,   + col =&#34; red&#34;)   +行(f_df [,1],   错误:意外的符号:   &#34; col =&#34; red&#34;)     线&#34;           f_df [,i],   错误:意外&#39;,&#39; in&#34; f_df [,i],&#34;           type =&#34; b&#34;,   错误:意外&#39;,&#39; in&#34; type =&#34; b&#34;,&#34;           lwd = 2,   错误:意外&#39;,&#39; in&#34; lwd = 2,&#34;           col =&#34; blue&#34;)   错误:意外&#39;)&#39; in&#34; col =&#34; blue&#34;)&#34;     图例(&#34; BOTTOMLEFT&#34 ;,   + col = c(&#34;红色&#34;,&#34;蓝色&#34;),   + legend = c(&#34; Ambient&#34;,&#34; Fridge&#34;),   + lwd = 2,   + y.intersp = 0.5,   + bty =&#34; n&#34;)   strwidth错误(图例,单位=&#34;用户&#34;,cex = cex,font = text.font):     plot.new还没有被调用   }   错误:意外&#39;}&#39;在&#34;}&#34;

Lakmal

1 个答案:

答案 0 :(得分: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]),因为它们应该相同。