开发者

Making a Line Over Portion of Points in R

开发者 https://www.devze.com 2023-04-10 10:32 出处:网络
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 o

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号