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 Canvas. Please make sure that this link is hyperlinked and that I can see the visualization and the code required to create it.

library('ggplot2')
theme = theme(text = element_text(size = 12),
              legend.background = element_rect(fill="lightblue", 
              size=0.5, linetype="solid", colour ="black"), legend.position = "right", 
              axis.text.x = element_text(angle = 20, hjust = 1), 
              plot.title = element_text(hjust = ".7"),
              plot.caption = element_text(hjust = 0.5,color = "black", face = "italic"))

Questions

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

  1. Create a pie chart using ggplot showing the proportion of cars from the mtcars data set that have different cylinder (cyl) values.
ggplot(mtcars, aes(x='', y = cyl, fill =factor(cyl))) + 
  geom_bar(width = 1, stat = "identity") +coord_polar(theta = "y")+labs(title=" Car Proportion based on Cylinder Values", caption = "Pie Chart",x = "", y = "",
      fill = "Cylinders")+ theme_bw() + theme 

#Summary: This pie-chart presents car proportion based on cylinder values. This visualization shows more than 50% cars having 8 cylinders followed by 4 cylinders and  6 cylinders cars.
  1. Create a bar graph using ggplot, that shows the number of each carb type in mtcars.
ggplot(mtcars, aes(x=factor(carb), fill=factor(carb))) +geom_bar()+labs(title="Number of cars for each Carbs Type", caption = "Bar Graph", x = "Carbs", y = "Count", fill = "Carb ")+ theme_bw() + theme 

#Summary: This bar chart shows Cars count for each Carbs Type, this visualization shows carbs type 2 and 4  having highest car count 10, Carb type 1 is at second position for car count. Carb 6 and 8 are having least cars as compared to other carbs.
  1. Next show a stacked bar graphusing ggplot of the number of each gear type and how they are further divided out by cyl.
ggplot(data=mtcars, 
       aes(x = factor(gear), fill = factor(cyl))) + geom_bar() +  labs(title="Number of cars based on Gear Type & Cylinders", caption = "Stacked Bar Chart",
       x = "Number of Gears",
       y = "Count",fill = "Number of Cylinders")+ theme_bw() + theme 

#Summary: This stacked bar chart represents number of cars based on gears and cylinder wise. This visualization  shows 8 cylinder has highest count in gear 3 where as gear 4 doesn't have 8 cylinder. Gear 4 has large count for 4 cylinders cars. Gear 5 has almost same count for 4 cylinders & 8 cylinders cars.
  1. Draw a scatter plot using ggplot showing the relationship between wt and mpg.
ggplot(mtcars, aes(x=wt,y=mpg)) + geom_point() + geom_smooth(method = lm)+labs(title="Relationship Between Weight and Mpg", caption = "Scatter Chart",
       x = "Weight",
       y = "Miles Per Gallon")+ theme_bw() + theme 

#Summary: This scatter plot showing relationship between weight and MPG by plotting them in x and y axis respectively. This visulization show how mpg values varies with respect to weight, when weight values increase, MPG decreases.
  1. Design a visualization of your choice using ggplot using the data and write a brief summary about why you chose that visualization.
ggplot(mtcars, aes(x=factor(cyl),hp)) + geom_point()+labs(title="Relationship Between Cylinder and Horsepower", caption = "Scatter Chart",
       x = "Cylinders",
       y = "Horse Power")+ theme_bw() + theme 

#Summary: I have choosen scatter plot because I wanted to see the relationship between horsepower and cylinders. By this visualtion, it can be conclude that as number of cylinder increased, Horsepower of cars also increased.