I am using R to plot baseball spray charts from play-by-play data. I have used the following command to plot the diamond: plot (0:250, -250:0, type="n", bg="white") lines(c(125,150,125,100,125),c(-210,-180,-150,-180,-210), col=c("black")) I have also plotted different hit locations using commands such as the following: points(subset(framename$hit_x, framename$hit_traj=="line_drive"), subset(-framename$hit_y, framename$hit_traj=="line_drive"), pch=20, col=c("red")) My question: Is there any easy way to plot a line from the origin (home plate) to each point on the graph? Preferably the line would share the same color as the dot that denotes where the ball landed. I have tried searching Google and these forums, and most graphing questions have to do with scatterplots or other varieties of graphs I am not using. Thanks very much in advance. -Jason -- View this message in context: http://www.nabble.com/Plotting-lines-to-sets-of-points-tf4404235.html#a12564704 Sent from the R help mailing list archive at Nabble.com.
# Create a matrix of ball locations # You'd do this using the calls within your points function balls <- matrix(c(0,50,25,-150,-100,-50), ncol=2, byrow=F) # Draw a line from the origin to each ball location apply(balls, 1, function(x) lines(c(125, x[1]), c(-210, x[2]), col='red')) A more complete example might loop over all the unique elements of framename$hit_traj, and then run this procedure for each value with a different colour, plotting both ball points and trajectories. lawnboy34 wrote:> > I am using R to plot baseball spray charts from play-by-play data. I have > used the following command to plot the diamond: > > plot (0:250, -250:0, type="n", bg="white") > lines(c(125,150,125,100,125),c(-210,-180,-150,-180,-210), col=c("black")) > > I have also plotted different hit locations using commands such as the > following: > > points(subset(framename$hit_x, framename$hit_traj=="line_drive"), > subset(-framename$hit_y, framename$hit_traj=="line_drive"), pch=20, > col=c("red")) > > My question: Is there any easy way to plot a line from the origin (home > plate) to each point on the graph? Preferably the line would share the > same color as the dot that denotes where the ball landed. I have tried > searching Google and these forums, and most graphing questions have to do > with scatterplots or other varieties of graphs I am not using. Thanks very > much in advance. > > -Jason >-- View this message in context: http://www.nabble.com/Plotting-lines-to-sets-of-points-tf4404235.html#a12564959 Sent from the R help mailing list archive at Nabble.com.
?segments On 9/7/07, lawnboy34 <jay.pare at gmail.com> wrote:> > I am using R to plot baseball spray charts from play-by-play data. I have > used the following command to plot the diamond: > > plot (0:250, -250:0, type="n", bg="white") > lines(c(125,150,125,100,125),c(-210,-180,-150,-180,-210), col=c("black")) > > I have also plotted different hit locations using commands such as the > following: > > points(subset(framename$hit_x, framename$hit_traj=="line_drive"), > subset(-framename$hit_y, framename$hit_traj=="line_drive"), pch=20, > col=c("red")) > > My question: Is there any easy way to plot a line from the origin (home > plate) to each point on the graph? Preferably the line would share the same > color as the dot that denotes where the ball landed. I have tried searching > Google and these forums, and most graphing questions have to do with > scatterplots or other varieties of graphs I am not using. Thanks very much > in advance. > > -Jason > -- > View this message in context: http://www.nabble.com/Plotting-lines-to-sets-of-points-tf4404235.html#a12564704 > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
see ?segments. And you can tidy up your subsetting with some with's while you're at it, although a function would be nicer still. Looking at your code, you probably need something like with(subset(framename, hit_traj=="line_drive"), segments(rep(125,length(hit_x)) , rep(-210,length(hit_y)), hit_x, hit_y, col="blue") ) #You can put it all on one line for convenience of repetition but its not as readable with(subset(framename, hit_traj=="line_drive"), points(hit_x, hit_y, pch=20, col="red") ) btw: seems an odd coordinate system. Why not put the hitter at the origin? Steve E>>> lawnboy34 <jay.pare at gmail.com> 08/09/2007 00:01:22 >>>I am using R to plot baseball spray charts from play-by-play data. I have used the following command to plot the diamond: plot (0:250, -250:0, type="n", bg="white") lines(c(125,150,125,100,125),c(-210,-180,-150,-180,-210), col=c("black")) I have also plotted different hit locations using commands such as the following: points(subset(framename$hit_x, framename$hit_traj=="line_drive"), subset(-framename$hit_y, framename$hit_traj=="line_drive"), pch=20, col=c("red")) My question: Is there any easy way to plot a line from the origin (home plate) to each point on the graph? Preferably the line would share the same color as the dot that denotes where the ball landed. I have tried searching Google and these forums, and most graphing questions have to do with scatterplots or other varieties of graphs I am not using. Thanks very much in advance. -Jason -- View this message in context: http://www.nabble.com/Plotting-lines-to-sets-of-points-tf4404235.html#a12564704 Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. ******************************************************************* This email and any attachments are confidential. Any use, co...{{dropped}}