Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
597 views
in Technique[技术] by (71.8m points)

r - Using both color and size attributes in Hexagon Binning (ggplot2)

I am hoping to construct some charts to display the shooting tendencies/effectiveness of some NBA players and teams. I would like to format the hexagons as follows: size will represent the number of shots and color will represent the relative efficiency (pts/attempt) from that location. Here is a great example of what I'm looking for, created by Kirk Goldsberry:

Ray Allen, by Kirk Goldsberry

I have been able to use hexbins and hexTapply to achieve something close to the desired result, but the shapes are circles. Here is my code (which includes sample data):

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")

Another way to think about it would be to coloring the hexagons in plot(h, style="lattice") by pts/attempt -- but I'm not sure how to do that, either.

Is there a way to get this graph with hexagons rather than circles?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First thank you for this question and for sharing this plot with great imagination!

Here a attempt using lattice package. Mainly I implement you idea of : coloring the hexagons in plot(h, style="lattice") by pts/attempt". The use of lattice is also motivated by the fact that you can use grid functions within the lattice panel functions( to draw the ground details for example)

I generate some data

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

here the plot:

    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))
   })

enter image description here

EDIT Using OP real data. I get this plot. You need to play with minarea and ``maxareaargument to define overlapping regions. I add also an image as abckground usinggrid.raster`. I don't have plot skills so I choose one from he net, but you can use this technique to add a ground. I am sure you can do a better image.

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])
})

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...