简体   繁体   English

格式化R的输出

[英]Formatting output of R

I want to plot the output of R on GNU plot. 我想在GNU图上绘制R的输出。 Eg, I have a matrix x storing integers, and I make matrix y as y<- x^2 . 例如,我有一个存储整数的矩阵x ,我将矩阵y设为y<- x^2

Now GNU plot requires input in input data file in following format: 现在GNU plot需要以下列格式输入输入数据文件:

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

How can I produce this output file from R? 如何从R生成此输出文件?

Specifically, how do I remove indices that are shown in output of R and arrange the elements of a matrix vertically? 具体来说,如何删除R输出中显示的索引并垂直排列矩阵的元素?

It is not entirely clear to me if you want to plot two vectors against each other, or do something with a matrix. 如果你想要相互绘制两个向量,或者用矩阵做某事,我并不完全清楚。 I assume the first. 我假设第一个。 Let's create some example data: 让我们创建一些示例数据:

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

Next, we put this in a file: 接下来,我们将其放在一个文件中:

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

and if we check the output: 如果我们检查输出:

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

I think this is what you need. 我想这就是你需要的。 Alternatively, just do: 或者,只需:

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

在此输入图像描述

and avoid GNUplot altogether. 并完全避免使用GNUplot。 Or even better imo, use ggplot2 : 或者甚至更好的imo,使用ggplot2

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

在此输入图像描述

There is a basic set of interface functions between R and gnuplot in the TeachingDemos package, see ?gp.open . 在TeachingDemos包中,R和gnuplot之间有一组基本的接口函数,参见?gp.open These may do what you want, or you can look at the code for an example of creating a datafile in the format that gnuplot wants. 这些可以做你想要的,或者你可以查看代码,以获得gnuplot想要的格式创建数据文件的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM