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

前端 未结 1 1098
离开以前
离开以前 2020-12-16 12:54

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 wil

1条回答
  •  清酒与你
    2020-12-16 13:24

    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

    0 讨论(0)
提交回复
热议问题