在 R 中的部分点上画一条线

发布于 2024-12-08 07:51:47 字数 697 浏览 0 评论 0原文

我有一组包含三列的数据。前两项是年份(Year)和胜率(WP)。第三栏是所取得的胜率的对手(Oponent)。样本中只有两个对手,因此每年有两个胜率。

我使用以下代码绘制了数据并由对手对点进行了着色:

plot(doc$WP~doc$Year, col=doc$Opponent)

我还想在对手的绘图中添加线条,因此我将在数据中绘制两条线。一个是按年份对第一个对手的胜率,一个是按年份对第二个对手的胜率。

我尝试使用此代码添加行:

lines(doc$WP[doc$Opponent=="N"] ~ doc$Year[doc$Opponent=="N"], col="grey", lwd=2)

我不知道为什么,但我的图表上没有显示任何内容。我也没有收到错误消息。

你能不能按照我尝试过的方式创建按数据分组的行,或者(更有可能)我错过了一些东西。

感谢您的帮助!

以下是数据示例:

Year     WP        Opponent
2001     .544        N
2002     .528        N
2003     .463        N
2001     .621        E
2002     .543        E
2003     .487        E

I have a set of data with three columns. The first two are year (Year) and winning percentage (WP). The third column is the opponent (Opponent) the winning percentage was achieved against. There are only two opponents in the sample, so for each year there are two winning percentages.

I plotted the data and colored the points by opponent using the following code:

plot(doc$WP~doc$Year, col=doc$Opponent)

I would also like to add lines to the plot by opponent, so I would have two lines through the data. One for winning percentage against the first opponent by year, and one for the second opponent by year.

I tried using this code to add the line:

lines(doc$WP[doc$Opponent=="N"] ~ doc$Year[doc$Opponent=="N"], col="grey", lwd=2)

I don't know why, but nothing shows up on my graph. I don't get an error message either.

Can you not create lines grouped by the data in the way I have tried, or (more likely) have I just missed something.

Thanks for any help!

Here is a sample of the data:

Year     WP        Opponent
2001     .544        N
2002     .528        N
2003     .463        N
2001     .621        E
2002     .543        E
2003     .487        E

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

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

发布评论

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

评论(1

老子叫无熙 2024-12-15 07:51:47

正如 Brian 指出的,这可能与 csv 文件有关 - 一些 csv 文件(在 Windows 下,从 Excel 导出)有一个“;”而不是“,”。这与使用的区域设置有关,并且在某些国家/地区,小数点分隔符是“,”,这会导致用“,”分隔的不可读的csv文件(除非数字被引用,但这会导致其他问题)。

尝试 R 中的 read.csv2() 函数来读取您的数据 - 也许可行。
来自 read.csv2 的 r-help:

 ‘read.csv’ and ‘read.csv2’ are identical to ‘read.table’ except
 for the defaults.  They are intended for reading ‘comma separated
 value’ files (‘.csv’) or (‘read.csv2’) the variant used in
 countries that use a comma as decimal point and a semicolon as
 field separator.  Similarly, ‘read.delim’ and ‘read.delim2’ are
 for reading delimited files, defaulting to the TAB character for
 the delimiter.  Notice that ‘header = TRUE’ and ‘fill = TRUE’ in
 these variants, and that the comment character is disabled.

希望这有帮助。

As Brian pointed out, this might have to do with the csv file - some csv files (under windows, exported from excel) have a ";" instead of th ",". This has to do with the locale used and the fact that in some countries, the decimal separator is a ",", which would lead to unreadable csv files separated with "," (unless the numbers are quoted, but this would cause other problems).

try the read.csv2() function in R to read your data - maybe that works.
From the r-help on read.csv2:

 ‘read.csv’ and ‘read.csv2’ are identical to ‘read.table’ except
 for the defaults.  They are intended for reading ‘comma separated
 value’ files (‘.csv’) or (‘read.csv2’) the variant used in
 countries that use a comma as decimal point and a semicolon as
 field separator.  Similarly, ‘read.delim’ and ‘read.delim2’ are
 for reading delimited files, defaulting to the TAB character for
 the delimiter.  Notice that ‘header = TRUE’ and ‘fill = TRUE’ in
 these variants, and that the comment character is disabled.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文