ggplot2 - 将两个ggplots合并为一个图形。击球区和球场

时间:2016-04-22 03:28:11

标签: r ggplot2

我有两个这样的ggplots:

strike_zones <- data.frame(
  x1 = rep(-1.5:0.5, each = 3),
  x2 = rep(-0.5:1.5, each = 3),
  y1 = rep(1.5:3.5, 3),
  y2 = rep(2.5:4.5, 3),
  z = factor(c(7, 4, 1, 8, 5, 2, 9, 6, 3))
  )

 pitcher <- data.frame(
 px = c(1.5,2.4, 0, 2),
 pz = c(3,3,3,4.1)
 )

 # Plot example of PITCHf/x strike zone regions
 p <- ggplot() +
 xlim(-4, 4) + xlab("") +
 ylim(0, 6.8) + ylab("") +
 geom_rect(data = strike_zones,
        aes(xmin = x1, xmax = x2, ymin = y2, ymax = y1), color = "grey") +     theme_bw() + theme(legend.position = "none")

q <- ggplot(pitcher, aes(x=px,y=pz,color=description)) + geom_point(shape=1)

基本上我试图在击球区域上绘制球场位置(px为x,pz为y)。它不会让我给我这样的信息:

  Error in p + o : non-numeric argument to binary operator

另外:警告信息: &#34; +&#34;不兼容的方法(&#34; + .gg&#34;,&#34; Ops.data.frame&#34;)

我该怎么办?

1 个答案:

答案 0 :(得分:2)

不是将整个q添加到绘图p,而是将几何对象添加到绘图p,如此

p + geom_point(data = pitcher, aes(x= px, y =pz), shape = 1)