绘制R中数据帧的多行 [英] Plotting multiple lines from a data frame in R

查看:101
本文介绍了绘制R中数据帧的多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <$> 

我正在构建一个R函数来绘制数据表中的几行代码,我不明白为什么这不起作用? c $ c> data = read.table(path,header = TRUE);
plot(data $ noop);
行(data $ noop,col =blue);
行(数据$ plus,col =green);

我从文件中读取数据,格式如下:

  noop加分钟
33.3 33.3 33.3
30.0 40.0 30.0
25.0 50.0 25.0

这是数据集的最小表示形式,它包含更多标题和更多数据点。因此,此数据集的每一行都反映了在给定时间采集的样本。所以我的目标是从文件中读取这些数据,然后将每一列绘制成一系列由不同颜色的线连接的点。

目前我使用的方法是只绘制1行,而不是多行。

解决方案(ggplot2)
library(reshape)


data < - data.frame(time = seq(0,23),noob = rnorm(24),plus = runif(24),extra = rpois(24,lambda = 1))
熔化< - 熔化(数据,id.vars =时间)
ggplot(Molten,aes(x = time,y = value,color = variable))+ geom_line()


I am building an R function to plot a few lines from a data table, I don't understand why this is not working?

data = read.table(path, header=TRUE);
plot(data$noop);
lines(data$noop, col="blue");
lines(data$plus, col="green");

I am reading the data from a file I has which is formatted like this:

 noop         plus         mins
 33.3         33.3         33.3
 30.0         40.0         30.0
 25.0         50.0         25.0

This is the minimal representation of the dataset, which contains more headers and more data points. So each of the rows of this data set reflects a sample taken at a given time. So my objective is to read this data in from the file, and then plot each column as a series of points connected by lines of different color.

The approach I am using currently is only plotting 1 line, and not multiple lines.

解决方案

Have a look at the ggplot2 package

library(ggplot2)
library(reshape)
data <- data.frame(time = seq(0, 23), noob = rnorm(24), plus = runif(24), extra = rpois(24, lambda = 1))
Molten <- melt(data, id.vars = "time")
ggplot(Molten, aes(x = time, y = value, colour = variable)) + geom_line()

这篇关于绘制R中数据帧的多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆