At first we are going to download the Browser market share of 2016 from w3schools. The following code scrape the page and imports the data as a data table.
library("rvest")
## Loading required package: xml2
library("xml2")
url <- "http://www.w3schools.com/browsers/"
browsershares <- url %>%
read_html() %>%
html_nodes(xpath='//*[@id="main"]/div[4]/table') %>%
html_table() %>%
as.data.frame()
Lets see the condition of the data -
browsershares
## X2016 Chrome IE Firefox Safari Opera
## 1 November 73.8 % 5.2 % 15.3 % 3.5 % 1.1 %
## 2 October 73.0 % 5.2 % 15.7 % 3.6 % 1.1 %
## 3 September 72.5 % 5.3 % 16.3 % 3.5 % 1.0 %
## 4 August 72.4 % 5.2 % 16.8 % 3.2 % 1.1 %
## 5 July 71.9 % 5.2 % 17.1 % 3.2 % 1.1 %
## 6 June 71.7 % 5.6 % 17.0 % 3.3 % 1.1 %
## 7 May 71.4 % 5.7 % 16.9 % 3.6 % 1.2 %
## 8 April 70.4 % 5.8 % 17.5 % 3.7 % 1.3 %
## 9 March 69.9 % 6.1 % 17.8 % 3.6 % 1.3 %
## 10 February 69.0 % 6.2 % 18.6 % 3.7 % 1.3 %
## 11 January 68.4 % 6.2 % 18.8 % 3.7 % 1.4 %
Now we need to clean the data. Specially we need to remove the percentage “%” charecter from the columns and convert the data types into numeric. Let’s make a function for removing percentage using regular expression and converting the data into numeric type.
removePercent <- function(x) {
as.numeric(gsub(" %","",x))
}
Now let’s apply the function to the 2-6 columns of the data frame.
browsershares[,-1] <- apply(browsershares[,-1], 2 ,removePercent)
Let’s see the data now-
browsershares
## X2016 Chrome IE Firefox Safari Opera
## 1 November 73.8 5.2 15.3 3.5 1.1
## 2 October 73.0 5.2 15.7 3.6 1.1
## 3 September 72.5 5.3 16.3 3.5 1.0
## 4 August 72.4 5.2 16.8 3.2 1.1
## 5 July 71.9 5.2 17.1 3.2 1.1
## 6 June 71.7 5.6 17.0 3.3 1.1
## 7 May 71.4 5.7 16.9 3.6 1.2
## 8 April 70.4 5.8 17.5 3.7 1.3
## 9 March 69.9 6.1 17.8 3.6 1.3
## 10 February 69.0 6.2 18.6 3.7 1.3
## 11 January 68.4 6.2 18.8 3.7 1.4
Now let’s select the data for pie slices and for labels -
browsershares.slices <- as.numeric(browsershares[1,2:6])
browsershares.labels <- paste(colnames(browsershares)[2:6]," " ,browsershares.slices," %",sep="")
Now plot the data into a rainbow coloured pie chart -
pie(browsershares.slices,browsershares.labels,
col=rainbow(length(browsershares.labels)),
main="Pie Chart of Browser market share of November, 2016")
To plot a 3D pie chart we can use the library “plotrix”-
library(plotrix)
pie3D(x = browsershares.slices,
labels = browsershares.labels,
explode=0.1,main="Pie Chart of Browser market share of 2016")