在Hexagon Binning(ggplot2)中同时使用颜色和大小属性 [英] Using both color and size attributes in Hexagon Binning (ggplot2)

查看:205
本文介绍了在Hexagon Binning(ggplot2)中同时使用颜色和大小属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够制作一些图表来展示一些NBA球员和球队的投篮倾向/效果。我想将六边形的格式设置如下:大小将表示镜头的数量,颜色将表示该位置的相对效率(点/次)。 。我得到这个阴谋。你需要玩 minarea 和``maxarea 参数来定义重叠区域。我还使用 grid.raster`将图像添加为abckground。我没有情节技巧,所以我从他的网上选择一个,但你可以使用这种技术来添加一个地面。我相信你可以做得更好。

 库(lattice)
库(hexbin)
库(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, ID = TRUE)
mtrans< -hexTapply(hbin,dat $ Points,sum,na.rm = TRUE)
cols< - 彩虹(4)
grid.hexagons(hbin,style ='lattice',
,minarea = 0.1,maxarea = 50,colorcut = c(0,.6,1),
border = NA,
pen = cols [mtrans + 1] )
})


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:

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?

解决方案

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

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

这篇关于在Hexagon Binning(ggplot2)中同时使用颜色和大小属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆