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)")
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")
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")
BAR CHART:
Bar charts are graphs that presents grouped data with rectangular bars with lengths proportional to the values that they represent.
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"))
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)