Convex Hull in R

library(readr)
library(ggplot2)
library(gridExtra)
library(ggforce)
library(ggExtra)


setwd("~/M4_Campus_Big_Data/3_Ejercicios_Resueltos/Ejercicios_resueltos_R/data")


full_data <- read_delim("full_data.txt", ";", 
						locale = locale(decimal_mark = ","),
						trim_ws = TRUE)
View(full_data)

jugador_elegido <- c("Modric")

rival_elegido <- c("Barcelona")

partido_elegido <- c("1")

evento_elegido <- c("Passes_All")
data_player <- subset(full_data, nombre_jugador == jugador_elegido & 
						Rival == rival_elegido & 
						Local == partido_elegido & 
						Sección == evento_elegido)


player_scatter_plot <- ggplot() +
 
  geom_point(data_player, mapping = aes(x = x1 - 58.015,
										y = y1,
										fill = factor(data_player$Resultado)),
             shape = 21,
             size = 5) +
  
  guides(fill = guide_legend(title = NULL)) +
  
  geom_rect(mapping = aes(xmin = 0.0, xmax = 596.97, 
						  ymin = 50.5, ymax = 446.5),
						  color ="#00529f", fill = NA, alpha = 0.1) +
  geom_rect(mapping = aes(xmin = 0.0, xmax = 91.935, 
						  ymin = 128.975, ymax = 368.025), 
						  color ="#00529f", fill = NA, alpha = 0.1) +
 
  geom_rect(mapping = aes(xmin = 0.0, xmax = 15.0,
              ymin = 50.5, ymax = 65.5),
              color ="#00529f", fill = NA, alpha = 0.1) +
  #Rectangulo izquierda arriba
  geom_rect(mapping = aes(xmin = 0.0, xmax = 15.0,
                          ymin = 431.5, ymax = 446.5),
            color ="#00529f", fill = NA, alpha = 0.1) +
  #Rectangulo derecha abajo
  geom_rect(mapping = aes(xmin = 581.97, xmax = 596.97, 
                        ymin = 50.5, ymax = 65.5), 
          color ="#00529f", fill = NA, alpha = 0.1) +

  geom_rect(mapping = aes(xmin = 581.97, xmax = 596.97, 
                          ymin = 431.5, ymax = 446.5),
            color ="#00529f", fill = NA, alpha = 0.1) +
  geom_rect(mapping = aes(xmin = 505.035, xmax = 596.97, 
						  ymin = 128.975, ymax = 368.025), 
						  color ="#00529f", fill = NA, alpha = 0.1) +
  geom_rect(mapping = aes(xmin = 0.0, xmax = 29.858, 
							ymin = 195, ymax = 302.0), 
							color ="#00529f", fill = NA, alpha = 0.1) +
  geom_rect(mapping = aes(xmin = 567.112, xmax = 596.97, 
						ymin = 195, ymax = 302.0), 
						color ="#00529f", fill = NA, alpha = 0.1) +
						
 
  geom_linerange(aes(x = 298.485, ymin = 50.5, 
						ymax = 446.5), 
						color = "#00529f") +
  geom_point(mapping = aes(x = 66.33, y = 248.5), 
							size = 1, color = "#00529f") +
  geom_point(mapping = aes(x = 530.64, y = 248.5), 
							size = 1, color = "#00529f") +
  geom_circle(mapping = aes(x0 = 298.485, 
							y0 = 248.5, r = 52), 
							color = "#00529f") +
  coord_fixed() +
 
  theme_no_axes(base.theme = theme_bw()) +
  theme(legend.position = c(0.5, 0.04),
        legend.box = "horizontal",
        legend.direction = "horizontal",
        legend.box.background = element_rect(fill = "transparent",
											colour = "transparent"),
        legend.text = element_text(size = 14),
        panel.border = element_blank(),
        axis.title = element_blank(), 
        axis.text = element_blank(), 
        axis.ticks = element_blank()) +
  scale_x_continuous(limits = c(0,647.47), expand = c(0,0)) +
  scale_y_continuous(limits = c(0,497), expand = c(0,0))

player_scatter_plot

ggExtra::ggMarginal(player_scatter_plot, 
                    x = x1 - 258.015, 
                    y = y1, 
                    type = "histogram", 
                    xparams = list(breaks = seq(0,600,50), 
                                   colour = "#00529f"),
                    yparams = list(breaks = seq(50,450,50),
                                   colour = "#00529f")
                    )

Modric R imagen

#Good morning. Would someone be so kind to help me represent the convex area of ​​this data? I try but I am unable.
Thanks! :slight_smile:

It is difficult to delve into a specific problem without access to the underlying data.

But in general a good starting point for concave hull problems is via concaveman::concaveman().

For more information have a look at https://joelgombin.github.io/concaveman/

1 Like

The ggplot2 package website contains information on how developers can expand on the ggplot2 functionality and create their own layers for plotting. By pure coincidence, the example used in their explanation includes how to create a convex hull on your data.
Feel free to give it a read and you might learn some additional stuff about ggplot2, or just grab the example code for the function stat_chull() and use it in your script.

1 Like