这是一种使用ggplot2
. 使用这篇文章*.ppm
中的想法,我们通过使用 ImageMagick将其转换为添加背景图像。球员的位置在coords
,所以你可能想改变他们,但因为ylim
他们xlim
会留在正确的区域。
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(x@red)
r <- rgb((x@red), (x@green), (x@blue))
dim(r) <- x@size
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))