Advertisement
Guest User

goalassists

a guest
Jul 2nd, 2014
1,000
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.24 KB | None | 0 0
  1. # Written by Haseeb Mahmud
  2. # Requirements  : R 3.0 or higher with appropriate libraries installed.
  3. #               : Internet connection
  4. #               : R studio environment.
  5. # http://www.nirpata.com
  6.  
  7. library(XML)
  8. library(ggplot2)
  9. library(RColorBrewer)
  10. library(colorRamps)
  11.  
  12. #### Assists
  13. # Page1
  14. url <- "http://www.uefa.com/worldcup/season=2014/statistics/round=2000296/players/type=assists/index.html"
  15. assists <- readHTMLTable(url)
  16. assists
  17. assists1.df <- do.call(rbind.data.frame, assists)
  18. # Page2
  19. url <- "http://www.uefa.com/worldcup/season=2014/statistics/round=2000296/players/type=assists/index,page=2.htmx"
  20. assists <- readHTMLTable(url)
  21. assists
  22. assists2.df <- do.call(rbind.data.frame, assists)
  23. # Page3
  24. url <- "http://www.uefa.com/worldcup/season=2014/statistics/round=2000296/players/type=assists/index,page=3.htmx"
  25. assists <- readHTMLTable(url)
  26. assists
  27. assists3.df <- do.call(rbind.data.frame, assists)
  28. # Page4
  29. url <- "http://www.uefa.com/worldcup/season=2014/statistics/round=2000296/players/type=assists/index,page=4.htmx"
  30. assists <- readHTMLTable(url)
  31. assists
  32. assists4.df <- do.call(rbind.data.frame, assists)
  33. #Combining all dataframes
  34. assists.df <- rbind(assists1.df, assists2.df, assists3.df, assists4.df)
  35. #Creating two numeric vectors from original dataset
  36. TotalAssists <- as.numeric(as.character(assists.df$Total))
  37. TimeTaken <- as.numeric(as.character(assists.df$Time))
  38. # Computing time taken per assists
  39. Timeperassists <- TimeTaken/TotalAssists
  40. #Merging new numeric vectors with original dataset
  41. assists.data <- data.frame(assists.df, TotalAssists, TimeTaken, Timeperassists)
  42. #Creating a subset consisting of player who scored at least 2 goals
  43. assists.Sub <- subset(assists.data, TotalAssists > 1)
  44. # Reordering the data to fit it into the plot
  45. assists.sub.trans <- transform(assists.Sub, Player = reorder(Player, TotalAssists))
  46. # Expanding colorpallete
  47. colourCount <- length(unique(assists.Sub$Player))
  48. getPalette <- colorRampPalette(brewer.pal(9, "Set1"))
  49. # plot with custom pallete  
  50. assists.graph <- ggplot(data=assists.Sub, aes(x=Player, y=TotalAssists, fill=Team)) + geom_bar() +  scale_fill_manual(values =getPalette(colourCount)) + theme(axis.text.x=element_text(angle = -90, hjust = 0)) + coord_flip()
  51. assists.graph
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement