Pie Chart

  1. Create a pie chart showing the proportion of cars from the mtcars data set that have different cylinder (cyl) values.
cyl.freq <- table(mtcars$cyl)
label <- names(cyl.freq)
label <- paste("Cylinder",label,"-")
percentage <- round(cyl.freq/sum(cyl.freq)*100)
lableWithPercent <- paste(label, percentage)
lableWithPercent <- paste(lableWithPercent, "%")
pie(cyl.freq, lableWithPercent, col = rainbow(length(label)) ,main = "Propotion of Cars by Cylinder Values")

The approach was to create a cross functional table with frequency of cylinders use by different cars, calculate the percentages and modify the labels to have better naming. To summarize, the above pie charts shows the distribution of cars with different cylinder and a percentage of distribution,

Bar Graph

  1. Create a bar graph, that shows the number of each carb type in mtcars.
cnt <- table(mtcars$carb)
labelBG <- names(cnt)
barplot(
  cnt,
  main = "Distribution of Cars by Carb",
  xlab = "Number of Carb",
  ylab = "Number of Cars",
  names.arg = c("Carb - 1","Carb - 2","Carb - 3","Carb - 4","Carb - 6","Carb - 8"),
  cex.names = 0.6,
  col = rainbow(length(labelBG))
)

The bar graphs display different carb and there distribution in cars and carb 2 being the highest and carb 6 & carb 8 being the lowest.

Stacked Graph

  1. Next show a stacked bar graph of the number of each gear type and how they are further divided out by cyl.
cntStacked <- table(mtcars$cyl, mtcars$gear)
barplot(
  cntStacked,
  main = "Distribution of Cars by Gears and Cylinders",
  xlab = "Number of Gears",
  ylab = "Number of Cylinders",
  names.arg = c("3 Gears","4 Gears","6 Gears"),
  cex.names = 0.8,
  col = c("red","blue","green"), 
                  legend = rownames(cntStacked))

The above graph shows that cars with 6 gear are less common with different types of cylinders, three gears are mostly used 8 cylinders and has low distribution of 4 and 6 cylinders, the last 4 gears cars are bulit only with 6 and 4 cylinder capacity being 4 the highest.

Scatter Plot

  1. Draw a scatter plot showing the relationship between wt and mpg.
plot(
  mtcars$wt,
  mtcars$mpg,
  main = "Comparision of Weight & Mpg",
  xlab = "Weight of Car",
  ylab = "Miles per gallon",
  pch=19
)

lines(lowess(mtcars$wt,mtcars$mpg), col="red")

The scatter plot visualizes the comparision between weight of car and miles per gallon, the data is distributed dispersed and with outliers. To get a better understanding of the plot a line has been drawn to observe the average performance and better fit of the model.

#Box Plot

  1. Design a visualization of your choice using the data and write a brief summary about why you chose that visualization.
boxplot(mpg ~ gear,
        xlab = "Gear of Car",
        ylab = "Mileage of Car",
        names=c("3 Gear","4 Gear","5 Gear"),
        main = "Comparision of Gears & Mileage for Cars",
        data=mtcars)

Box plot is my first choice of visuailization as it helps with daily analytical opertaions I perform. One of the good thing about this chart is easy to understand and also shows outliers if any plus it also shows the summary of your data by displaying the 25 pecentile, median and 75 percentile. In this example, the average value shuffle of being 16 for 3 gear cars, 22 for 4 gear cars and 20 for 5 Gear cars.