我在网球场上有一些分散的位置,例如:
x y
20 22
30 41
20 89
网球场图像转换为100x100
我想在网球场的图像上叠加的热图,以指出出现次数最多和发生次数最少的区域
示例:
答案 0 :(得分:3)
您可以创建平滑的梯度热图,而不是平铺的热图。这是一个用您的网球场图像和一些废话数据制作的示例。
示例代码:
#load the libraries
library(ggplot2)
library(png)
#load the image of the tennis court
backgroundImage <- readPNG("tennis_court.png")
#load your data
#the data used in this example would be a .csv file with "x" and "y" columns
data <- read.csv("tennis_data.csv", header = TRUE, sep = ",", stringsAsFactors = FALSE)
#make the plot
ggplot(data, aes(x,y)) + #the 'x' and 'y' here reference the column names in your .csv file
annotation_raster(backgroundImage, xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) +
stat_density2d(geom = "polygon", aes(fill=..level.., alpha=..level..)) +
scale_alpha_continuous(range=c(0.05,0.4)) + #range values adjust opacity of the overlay
scale_fill_gradient(low="blue",high="red") +
scale_x_continuous(limits=c(0,dim(backgroundImage )[2]),expand=c(0,0)) +
scale_y_continuous(limits=c(0,dim(backgroundImage )[1]),expand=c(0,0)) +
ggtitle("Tennis court heatmap") + #you can put a title here
#geom_point(colour="red",alpha=0.01)+ #uncomment this line to show the individual data points
coord_fixed()