18

一部の NBA プレーヤーとチームのシュート傾向/有効性を表示するために、いくつかのチャートを作成したいと考えています。六角形を次のようにフォーマットしたいと思います。サイズはショットの数を表し、色はその場所からの相対的な効率 (ポイント/試行) を表します。Kirk Goldsberry によって作成された、私が探しているものの良い例を次に示します。

レイ・アレン、カーク・ゴールズベリー画

hexbinsand を使用しhexTapplyて、目的の結果に近いものを達成することができましたが、形状は円です。これが私のコードです(サンプルデータが含まれています):

library(hexbin); library(ggplot2)
df <- read.table(text="xCoord yCoord   pts
11.4     14.9     2
2.6       1.1      0
4.8       4.1      2
-14.4    8.2      2
4.2       0.3      0
0.4       0.0     2
-23.2   -1.1      3", header=TRUE)
h <- hexbin (x=df$xCoord, y = df$yCoord, IDs = TRUE, xbins=50)
pts.binned <- hexTapply (h, df$pts, FUN=mean)

df.binned <- data.frame (xCoord  = h@xcm, 
          yCoord  = h@ycm, FGA = h@count, pts = pts.binned)

chart.player <- ggplot (df.binned, aes (x =xCoord , 
                  y =yCoord , col = pts, size = FGA)) + coord_fixed() + 
geom_point()  + scale_colour_gradient("Points/Attempt", low = "green", high="red")

それについて考える別の方法はplot(h, style="lattice")、ポイント/試行で六角形を色付けすることです-しかし、それを行う方法もわかりません。

円ではなく六角形でこのグラフを取得する方法はありますか?

4

1 に答える 1

9

まず、この質問に感謝し、素晴らしい想像力でこのプロットを共有していただきありがとうございます!

ここでlatticeパッケージを使用してみます。主に私はあなたの考えを実装します: plot(h, style="lattice") の六角形を pts/attempt で色付けしますgrid。地面の詳細など)

私はいくつかのデータを生成します

dat <- data.frame(
  xCoord = round(runif(1000,-30,30),2),
  yCoord = round(runif(1000,-2,30),2),
  pts = sample(c(0,2,3),100,rep=T))
#dat$pts[dat$xCoord <0 & dat$yCoord] <- 3

ここでプロット:

    xyplot(yCoord~xCoord,data =dat , panel = function(x,y,...)
   {
     hbin<-hexbin(dat$xCoord,dat$yCoord,xbins=50,IDs=TRUE)
     mtrans<-hexTapply(hbin,dat$pts,sum,na.rm=TRUE)
     cols <- rainbow( 4,alpha=0.5)
     grid.hexagons(hbin, style='lattice',
                   ,minarea=0.5,maxarea=5,colorcut=c(0,.6,1),
                   border=NA,
                   pen=cols[mtrans+1])
     ## Now you can get fun to draw the ground here
     ## something like...
     ## grid.circle(gp=gpar(fill=NA))
   })

ここに画像の説明を入力

編集OP 実データを使用。私はこのプロットを取得します。minareamaxarea grid.rasterで遊ぶ必要がありますargument to define overlapping regions. I add also an image as abckground using。私はプロットスキルを持っていないので、彼のネットから1つを選択しますが、このテクニックを使用して地面を追加できます. より良いイメージを実現できると確信しています。

library(lattice)
library(hexbin)
library(png)
xyplot(locationY~locationX,data =dat , panel = function(x,y,...)
{
    ## imgae bakground
    m <- readPNG('basket.png')
    rimg <- as.raster(m)
    grid.raster(rimg, x=0, y=61.5, just="top", width=50,
              default.units = "native")
    panel.fill(col=rgb(1,1,1,alpha=0.8))

    hbin<-hexbin(dat$locationX,dat$locationY,xbins=50,IDs=TRUE)
    mtrans<-hexTapply(hbin,dat$Points,sum,na.rm=TRUE)
    cols <- rainbow(4)
    grid.hexagons(hbin, style='lattice',
                  ,minarea=0.1,maxarea=50,colorcut=c(0,.6,1),
                  border=NA,
                  pen=cols[mtrans+1])
})

ここに画像の説明を入力

于 2013-02-13T04:46:11.493 に答える