热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何在R中分割数据集和绘图-HowtoSplitDatasetandplotinR

Iamusingadatasetlike:我使用的数据集如下:14843414566156711628915882641742566266374

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.

我需要在R中为第一列的每个值绘制一个图,即对于上面的数据,会有10个不同的图(每组1-10)为第三列的值。x轴是迭代次数,y轴是最大63000的值。我还需要用红色的线把这些点连起来。我是R的新手,一直在阅读文档,但这让我更加困惑。任何人都能帮忙吗?

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

编辑:实际上我想要V3值的线图。v3列的行数在x轴上,v3值在y轴上。我要用不同的图来表示v1表示的基团。Chase的解决方案是可行的,只是我想要平移轴,V3的值应该在y轴上。下面是例子

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

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

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?

还有2件事,如何将盒子上的V1标签更改为1、2、……其次,我有包含100个组的文件,我试过一个,它让所有的图形都在一个页面上(显然不能读),我可以在不止一个窗口上做这些吗?

3 个解决方案

#1


2  

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

你也可能希望V1是一个因子(一个分类变量)。

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:

要使这些图出现在一个以上的页面上,使用布局选项限制一个页面上的绘图数量。您将需要将绘图保存到支持多页输出的文件中,以实现这一点。例如,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()

Also, to make the plot appear when running the code using source, you need to specifically plot the output from the xyplot command, like

此外,要使代码在使用源代码运行时显示图表,您需要特别绘制xyplot命令的输出,如

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.

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

#2


1  

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

#3


0  

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.

我们将利用两个包:plyr和ggplot2。我们将使用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.

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.

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

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

编辑:正如你所看到的,人们用几种方式来解释你的问题。如果@Roman的解决方案更符合您的需要,这里有大致相同的ggplot代码

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

推荐阅读
  • 拥抱Android Design Support Library新变化(导航视图、悬浮ActionBar)
    转载请注明明桑AndroidAndroid5.0Loollipop作为Android最重要的版本之一,为我们带来了全新的界面风格和设计语言。看起来很受欢迎࿰ ... [详细]
  • IjustinheritedsomewebpageswhichusesMooTools.IneverusedMooTools.NowIneedtoaddsomef ... [详细]
  • REVERT权限切换的操作步骤和注意事项
    本文介绍了在SQL Server中进行REVERT权限切换的操作步骤和注意事项。首先登录到SQL Server,其中包括一个具有很小权限的普通用户和一个系统管理员角色中的成员。然后通过添加Windows登录到SQL Server,并将其添加到AdventureWorks数据库中的用户列表中。最后通过REVERT命令切换权限。在操作过程中需要注意的是,确保登录名和数据库名的正确性,并遵循安全措施,以防止权限泄露和数据损坏。 ... [详细]
  • 使用C++编写程序实现增加或删除桌面的右键列表项
    本文介绍了使用C++编写程序实现增加或删除桌面的右键列表项的方法。首先通过操作注册表来实现增加或删除右键列表项的目的,然后使用管理注册表的函数来编写程序。文章详细介绍了使用的五种函数:RegCreateKey、RegSetValueEx、RegOpenKeyEx、RegDeleteKey和RegCloseKey,并给出了增加一项的函数写法。通过本文的方法,可以方便地自定义桌面的右键列表项。 ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • 本文介绍了三种方法来实现在Win7系统中显示桌面的快捷方式,包括使用任务栏快速启动栏、运行命令和自己创建快捷方式的方法。具体操作步骤详细说明,并提供了保存图标的路径,方便以后使用。 ... [详细]
  • 也就是|小窗_卷积的特征提取与参数计算
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了卷积的特征提取与参数计算相关的知识,希望对你有一定的参考价值。Dense和Conv2D根本区别在于,Den ... [详细]
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • 本文介绍了在Windows系统上使用C语言命令行参数启动程序并传递参数的方法,包括接收参数程序的代码和bat文件的编写方法,同时给出了程序运行的结果。 ... [详细]
  • 本文介绍了Swing组件的用法,重点讲解了图标接口的定义和创建方法。图标接口用来将图标与各种组件相关联,可以是简单的绘画或使用磁盘上的GIF格式图像。文章详细介绍了图标接口的属性和绘制方法,并给出了一个菱形图标的实现示例。该示例可以配置图标的尺寸、颜色和填充状态。 ... [详细]
  • 本文介绍了一种轻巧方便的工具——集算器,通过使用集算器可以将文本日志变成结构化数据,然后可以使用SQL式查询。集算器利用集算语言的优点,将日志内容结构化为数据表结构,SPL支持直接对结构化的文件进行SQL查询,不再需要安装配置第三方数据库软件。本文还详细介绍了具体的实施过程。 ... [详细]
  • 本文详细介绍了Android中的坐标系以及与View相关的方法。首先介绍了Android坐标系和视图坐标系的概念,并通过图示进行了解释。接着提到了View的大小可以超过手机屏幕,并且只有在手机屏幕内才能看到。最后,作者表示将在后续文章中继续探讨与View相关的内容。 ... [详细]
  • 带添加按钮的GridView,item的删除事件
    先上图片效果;gridView无数据时显示添加按钮,有数据时,第一格显示添加按钮,后面显示数据:布局文件:addr_manage.xml<?xmlve ... [详细]
author-avatar
轻点指尖划过秋天的一抹痕8023意
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有