Directions

During ANLY 512 we will be studying the theory and practice of data visualization. We will be using R and the packages within R to assemble data and construct many different types of visualizations. We begin by studying some of the theoretical aspects of visualization. To do that we must appreciate the basic steps in the process of making a visualization.

The objective of this assignment is to introduce you to R markdown and to complete and explain basic plots before moving on to more complicated ways to graph data.

The final product of your homework (this file) should include a short summary of each graphic.

To submit this homework you will create the document in Rstudio, using the knitr package (button included in Rstudio) and then submit the document to your Rpubs account. Once uploaded you will submit the link to that document on Moodle. Please make sure that this link is hyperlinked and that I can see the visualization and the code required to create it.

Questions

Find the mtcars data in R. This is the dataset that you will use to create your graphics.

  1. Create a pie chart showing the proportion of cars from the mtcars data set that have different carb values.
carburetors <- table(mtcars$carb)
View(carburetors)
#Cal % values
percent<- round(100*carburetors/sum(carburetors), 1)
#Create labels for each pie in the chart
pielabels<- paste(percent, "%", sep="")
#R code to create the Pie Chart
pie(carburetors,col = rainbow(length(carburetors)), labels = percent , main = '% of Carburetors', cex = 0.8)
#Legend for the pie chart
legend("topright", c("Carburetor-1","Carburetor-2","Carburetor-3","Carburetor-4","Carburetor-6","Carburetor-8"), cex=0.8, fill=  rainbow(length(carburetors)))

We can see from the above piechart that the frequency of carburetors 2,4 in the mtcars dataset are the highest at 31.2%

  1. Create a bar graph, that shows the number of each gear type in mtcars.
#Freq of gear type
gearType<-table(mtcars$gear)
View(gearType)
#vector of names appearing under each bar
xVal<-c("Type 3", "Type 4","Type 5")
# Plot the bar chart 
barplot(gearType,names.arg=xVal,xlab="Gear Types",ylab="Total # of each type",col="green",
        main="Distribution Of Gear Types",border="black")

We can infer from the above bar graph that most cars in the mtcars data set had 3 gears

  1. Next show a stacked bar graph of the number of each gear type and how they are further divided out by cyl.
#Gear types and cylinders
cylGear<-table(mtcars$cyl,mtcars$gear)
View(cylGear)
#Fill colors
colors = c("blue","red","yellow")
xVal<-c("Type 3", "Type 4","Type 5")
# Create the bar chart
barplot(cylGear, main = "Distribution of Gears vs Cyclinders", names.arg = xVal, xlab = "Gear Types", ylab = "Frequency", col = colors)
# Add the legend to the chart
legend("topright", rownames(cylGear), cex = 1.3, fill = colors)

From the stacked bar, we see that cars with 3 gears in the dataset have mostly 8 cylinder engines whereas cars with 4 gears have no 8 cylinder engines

  1. Draw a scatter plot showing the relationship between wt and mpg.
#wt vs mpg
wtMpg <- mtcars[,c('wt','mpg')]
# Plot the chart for cars with weight between 2.5 to 5 and mileage between 15 and 30.
plot(x = wtMpg$wt,y = wtMpg$mpg,
     xlab = "Weight",
     ylab = "Milage",
     xlim = c(1,6),
     ylim = c(10,35),        
     main = "Weight vs Milage"
)

We can infer from the scatterplot that there is an inverse relationship between mpg and weight. The heavier the car the lesser the mileage

  1. Design a visualization of your choice using the data and write a brief summary about why you chose that visualization.
#horsepower vs mieage
hpMpg <- mtcars[,c('hp','mpg')]
plot(x = hpMpg$hp,y = hpMpg$mpg,
     xlab = "Horse Power",
     ylab = "Milage",
     xlim = c(50,350),
     ylim = c(10,35),        
     main = "Horsepower vs Milage"
)

I was interested in seeing the relationship between horsepower and mileage.It is beliveed that more horspepower and cc results in lower mileage. This is especially true for supercars like a ferrari or a bugatti and I wanted to test this hypothesis out on the mtcars dataset and it did turn out that the mileage declined as the horsepower increased