且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在R中绘制数据框的所有列

更新时间:2023-02-18 09:15:22

ggplot2 包需要一点学习,但结果看起来非常不错,你得到了不错的图例,以及许多其他不错的功能,无需编写大量代码.

The ggplot2 package takes a little bit of learning, but the results look really nice, you get nice legends, plus many other nice features, all without having to write much code.

require(ggplot2)
require(reshape2)
df <- data.frame(time = 1:10,
                 a = cumsum(rnorm(10)),
                 b = cumsum(rnorm(10)),
                 c = cumsum(rnorm(10)))
df <- melt(df ,  id.vars = 'time', variable.name = 'series')

# plot on same grid, each series colored differently -- 
# good if the series have same scale
ggplot(df, aes(time,value)) + geom_line(aes(colour = series))

# or plot on different plots
ggplot(df, aes(time,value)) + geom_line() + facet_grid(series ~ .)