在R中对第一列绘制数据框的列

时间:2015-10-31 16:29:45

标签: r dataframe

在R中,如何将数据帧的所有列映射到该数据帧的第一列?

如果数据框有4列,则需要是3条重叠线的单个图。

plot(df)生成散点图矩阵,而

for(i in 1:ncol(df)) {
  plot(df$length, df[,i], type='l')
}

仅绘制第一列的最后一列。

1 个答案:

答案 0 :(得分:1)

我们可以使用matplot

matplot(x= df[,1], y= as.matrix(df[-1]), type='l', pch=1, 
       col= 2:5, xlab='first column', ylab = 'other columns')
legend("bottomright", inset=.05, legend=c("V2", "V3", "V4", "V5"), 
              pch=1, col= 2:5, horiz=TRUE)

数据

 df <- structure(list(V1 = c(12L, 21L, 50L, 71L, 98L, 135L, 147L, 178L, 
211L, 222L, 247L, 262L, 289L, 316L, 329L, 366L, 374L, 376L, 397L, 
403L), V2 = c(4L, 27L, 57L, 63L, 72L, 100L, 104L, 128L, 154L, 
156L, 169L, 179L, 194L, 200L, 238L, 247L, 277L, 303L, 331L, 370L
), V3 = c(19L, 35L, 46L, 55L, 57L, 61L, 77L, 92L, 103L, 117L, 
136L, 171L, 190L, 219L, 241L, 281L, 300L, 312L, 319L, 325L), 
    V4 = c(20L, 30L, 42L, 48L, 78L, 91L, 129L, 168L, 198L, 218L, 
    224L, 233L, 234L, 274L, 300L, 314L, 337L, 370L, 372L, 401L
    ), V5 = c(22L, 27L, 48L, 57L, 72L, 86L, 97L, 133L, 149L, 
    183L, 196L, 226L, 264L, 269L, 288L, 312L, 338L, 342L, 359L, 
    379L)), .Names = c("V1", "V2", "V3", "V4", "V5"), row.names = c(NA, 
-20L), class = "data.frame")