如何在R中拆分数据集和绘图 [英] How to Split Dataset and plot in R
问题描述
我正在使用如下数据集:
I am using a data set like:
1 48434 14566
1 56711 6289
1 58826 4174
2 56626 6374
2 58888 4112
2 59549 3451
2 60020 2980
2 60468 2532
3 56586 6414
3 58691 4309
3 59360 3640
3 59941 3059
.
.
.
10 56757 6243
10 58895 4105
10 59565 3435
10 60120 2880
10 60634 2366
对于第一列的每个值,我需要在第三列的R中绘制一个图,即,对于上述数据,第三列的值(每个组1-10)将有10个不同的图.x轴是迭代数,Y轴是最大63000的值.我还需要将点连接成红色的线.我是R的新手,一直在阅读文档,但这使我更加困惑.身体有没有帮助?
I need a plot in R of 3rd column for each value of first column i.e. for above data there would be 10 different plots of (each group 1-10) of values of 3rd column. x-axis is number of Iterations and Y-axis is the values with max 63000. I also need to connect the dots with a line in color red. I am new to R and have been reading documentation but that confused me more. could any body plz help.
我实际上想要V3值的折线图.v3列的行数将在x轴上,而v3值将在y轴上.我想要由v1表示的组各有不同的图形.Chase的解决方案有效,除了我要移动轴,V3值应在y轴上.这里是示例
@Roman,这是我正在执行的代码.
library(lattice)
d <- read.delim("c:\\proj58\\positions23.txt",sep="")
d <- do.call(rbind, lapply(split(d, d$V1), function(x) {
x$iterations <- order(x$V3, decreasing=TRUE)
x
}))
xyplot(V3 ~ iterations | V1, type="l", data=d)
这是我收到的错误
>
> source("C:\\proj58\\plots2.R")
> d
V1 V2 V3 iterations
1.1 1 48434 14566 1
1.2 1 56711 6289 2
1.3 1 58826 4174 3
1.4 1 59528 3472 4
我没有积蓄吗?我想念什么好的,我知道了.不知道怎么了在这里,
I am not getting any plot?? what am I missing OK: Got It. don't know what was wrong. Here it is,
还有2件事,如何将包装盒上的V1标签更改为实际数字,例如1,2,...其次,我的文件包含100个组,我尝试了一个,并且将所有图形都放在一个页面上(显然不可读),我可以在多个窗口上创建这些图形吗?
2 more things, how to change V1 labels on the boxes to actual numbers like 1,2,... secondly I have files that contain 100 groups, I tried one and it made all graphs on a single page (unreadable obviously), can I make these on more than one windows?
推荐答案
首先,您需要为第一个变量的每个子集分别创建一个带有行号的变量.这是一种方法,方法是将数据集除以第一个变量,再创建一个具有行号的新变量,然后重新组合.
Well, first you need to create a variable with the row number, for each subset of the first variable separately. Here's one way to do it, by splitting the data set by the first variable, making a new variable that has the row number, and recombining.
您可能还希望V1是一个因子(分类变量).
You also probably want V1 to be a factor (a categorical variable).
d <- do.call(rbind, lapply(split(d, d$V1), function(x) {
x$iterations <- 1:nrow(x)
x
}))
d$V1 <- factor(d$V1)
然后使用 lattice
库,您将执行类似的操作
Then using the lattice
library, you'd do something like
xyplot(V3 ~ iterations | V1, type="l", data=d)
要使绘图显示在一页以上,请使用 layout
选项限制页面上的绘图数量.您需要将绘图保存到支持多页输出的文件中.例如,对于5行5列:
To make the plots appear on more than one page, limit the number of plots on a page using the layout
option. You'll need to save the plot to a file that supports multi-page output to do that. For example, for 5 rows and 5 columns:
trellis.device("pdf", file="myplot.pdf")
p <- xyplot(V3 ~ iterations | V1, type="l", data=d, layout=c(5,5))
plot(p)
dev.off()
此外,要使图表在使用 source
运行代码时显示,您需要专门绘制xyplot命令的输出,例如
Also, to make the plot appear when running the code using source
, you need to specifically plot the output from the xyplot command, like
p <- xyplot(...)
plot(p)
在控制台上运行时,这不是必需的,因为默认情况下会在其上调用 plot
(实际上是 print
函数).
When running at the console, this is not necessary as the plot
(well, actually, the print
function) is called on it by default.
这篇关于如何在R中拆分数据集和绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!