格式化R的输出

时间:2012-10-25 07:00:50

标签: r plot gnu output

我想在GNU图上绘制R的输出。例如,我有一个矩阵x存储整数,我将矩阵y设为y<- x^2

现在GNU绘图需要以下列格式输入输入数据文件:

x1 y1
x2 y2
x3 y3
... and so on...

如何从R生成此输出文件?

具体来说,如何删除R输出中显示的索引并垂直排列矩阵的元素?

2 个答案:

答案 0 :(得分:3)

我不完全清楚你是想要相互绘制两个向量,还是用矩阵做一些事情。我假设第一个。让我们创建一些示例数据:

x = 1:10
y = x^2    
z = cbind(x, y)

接下来,我们将其放在一个文件中:

write.table(z, file = "/tmp/spam", row.names = FALSE, col.names = FALSE)

如果我们检查输出:

$ cat /tmp/spam
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
10 100

我认为这就是你所需要的。或者,只需:

plot(y~x, z, type = "l")

enter image description here

完全避免使用GNUplot。或者甚至更好的imo,使用ggplot2

require(ggplot2); theme_set(theme_bw())
qplot(x, y, data = data.frame(z), geom = "line")

enter image description here

答案 1 :(得分:3)

在TeachingDemos包中,R和gnuplot之间有一组基本的接口函数,请参阅?gp.open。这些可以做你想要的,或者你可以查看代码,看一下以gnuplot想要的格式创建数据文件的例子。

相关问题