I'm looking to build a heat map of tickets sold and apply it over the actual image of the stadium.
Here is my current code:
# plot with picture as layer
library(ggplot2)
library(magick)
#download picture of the football pitch
image_url <- "https://www.kindpng.com/picc/m/202-2026208_soccer-field-coloring-page-png-download-circle-transparent.png"
pic <- image_read(image_url)
#Build the dataset which has the geometry for the different blocks
ids <- factor(c("block_1",
"block_2",
"block_3",
"block_4",
"block_5"))
values <- data.frame(
id = ids,
value = c(3,
4,
2,
1,
10)
)
#input the coordinates for the polygons
positions <- data.frame(
id = rep(ids, each = 4),
x = c(1, 3, 3,1, #block1
3.75, 6.25, 6.25, 3.75, #block2
3.75, 6.25, 6.25, 3.75, #ect..
2,3,3,2,
7,8,8,7),
y = c(10, 10, 8, 8,
9, 9, 8,8,
2, 2, 1,1,
6.5,6.5,3.5,3.5,
6.5,6.5,3.5,3.5)
)
datapoly <- merge(values, positions, by = c("id"))
#plot out the results
ggplot(datapoly, aes(x = x, y = y)) +
annotation_raster(pic, xmin = 3, xmax =7, ymin = 8, ymax = 2) +
geom_polygon(aes(fill = value, group = id,colour=id)) +
scale_x_continuous(limits = c(0,10),breaks = c(seq(1,10,1))) +
scale_y_continuous(limits = c(0,10),breaks = c(seq(1,10,1))) +
geom_point(alpha=0) +theme_light()+theme(aspect.ratio = 0.75,
panel.grid.minor = element_line(color = 2,
size = 0.25,
linetype = 1),
panel.grid.major = element_line(color = 1,
size = 0.25,
linetype = 1))
Having read into the a few similar questions like this one, I've decided to plot out each of my polygons and fill them one at a time.
My question is I feel that this is a very long way round the problem and if I need to make any changes or change the stadium it will be very time consuming.
Is there a better way of doing this if sf
? I would really prefer to have an output like an actual stadium, like this.