Skip to main content

Univariate graphs (part 2)

Visualizing data prior to any analysis is a basic and important step. Univariate plots are those that take into account one varible, these may include histograms, density plots, boxplots, etc.
Here we will cover pie chart and bar chart, which are types of univariate plots.

PIE CHART

Pie charts are a circular statistical graphic which is divided into slices to repesent a numerical proportion. We have to take into account that using pie charts is difficult to compare different sections of a given pie chart, or to compare data across different pie charts.

pie(x, labels = names(x), edges = 200, radius = 0.8, clockwise = FALSE, init.angle = if(clockwise)       90 else 0, density = NULL, angle = 45, col = NULL, border = NULL, lty = NULL, main =             NULL, …)

Using the dataset chickwts we can plot which percentatge of chickens have been feed with each feed type.

TableFeed <- table(chickwts$feed)
TableFeed
## 
##    casein horsebean   linseed  meatmeal   soybean sunflower 
##        12        10        12        11        14        12
par(mfrow= c(1,2))
pie(TableFeed)
labelschi <- paste(names(TableFeed), "(",TableFeed, ")", sep = "")
pie(TableFeed, labels = labelschi, main="Pie Chart (sample sizes)")

plot of chunk unnamed-chunk-1
par(mfrow = c(1,3))
pie(TableFeed, labels = labelschi, main="Pie Chart (sample sizes)")
pie(TableFeed, labels = labelschi, edges =  6,  main="Pie Chart (sample sizes)")
pie(TableFeed, labels = labelschi, clockwise = TRUE,  main="Pie Chart (sample sizes)", init.angle = 45, col = rainbow(6, s= 0.2) , border = "grey")
plot of chunk unnamed-chunk-2
Using percentatges:

slices <- c(1103, 1672, 4786, 3456, 8876) 
labelcountry <- c("Alabama","Florida", "Nevada", "Texas", "Washington")
percent<- round((slices/sum(slices) * 100), 1) #percentatges
labelspercent <- paste(labelcountry, " (" , percent, "%", ")" , sep = c("")) #add percentatges
pie(slices, labels = labelspercent, col=rainbow(5, s= 0.2), main="Pie Chart of States")
plot of chunk unnamed-chunk-3

BAR CHART:

Bar charts are graphs that presents grouped data with rectangular bars with lengths proportional to the values that they represent.

barplot(height, width = 1, space = NULL, names.arg = NULL, legend.text = NULL, beside =             FALSE, horiz = FALSE, density = NULL, angle = 45, col = NULL, border = par(“fg”), main       = NULL, sub = NULL, xlab = NULL, ylab = NULL, xlim = NULL, ylim = NULL, xpd =               TRUE, log = “”, axes = TRUE, axisnames = TRUE, cex.axis = par(“cex.axis”), cex.names =         par(“cex.axis”), inside = TRUE, plot = TRUE, axis.lty = 0, offset = 0, add = FALSE,                     args.legend = NULL, …)

par(mfrow = c(1,3))
barplot(table(chickwts$feed))
barplot(table(chickwts$feed), main="Types of feed", xlab = "Number of chickens", col = rainbow(5, s = 0.2 ), las =2, cex.lab = "0.7", cex.axis = "0.75", cex.names = 0.7)
barplot(table(chickwts$feed), main="Types of feed", col = rainbow(5, s = 0.2 ), las =2, legend.text = TRUE, args.legend = list(x = "bottomleft"))
plot of chunk unnamed-chunk-4
par(mfrow =c(1,2))
barplot(table(chickwts$feed), main="Types of feed", col = rainbow(5, s = 0.2 ), las =2, xlab= "Number of chickens", horiz = TRUE)
barplot(table(chickwts$feed), main="Types of feed", col = rainbow(5, s = 0.2 ), las =2, xlab= "Number of chickens", horiz = TRUE, axes = FALSE, space = FALSE)
plot of chunk unnamed-chunk-5

Popular posts from this blog

Support Vector Machines (SVM) in R (package 'kernlab')

Support Vector Machines (SVM) learning combines of both the instance-based nearest neighbor algorithm and the linear regression modeling. Support Vector Machines can be imagined as a surface that creates a boundary (hyperplane) between points of data plotted in multidimensional that represents examples and their feature values. Since it is likely that the line that leads to the greatest separation will generalize the best to the future data, SVM involves a search for the Maximum Margin Hyperplane (MMH) that creates the greatest separation between the 2 classes. If the data ara not linearly separable is used a slack variable, which creates a soft margin that allows some points to fall on the incorrect side of the margin. But, in many real-world applications, the relationship between variables are nonlinear. A key featureof the SVMs are their ability to map the problem to a higher dimension space using a process known as the Kernel trick, this involves a process of constructing ne...

Initial Data Analysis (infert dataset)

Initial analysis is a very important step that should always be performed prior to analysing the data we are working with. The data we receive most of the time is messy and may contain mistakes that can lead us to wrong conclusions. Here we will use the dataset infert , that is already present in R. To get to know the data is very important to know the background and the meaning of each variable present in the dataset. Since infert is a dataset in R we can get information about the data using the following code: require(datasets) ?infert #gives us important info about the dataset inf <- infert #renamed dataset as 'inf' This gives us the following information: Format 1.Education: 0 = 0-5 years, 1 = 6-11 years, 2 = 12+ years 2.Age: Age in years of case 3.Parity: Count 4.Number of prior induced abortions: 0 = 0, 1 = 1, 2 = 2 or more 5.Case status: 1 = case 0 = control 6.Number of prior spontaneous abortions: 0 = 0, 1 = 1, 2...

Ant Colony Optimization (part 2) : Graph optimization using ACO

The Travelling Salesman Problem (TSP) is one of the most famous problems in computer science for studying optimization, the objective is to find a complete route that connects all the nodes of a network, visiting them only once and returning to the starting point while minimizing the total distance of the route. The problem of the traveling agent has an important variation, and this depends on whether the distances between one node and another are symmetric or not, that is, that the distance between A and B is equal to the distance between B and A, since in practice is very unlikely to be so. The number of possible routes in a network is determined by the equation: (𝒏−𝟏)! This means that in a network of 5 nodes the number of probable routes is equal to (5-1)! = 24, and as the number of nodes increases, the number of possible routes grows factorially. In the case that the problem is symmetrical the number of possible routes is reduced to half: ( (𝒏−𝟏)! ) / 𝟐 The complexity o...