R/hello.R

Defines functions create_Soccer_Pitch

Documented in create_Soccer_Pitch

#Load Libraries
library(ggplot2)


#' Title
#'
#' @param grass_colour
#' @param line_colour
#' @param background_colour
#' @param goala_colour
#' @param goalb_colour
#' @param tram_colour
#' @param label_colour
#' @param BasicFeatures
#'
#' @return
#' @export
#'
#' @examples
create_Soccer_Pitch <- function(grass_colour, line_colour, background_colour, goala_colour,goalb_colour,tram_colour,label_colour, BasicFeatures){

  theme_blankPitch = function(size=12) {
    theme(
      #axis.line=element_blank(),
      axis.text.x=element_blank(),
      axis.text.y=element_blank(),
      #axis.ticks.y=element_text(size=size),
      #axis.ticks=element_blank(),
      axis.ticks.length=unit(0, "lines"),
      #axis.ticks.margin=unit(0, "lines"),
      axis.title.x=element_blank(),
      axis.title.y=element_blank(),
      legend.key.size=unit(1.2, "lines"),
      legend.text=element_text(size=size),
      legend.title=element_text(size=size, face="bold",hjust=0),
      # panel.border=element_blank(),
      panel.grid.major=element_blank(),
      panel.grid.minor=element_blank(),
      panel.spacing=element_blank(),
      plot.margin=unit(c(0, 0, 0, 0), "lines"),
      plot.title=element_text(size=size*1.2),
      panel.background=element_rect(fill= background_colour,colour= background_colour),
      strip.text.x=element_text(size=size*1))}

  ymin <- 0 # minimum width
  ymax <- 68 # maximum width
  xmin <- 0 # minimum length
  xmax <- 105 # maximum length

  # pitch lines
  defbbxst <- 16.5
  offbbxst <- 88.5
  bbyst <- 13.85
  bbyed <- 54.15
  defsbxst <- 5.5
  offsbxst <- 99.5
  sbyst <- 24.85
  sbyed <- 43.15

  # small box
  sbrd <- 24.85
  sbld <- 43.15
  sbDefxed <- 5.5
  sbOffxed <- 99.5

  #Big box
  bbrd <- 13.85
  bbld <- 54.15
  bbOffxed <- 88.5


  #Goals
  glxst <- 0
  glyst <- 30.35
  glxed <- 105
  glyed <- 37.65

  #halfway line
  hwxst <- 52.5
  hwyst <- ymin
  hwyed <- ymax
  #centre spot
  hwspyst <- 34
  hwspxst <- 52.3
  hwspxed <- 52.8

  #pen spot

  pdxst <- 11
  pdyst <- 33.8
  pdyed <- 34.2
  poxst <- 94

  #tramlines
  srwing <- 22.65
  rhalfsp <- 45.32
  ctre <- 51
  lhalfsp <- 54.4


  circleFundef <- function(center=c(0,0), diameter=1, npoints=100, start=0, end=1)
  {
    tt <- seq(start*pi, end*pi, length.out=npoints)
    data.frame(x = center[1] + diameter / 2  * cos(tt),
               y = center[2] + diameter / 2 * sin(tt))
  }


  circleFunatt <- function(center=c(0,0), diameter=1, npoints=100, start=0, end=1)
  {
    tt <- seq(start*pi, end*pi, length.out=npoints)
    data.frame(x = center[1] - diameter / 2 * cos(tt),
               y = center[2] - diameter / 2 * sin(tt))
  }


  circleFuncent <- function(center=c(0,0), diameter=2, npoints=1000, start=0, end=2)
  {
    r = diameter / 2
    tt <- seq(0,2*pi,length.out = npoints)
    x <- center[1] + r * cos(tt)
    y <- center[2] + r * sin(tt)
    return(data.frame(x = x, y = y))
  }

  # create penalty semi circle with help from https://github.com/FCrSTATS/Visualisations/blob/master/3.CreateAPitch.md
  def_circle <-  circleFundef(c(13,34), 14, start=1.5, end=2.5)
  def_circle <- def_circle[which(def_circle$x >= (16.5)),]
  att_circle <-  circleFunatt(c(92,34), 14, start=1.5, end=2.5)
  att_circle <- att_circle[which(att_circle$x <= (88.5)),]
  cent_circle <-  circleFuncent(c(52.5,34), 18, start=1.5, end=2.5)

  if(BasicFeatures == TRUE){
    ## initiate the plot, set some boundries to the plot
    np <- ggplot() + xlim(c(xmin,xmax)) + ylim(c(ymin,ymax)) +
      # add the theme
      theme_blankPitch() +
      # add lines goals etc
      geom_rect(aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax), fill = grass_colour, colour = line_colour) +
      #horbox
      geom_segment(aes(x = defsbxst, y = sbyst, xend = defsbxst, yend = sbyed),colour = line_colour,linetype="solid",size = 0.5) +
      geom_segment(aes(x = offsbxst, y = sbyst, xend = offsbxst, yend = sbyed),colour = line_colour,linetype="solid",size = 0.5) +
      geom_segment(aes(x = defbbxst, y = bbyst, xend = defbbxst, yend = bbyed),colour = line_colour,linetype="solid",size = 0.5) +
      geom_segment(aes(x = offbbxst, y = bbyst, xend = offbbxst, yend = bbyed),colour = line_colour,linetype="solid",size = 0.5) +
      #vertsbox
      geom_segment(aes(x = xmin, y = sbrd, xend = sbDefxed, yend = sbrd),colour = line_colour,linetype="solid",size = 0.5) +
      geom_segment(aes(x = xmin, y = sbld, xend = sbDefxed, yend = sbld),colour = line_colour,linetype="solid",size = 0.5) +
      geom_segment(aes(x = xmax, y = sbrd, xend = sbOffxed, yend = sbrd),colour = line_colour,linetype="solid",size = 0.5) +
      geom_segment(aes(x = xmax, y = sbld, xend = sbOffxed, yend = sbld),colour = line_colour,linetype="solid",size = 0.5) +
      #vertbbox
      geom_segment(aes(x = xmin, y = bbrd, xend = defbbxst, yend = bbrd),colour = line_colour,linetype="solid",size = 0.5) +
      geom_segment(aes(x = xmin, y = bbld, xend = defbbxst, yend = bbld),colour = line_colour,linetype="solid",size = 0.5) +
      geom_segment(aes(x = xmax, y = bbrd, xend = offbbxst, yend = bbrd),colour = line_colour,linetype="solid",size = 0.5) +
      geom_segment(aes(x = xmax, y = bbld, xend = offbbxst, yend = bbld),colour = line_colour,linetype="solid",size = 0.5) +
      #halfway
      geom_segment(aes(x = hwxst, y = hwyst, xend = hwxst, yend = hwyed),colour = line_colour,linetype="solid",size = 0.5) +
      geom_segment(aes(x = hwspxst, y = hwspyst, xend = hwspxed, yend = hwspyst),colour = line_colour,linetype="solid",size = 0.5) +
      #pen spots
      geom_segment(aes(x = pdxst, y = pdyst, xend = pdxst, yend = pdyed),colour = line_colour,linetype="solid",size = 0.5) +
      geom_segment(aes(x = poxst, y = pdyst, xend = poxst, yend = pdyed),colour = line_colour,linetype="solid",size = 0.5) +
      #pen semi circles
      geom_path(data=def_circle, aes(x,y), colour = line_colour,size = 0.5) +
      geom_path(data=att_circle, aes(x,y), colour = line_colour,size = 0.5) +
      #goals
      geom_segment(aes(x = glxst, y =  glyst, xend = glxst, yend = glyed),colour = goala_colour, size = 1) +
      geom_segment(aes(x =glxed, y =  glyst, xend =glxed, yend = glyed),colour = goalb_colour, size = 1) +
      #circle
      geom_path(data=cent_circle, aes(x,y), colour = line_colour,size = 0.5) +
      annotate(geom="text", x= 98, y=2, label="www.witnesstheanalysis.com",color = label_colour,size = 4, alpha = 0.4) +
      annotate(geom="text", x= 8, y=66, label="www.witnesstheanalysis.com",color = label_colour,size = 4, alpha = 0.4)

  }

  else{
    ## initiate the plot, set some boundries to the plot
    np <- ggplot() + xlim(c(xmin,xmax)) + ylim(c(ymin,ymax)) +
      # add the theme
      theme_blankPitch() +
      # add lines goals etc
      geom_rect(aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax), fill = grass_colour, colour = line_colour) +
      #horbox
      geom_segment(aes(x = defsbxst, y = sbyst, xend = defsbxst, yend = sbyed),colour = line_colour,linetype="solid",size = 0.5) +
      geom_segment(aes(x = offsbxst, y = sbyst, xend = offsbxst, yend = sbyed),colour = line_colour,linetype="solid",size = 0.5) +
      geom_segment(aes(x = defbbxst, y = bbyst, xend = defbbxst, yend = bbyed),colour = line_colour,linetype="solid",size = 0.5) +
      geom_segment(aes(x = offbbxst, y = bbyst, xend = offbbxst, yend = bbyed),colour = line_colour,linetype="solid",size = 0.5) +
      #vertsbox
      geom_segment(aes(x = xmin, y = sbrd, xend = sbDefxed, yend = sbrd),colour = line_colour,linetype="solid",size = 0.5) +
      geom_segment(aes(x = xmin, y = sbld, xend = sbDefxed, yend = sbld),colour = line_colour,linetype="solid",size = 0.5) +
      geom_segment(aes(x = xmax, y = sbrd, xend = sbOffxed, yend = sbrd),colour = line_colour,linetype="solid",size = 0.5) +
      geom_segment(aes(x = xmax, y = sbld, xend = sbOffxed, yend = sbld),colour = line_colour,linetype="solid",size = 0.5) +
      #vertbbox
      geom_segment(aes(x = xmin, y = bbrd, xend = defbbxst, yend = bbrd),colour = line_colour,linetype="solid",size = 0.5) +
      geom_segment(aes(x = xmin, y = bbld, xend = defbbxst, yend = bbld),colour = line_colour,linetype="solid",size = 0.5) +
      geom_segment(aes(x = xmax, y = bbrd, xend = offbbxst, yend = bbrd),colour = line_colour,linetype="solid",size = 0.5) +
      geom_segment(aes(x = xmax, y = bbld, xend = offbbxst, yend = bbld),colour = line_colour,linetype="solid",size = 0.5) +
      #halfway
      geom_segment(aes(x = hwxst, y = hwyst, xend = hwxst, yend = hwyed),colour = line_colour,linetype="solid",size = 0.5) +
      geom_segment(aes(x = hwspxst, y = hwspyst, xend = hwspxed, yend = hwspyst),colour = line_colour,linetype="solid",size = 0.5) +
      #pen spots
      geom_segment(aes(x = pdxst, y = pdyst, xend = pdxst, yend = pdyed),colour = line_colour,linetype="solid",size = 0.5) +
      geom_segment(aes(x = poxst, y = pdyst, xend = poxst, yend = pdyed),colour = line_colour,linetype="solid",size = 0.5) +
      #pen semi circles
      geom_path(data=def_circle, aes(x,y), colour = line_colour,size = 0.5) +
      geom_path(data=att_circle, aes(x,y), colour = line_colour,size = 0.5) +
      #goals
      geom_segment(aes(x = glxst, y =  glyst, xend = glxst, yend = glyed),colour = goala_colour, size = 1) +
      geom_segment(aes(x =glxed, y =  glyst, xend =glxed, yend = glyed),colour = goalb_colour, size = 1) +
      #tramlines
      geom_segment(aes(x = xmin, y = srwing, xend = xmax, yend = srwing),colour = tram_colour,linetype="dotted",size = 0.5) +
      geom_segment(aes(x = xmin, y = rhalfsp, xend = xmax, yend = rhalfsp),colour = tram_colour,linetype="dotted",size = 0.5) +
      geom_segment(aes(x = 16.5, y = ymin, xend = 16.5, yend = ymax),colour = tram_colour,linetype="dotted",size = 0.5) +
      geom_segment(aes(x = 35, y = ymin, xend = 35, yend = ymax),colour = tram_colour,linetype="dotted",size = 0.5) +
      geom_segment(aes(x = 70, y = ymin, xend = 70, yend = ymax),colour = tram_colour,linetype="dotted",size = 0.5) +
      geom_segment(aes(x = 88.5, y = ymin, xend = 88.5, yend = ymax),colour = tram_colour,linetype="dotted",size = 0.5) +
      annotate(geom="text", x= 98, y=2, label="www.witnesstheanalysis.com",color = label_colour,size = 4, alpha = 0.4) +
      annotate(geom="text", x= 8, y=66, label="www.witnesstheanalysis.com",color = label_colour,size = 4, alpha = 0.4) +
      #circle
      geom_path(data=cent_circle, aes(x,y), colour = line_colour,size = 0.5)
  }

  return(np)}

create_Soccer_Pitch("#A2CD5A", "white", "lightblue", "blue","red","white","white", BasicFeatures=TRUE)
witnesstheanalysis/SoccerPitch documentation built on March 6, 2020, 1 a.m.