如何绘制多列CSV文件? [英] How to plot a multicolumn CSV file?

查看:190
本文介绍了如何绘制多列CSV文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个多列CSV(简单逗号分隔,没有引号)文件,其中第一行是标题,第一列是连续整数索引,其他17列是函数的浮点值。



任务是



听起来很简单,但实际上并不是很明显。

解决方案

您可以使用 read.csv 将数据输入为data.frame。然后你有很多选择绘图。对于大多数调查性工作,我更喜欢 lattice



  library(lattice)
d< - data.frame(index = 1:20,x = rnorm(20),y = rnorm(20))

>头(d,n = 3)
索引xy
1 1 -1.065591 0.2422635
2 2 -1.563782 -1.4250984
3 3 1.156537 0.3659411

xyplot (x + y〜index,data = d,type ='l',auto.key = list(space ='right'))

您可以从列的名称生成公式。我通常不从提示符这样做,但在代码中使用这样的结构:

  f <名称(d [,-1,drop = FALSE]),collapse =+),
名称(d [,1,drop = FALSE]),
sep =〜)

xyplot(as.formula(f),data = d,type ='l',auto.key = list(space ='right'))
/ pre>

在Ben的回答中, type ='l'指定行。默认值为 type ='p'。我在此处添加了 auto.key 参数来标记系列。



>


I am very new to R, so excuse me for a question probably stupid.

I've got a multicolumn CSV (plain comma-separated, no quotes) file where the first row is the header, the first column is a contiguous integer index and the other 17 columns are floating-point values of the functions.

The task is to plot all the 17 lines on the same chart (with the same axes).

Sounds very simple but isn't actually very obvious to do.

解决方案

You can use read.csv to input the data as a data.frame. Then you have plenty of choices for plotting. I prefer lattice for most investigative work.

Two in lattice. Here I am creating random data to chart.

library(lattice)
d <- data.frame(index=1:20, x=rnorm(20), y=rnorm(20))

> head(d, n=3)
  index         x          y
1     1 -1.065591  0.2422635
2     2 -1.563782 -1.4250984
3     3  1.156537  0.3659411

xyplot(x+y~index, data=d, type='l', auto.key=list(space='right'))

You can generate the formula from the names of the columns. I don't usually do this from the prompt, but use such constructs in code:

f <- paste(paste(names(d[,-1,drop=FALSE]), collapse="+"),
           names(d[,1,drop=FALSE]),
           sep=" ~ ")

xyplot(as.formula(f), data=d, type='l', auto.key=list(space='right'))

As in Ben's answer, type='l' specifies lines. The default is type='p' for points. I added the auto.key parameter here, to label the series.

这篇关于如何绘制多列CSV文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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