我有一张初始速度,发射角度和棒球射弹结果的表格,如下所示:
hit_speed hit_angle events
1 87.44 44.96 Double
2 83.53 8.57 Groundout
3 97.30 0.67 Single
4 78.11 36.01 Flyout
5 63.54 -26.46 Groundout
6 90.86 35.31 Flyout
对于这些中的每一个,我想像这样计算棒球的轨迹:
feetPerSec <- hit_speed * 5280 / (60 * 60) # Convert mph to ft/s
radians <- hit_angle * pi / 180
x <- 0:500
y = 2.5 + x*tan(radians) - (32.174 * x^2)/(2*(feetPerSec*cos(radians))^2)
一旦我为每个棒球的轨迹设置了x和y坐标,我想将它们全部绘制在与每个唯一的&#34;事件&#34; (单,双,基础等)作为不同的颜色。
我知道如何绘制单个轨迹的图形,但我无法在同一图表上使用不同颜色的多个轨迹来完成每个结果。有什么想法吗?
编辑:我还应该指定我想绘制线条,而不是点。
答案 0 :(得分:1)
row hit_speed hit_angle events
1 87.44 44.96 Double
2 83.53 8.57 Groundout
3 97.30 0.67 Single
4 78.11 36.01 Flyout
5 63.54 -26.46 Groundout
6 90.86 35.31 Flyout
mydata <- read.table(con <- file("clipboard"), header = T)
feetPerSec <- mydata$hit_speed * 5280 / (60 * 60) # Convert mph to ft/s
radians <- mydata$hit_angle * pi / 180
x <- 0:500
y = 2.5 + x*tan(radians) - (32.174 * x^2)/(2*(feetPerSec*cos(radians))^2)
mydata$colors <- NA
mydata$colors[mydata$events == "Groundout"] <- "blue"
mydata$colors[mydata$events == "Flyout"] <- "purple"
mydata$colors[mydata$events == "Single"] <- "orange"
mydata$colors[mydata$events == "Double"] <- "red"
plot(x,y,col=mydata$colors)