2012-12-11 25 views
2

我有一個籃球比賽的傳球交通數據,我有100+行這表明同一隊玩家之間傳遞,就像這樣:如何在R中玩家之間形象化傳球數據?

Player 
1 21 
2 12 
3 5 
4 21 
5 12 
6 2 
7 12 
8 5 
9 3 
.. 

我想這個可視化的數據,像下面的圖片。在每一行,我都想在玩家之間畫一條線。例如,在第一排之後,球從「選手21」移動到「選手12」,此時,我想在「選手21和選手12」之間劃一條線,方向不重要。

樣本圖像:http://img5.imageshack.us/img5/9885/samplex.gif

我也想給皮膚上色這些行[創建顏色圖例]將國家角色之間有多少通行證發生。例如,如果「玩家12和21」之間的界限是「綠色」,則這意味着根據「彩色圖例」,它們之間的通行在「XXX」之上。

我怎樣才能看到這個?

感謝

+0

可不可以給,我們實際上可以讀取一些樣本數據,例如來'dput(yourData [1:20])'的輸出?你有座標的球員的位置? – Thilo

+0

@Thilo你可以從這裏下載示例:[鏈接](https://www.dropbox.com/s/akqmnmfpg4mqjs9/Sample.csv) 其實我還沒有計算座標,你可以簡單地給隨機值。 – forochelian

回答

4

以下是使用ggplot2的方法。使用來自this post的想法,我們通過將ImageMagick轉換爲*.ppm來添加背景圖像。球員的位置在coords,所以你可能想要改變它們,但由於ylimxlim他們將保持在正確的區域。

enter image description here

library(ggplot2) 
library(pixmap) 

data <- data.frame(Player = c(2, 12, 21, 5, 3, 21, 5, 12, 3, 12, 21, 5)) 
p <- data.frame(Pass1 = data[-nrow(data), ], Pass2 = data[-1, ]) 
p <- apply(p, 1, function(i) paste(sort(i), collapse = " ")) 
p <- factor(table(p)[p]) 
coords <- replicate(2, runif(nrow(unique(data)))) 
xmap <- setNames(coords[,1], unique(data$Player)) 
ymap <- setNames(coords[,2], unique(data$Player)) 
plotData <- data.frame(x = xmap[as.character(data$Player)], 
         y = ymap[as.character(data$Player)], 
         Player = factor(data$Player)) 
plotData <- plotData[rep(1:nrow(plotData), each = 2),] 
plotData <- cbind(plotData[-c(1, nrow(plotData)),], id = rep(p, each = 2)) 
image <- read.pnm("p.ppm") 

as.raster.pixmapRGB <- function(x) { 
    nr <- nrow([email protected]) 
    r <- rgb(([email protected]), ([email protected]), ([email protected])) 
    dim(r) <- [email protected] 
    r 
} 

ggplot(plotData, aes(x = x, y = y, label = Player)) + 
    annotation_raster(image, -Inf, Inf, -Inf, Inf, interpolate = TRUE) + 
    geom_text(vjust = -1, colour = "red") + xlab(NULL) + ylab(NULL) + 
    geom_point(size = 5) + geom_path(aes(colour = id)) + xlim(c(-0.1, 1.1)) + 
    theme(axis.ticks = element_blank(), axis.text = element_blank()) + 
    scale_colour_discrete(name = "Number of passes") + ylim(c(-0.1, 1.1)) 
+0

@ Julius:它給了2個錯誤。第一:文件錯誤(文件打開=「rb」):無法打開連接 另外:警告信息: 在文件(文件,打開=「rb」)中: 無法打開文件'p.ppm':否這個文件或目錄 和第二個: 使用方法錯誤(「as.raster」): 'as.raster' – forochelian

+0

@forochelian沒有適用的方法,你可以找到這個特定的圖像已經轉換[這裏](https:// www.dropbox.com/s/0e7ahsbu30yogd9/p.ppm)。如果您想使用另一臺ImageMagick軟件,則必須使用該ImageMagick軟件。 – Julius

+0

@ Julius:謝謝Julius,它運作得非常好。 – forochelian

1

詹姆斯Kierstead具有https://github.com/bobthecat/codebox/blob/master/table.graph.r

森悠在http://www.jameskeirstead.ca/r/slopegraphs-in-r/

大衛Ruau使用R的slopegraph [R程序slopegraph例如,在他的書中可視化這,有一個很好的使用斜坡圖的樣本。如果我沒有記錯(他現在沒有這本書),他使用R.

不幸的是,我無法找到使用R的任何和絃圖(參見下文)。


Java選項

本弗萊有slopegraph demo using Java/Processing


JavaScript選項

如果你願意嘗試的JavaScript,在使用幾個選項D3

參見D3 gallery用於其它更多的實施例。