在 R 中的部分点上画一条线
我有一组包含三列的数据。前两项是年份(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 Brian 指出的,这可能与 csv 文件有关 - 一些 csv 文件(在 Windows 下,从 Excel 导出)有一个“;”而不是“,”。这与使用的区域设置有关,并且在某些国家/地区,小数点分隔符是“,”,这会导致用“,”分隔的不可读的csv文件(除非数字被引用,但这会导致其他问题)。
尝试 R 中的 read.csv2() 函数来读取您的数据 - 也许可行。
来自 read.csv2 的 r-help:
希望这有帮助。
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:
Hope this helps.