如何在 R 中分割数据集并绘图

发布于 2024-10-13 05:46:00 字数 1479 浏览 3 评论 0原文

我使用的数据集如下:

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 中为第一列的每个值绘制一个图,即对于上述数据,将有 10 个不同的第三列值图(每组 1-10)。 x 轴是迭代次数,Y 轴是最大 63000 的值。我还需要用红色线连接这些点。 我是 R 新手,一直在阅读文档,但这让我更加困惑。请任何人帮忙。

编辑:我实际上想要 V3 值的折线图。 v3 列的行数将在 x 轴上,v3 值将在 y 轴上。我想要 v1 指示的组的每个不同的图表。 Chase 的解决方案有效,只是我希望轴移动,V3 值应该在 y 轴上。这里是示例 alt text

编辑2:@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

我没有得到任何情节?我错过了什么 好的:明白了。不知道出了什么问题。在这里,

alt text

还有 2 件事,如何将盒子上的 V1 标签更改为实际数字,例如 1,2, ... 其次,我有包含 100 个组的文件,我尝试了一个,它使所有图表都在一页上(显然不可读),我可以在多个窗口上制作这些图表吗?

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

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.

EDIT: I actually want line graph of V3 values. the number of rows of v3 column would be on x-axis and v3 values on y-axis. And I want different graphs each for a group indicated by v1. Chase's solution works except that I want the axis shifted, the V3 values should be on y-axis.here is example
alt text

EDIT2: @Roman, Here is the code I am executing.

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)

This is the error I get,

    > 
>  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,

alt text

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绝不服输 2024-10-20 05:46:00

好吧,首先您需要为第一个变量的每个子集分别创建一个带有行号的变量。这是一种方法,通过第一个变量拆分数据集,创建一个具有行号的新变量,然后重新组合。

您可能还希望 V1 成为一个因子(分类变量)。

d <- do.call(rbind, lapply(split(d, d$V1), function(x) {
    x$iterations <- 1:nrow(x)
    x
}))
d$V1 <- factor(d$V1)

然后使用lattice库,您可以执行类似的操作:

xyplot(V3 ~ iterations | V1, type="l", data=d)

要使绘图显示在多个页面上,请使用layout选项限制页面上的绘图数量。您需要将绘图保存到支持多页输出的文件中才能执行此操作。例如,对于 5 行和 5 列:

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 命令的输出,如

p <- xyplot(...)
plot(p)

在控制台运行时,这不是必需的,因为默认情况下会调用 plot (实际上是 print 函数)。

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.

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)

Then using the lattice library, you'd do something like

xyplot(V3 ~ iterations | V1, type="l", data=d)

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()

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)

When running at the console, this is not necessary as the plot (well, actually, the print function) is called on it by default.

陌伤浅笑 2024-10-20 05:46:00

就像蔡斯所说,请澄清你的问题,以便我们更好地设想你想要实现的目标。为了增加混乱,这里有一个我认为您可能想要的lattice大致解决方案。

library(lattice)
fdt <- data.frame(col1 = seq(from = 1, to = 10, each = 10),
        col2 = round(56 * rnorm(100, mean = 30, sd = 5)),
        col3 = round(20 * rnorm(100, mean = 11,)))
xyplot(col3 ~ 1:100 | col1, data = fdt)

替代文字

Like Chase said, please clarify on your question so that we can envision better what you're trying to achieve. To add to the heap of confusion, here's a lattice ballpark solution of what I think you may be after.

library(lattice)
fdt <- data.frame(col1 = seq(from = 1, to = 10, each = 10),
        col2 = round(56 * rnorm(100, mean = 30, sd = 5)),
        col3 = round(20 * rnorm(100, mean = 11,)))
xyplot(col3 ~ 1:100 | col1, data = fdt)

alt text

孤单情人 2024-10-20 05:46:00

我没有完全遵循您想要绘制的内容,但这里有一种方法应该让您走上正确的道路,您可以填写适当的绘图命令...或澄清您的问题并解释最终结果是什么你的情节应该看起来更详细。

我们将利用两个包:plyrggplot2。我们将使用 plyr 将数据分成适当的组,然后使用 ggplot2 进行实际绘图。我们将利用 pdf() 函数并在每个页面上放置不同的绘图。

library(ggplot2)
library(psych)    #For copying in data, not needed beyond that.

df <- read.clipboard(header = F)

pdf("test.pdf")
    d_ply(df, "V1", function(x)     #Split on the first column
        print(qplot(x$V3))          #Your plotting command should go here. This plots histograms.
    )
dev.off()                           #Close the plotting device.

这将生成一个 n 页 PDF,其中 n 代表 V1(您的拆分列)中的组数。如果您希望有 JPEG 输出,请查看 ?jpeg 或用于制作其他输出的其他图形选项。

编辑:如您所见,人们以多种方式解释您的问题。如果@Roman的解决方案更符合您的需求,这里有大致相同的ggplot代码

qplot(col2, col3, data = fdt, geom = "point") + facet_wrap(~ col1 , nrow = 2)

I'm not exactly following what it is that you want to plot, but here's an approach that should get your down the right path and you can fill in the appropriate plotting command...or clarify your question and explain what the final result of your plot should look like in more detail.

We are going to take advantage of two packages: plyr and ggplot2. We will use plyr to split up your data into the appropriate groups and then use ggplot2 for the actual plotting. We'll take advantage of the pdf() function and put a different plot on each page.

library(ggplot2)
library(psych)    #For copying in data, not needed beyond that.

df <- read.clipboard(header = F)

pdf("test.pdf")
    d_ply(df, "V1", function(x)     #Split on the first column
        print(qplot(x$V3))          #Your plotting command should go here. This plots histograms.
    )
dev.off()                           #Close the plotting device.

This will generate an n page PDF where n represents the number of groups in V1 (your splitting column). If you'd rather have JPEG outputs, look at ?jpeg or the other graphics options for making other outputs.

EDIT: As you can see, people interpreted your question in a few ways. If @Roman's solution is more what you want, here's roughly the same ggplot code

qplot(col2, col3, data = fdt, geom = "point") + facet_wrap(~ col1 , nrow = 2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文