#concat the names for the legend
names = as.factor(paste(unique(mtcars$carb),"carbs"))
#get the percentages
percent=100*table(mtcars$carb)/length(mtcars$carb)
pie(x=percent, label=paste(percent, "%"), col=rainbow(length(names)), main="Percentage of cars per number of carburators" )
#add legend
legend("right",legend = names, fill = rainbow(length(names)), cex=0.8)
As you can see in the pie chart, the majority of the cars have 3 or 1 carburators while the minority of the cars have 6 or 8 carburators.
#create one-dimensional table of frequencies
freq = table(mtcars$gear)
barplot(freq, main = "Frequency by number of gears", xlab = "Number of Gears", ylab="Frequency")
Most of the cars have 3 gears with less cars with 4 gears and even less with 5 gears.
#create the two dimensional matrix of frequencies
combined = table(mtcars$cyl, mtcars$gear)
barplot(combined, main = "Cars distribution by gears and cyllinders",
xlab = "Number of Gears",
ylab= "Frequency",
col = rainbow(3),
legend = rownames(combined))
We can see that most of the cars with 3 gears have 8 cyllinders and there are no cars with 4 gears that have 8 cyllinders. The distribution is less than wild for cars with 5 gears.
plot(mtcars$wt , mtcars$mpg, xlab = 'Weight',
ylab = 'MPG',
main = 'Weight Vs MPG')
We can clearly see the relationship between weight and fuel consumption. The heavier the car - the more fuel it consumes per gallon, leading to lower MPG.
boxplot(mtcars$mpg~mtcars$gear,data=mtcars,
xlab="Number of gears",
ylab="MPG",
main="Boxplot to show the distribution of MPG values per number of gears")
I chose boxplots for this visualization because it provides a lot of information in a single chart. We can see the range, the median, and the ourliers easily and it is very effective for visual exploratory analysis.