在R中对点的一部分划线

时间:2011-10-04 20:51:22

标签: r plot line

我有一组包含三列的数据。前两个是年(年)和胜率(WP)。第三列是对手(对手)获胜的百分比。样本中只有两个对手,因此每年有两个胜率。

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

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

1 个答案:

答案 0 :(得分:1)

正如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.

希望这有帮助。