R:是否可以将格子xy图与ggplot结合起来?

时间:2016-05-31 18:20:25

标签: r ggplot2 lattice

我试图创建一个篮球投篮图,我已经开始享受格子投影图的外观,但似乎我无法结合(或者,放在上面这与我的ggplot法院设计。反正有没有这样做?或者我必须最终使用球场作为背景中的光栅图像(由于猜测和检查图像的坐标,这已经让我适合了吗?)

以下是dat数据框

的示例
      location_x location_y points
1         5.5       22.1      2
2         6.0       24.1      2
3         6.1       39.4      2
4        25.3       38.7      0
5         8.0       24.4      2
6         8.8       12.7      0
7         9.0        6.0      0
8        10.6       31.6      0
9         4.2       47.5      0
10        4.3        8.9      2

编辑:以下是我的代码,感谢帖子的帮助:Using both color and size attributes in Hexagon Binning (ggplot2)。 geom_path行是用于绘制法庭的内容。我目前得到的错误是:

Error: Don't know how to add o to a plot

dat = mod_shots[mod_shots$player_id == 398068, ]

lattice_plot = xyplot(location_x~location_y, data = dat, panel = function(x,y,...)
{
  hbin = hexbin(dat$location_y, dat$location_x, xbins=40, IDs=TRUE)
  mtrans = hexTapply(hbin, dat$points, sum, na.rm=TRUE)
  cols = rev(heat.colors(7))
  grid.hexagons(hbin, style='lattice',
                minarea=0.1,maxarea=3,
                border="NA",
                pen=cols[mtrans+1])
})

ggplot() + geom_path(data = court_points,
          aes(x = x, y = y, z = NULL, group = desc, linetype = dash),
          color = "#000004") + lattice_plot

1 个答案:

答案 0 :(得分:1)

这是一个可疑的策略:将格子面板作为注释层放在ggplot中。我已将格子图层设为半透明图示(烧录混合模式)。

enter image description here

library(grid) 
library(lattice) 
library(ggplot2) 

ghost_grob <- function(m, ...){
  grob(m=m, cl="ghost") 
}

preDrawDetails.ghost <- function(x){
  pushViewport(viewport(xscale = c(0, 1 + ncol(x$m)), yscale = 
                          c(0, 1 + nrow(x$m))))
}
postDrawDetails.ghost <- function(x){
  upViewport(1)
}

drawDetails.ghost <- function(x, recording){
  panel.levelplot(col(x$m), row(x$m), x$m, alpha.regions = 0.5,
                  subscripts=TRUE, at = do.breaks(range(x$m), 30)) 
}

grid.newpage() 
gpanel <- ghost_grob(volcano)
# gpanel <- rectGrob()
# grid.draw(gpanel)

ggplot(faithfuld, aes(waiting, eruptions)) +
  theme_minimal() + 
  scale_x_continuous(expand=c(0,0)) +
  scale_y_continuous(expand=c(0,0)) +
  geom_raster(aes(fill = density), interpolate = TRUE) +
  scale_fill_gradient(low = "white", high="black") +
  annotation_custom(gpanel, xmin = -Inf, xmax=Inf, ymin=-Inf, ymax=Inf)

添加一层六边形需要了解hexbin提供的网格功能。我不知道如何使用它们,所以我只是根据?hexpolygon改编了一个例子来说明。显然,尺度和纵横比在这里是荒谬的。

ghost_grob <- function(x=runif(20, -2, 2), y=x + rnorm(20), ...){
  grob(x=x, y=y, cl="ghost") 
}

preDrawDetails.ghost <- function(x){
  addBit <- function(bnds, f = 0.05) bnds + c(-f, f) * diff(bnds)
  sc <- addBit(rxy <- range(x$x, x$y))
  pushViewport(plotViewport(.1+c(4,4,2,1), xscale = sc, yscale = sc))
}
postDrawDetails.ghost <- function(x){
  upViewport(1)
}

drawDetails.ghost <- function(x, recording){
  require(hexbin)
  hexpolygon(x$x,x$y, hexcoords(dx = 0.1, sep=NULL), border = "blue", fill=NA)
}


grid.newpage() 
gpanel <- ghost_grob()
# gpanel <- rectGrob()
# grid.draw(gpanel)

ggplot(faithfuld, aes(waiting, eruptions)) +
  theme_bw() + 
  scale_x_continuous(expand=c(0,0)) +
  scale_y_continuous(expand=c(0,0)) +
  geom_raster(aes(fill = density), interpolate = TRUE) +
  scale_fill_gradient(low = "white", high="black") +
  annotation_custom(gpanel, xmin = -Inf, xmax=Inf, ymin=-Inf, ymax=Inf) 

enter image description here