Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Written by Haseeb Mahmud
- # Requirements : R 3.0 or higher with appropriate libraries installed.
- # : Internet connection
- # : R studio environment.
- # http://www.nirpata.com
- library(XML)
- library(ggplot2)
- library(RColorBrewer)
- library(colorRamps)
- #### Assists
- # Page1
- url <- "http://www.uefa.com/worldcup/season=2014/statistics/round=2000296/players/type=assists/index.html"
- assists <- readHTMLTable(url)
- assists
- assists1.df <- do.call(rbind.data.frame, assists)
- # Page2
- url <- "http://www.uefa.com/worldcup/season=2014/statistics/round=2000296/players/type=assists/index,page=2.htmx"
- assists <- readHTMLTable(url)
- assists
- assists2.df <- do.call(rbind.data.frame, assists)
- # Page3
- url <- "http://www.uefa.com/worldcup/season=2014/statistics/round=2000296/players/type=assists/index,page=3.htmx"
- assists <- readHTMLTable(url)
- assists
- assists3.df <- do.call(rbind.data.frame, assists)
- # Page4
- url <- "http://www.uefa.com/worldcup/season=2014/statistics/round=2000296/players/type=assists/index,page=4.htmx"
- assists <- readHTMLTable(url)
- assists
- assists4.df <- do.call(rbind.data.frame, assists)
- #Combining all dataframes
- assists.df <- rbind(assists1.df, assists2.df, assists3.df, assists4.df)
- #Creating two numeric vectors from original dataset
- TotalAssists <- as.numeric(as.character(assists.df$Total))
- TimeTaken <- as.numeric(as.character(assists.df$Time))
- # Computing time taken per assists
- Timeperassists <- TimeTaken/TotalAssists
- #Merging new numeric vectors with original dataset
- assists.data <- data.frame(assists.df, TotalAssists, TimeTaken, Timeperassists)
- #Creating a subset consisting of player who scored at least 2 goals
- assists.Sub <- subset(assists.data, TotalAssists > 1)
- # Reordering the data to fit it into the plot
- assists.sub.trans <- transform(assists.Sub, Player = reorder(Player, TotalAssists))
- # Expanding colorpallete
- colourCount <- length(unique(assists.Sub$Player))
- getPalette <- colorRampPalette(brewer.pal(9, "Set1"))
- # plot with custom pallete
- 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()
- assists.graph
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement