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.

data = mtcars
summary(data)
##       mpg             cyl             disp             hp       
##  Min.   :10.40   Min.   :4.000   Min.   : 71.1   Min.   : 52.0  
##  1st Qu.:15.43   1st Qu.:4.000   1st Qu.:120.8   1st Qu.: 96.5  
##  Median :19.20   Median :6.000   Median :196.3   Median :123.0  
##  Mean   :20.09   Mean   :6.188   Mean   :230.7   Mean   :146.7  
##  3rd Qu.:22.80   3rd Qu.:8.000   3rd Qu.:326.0   3rd Qu.:180.0  
##  Max.   :33.90   Max.   :8.000   Max.   :472.0   Max.   :335.0  
##       drat             wt             qsec             vs        
##  Min.   :2.760   Min.   :1.513   Min.   :14.50   Min.   :0.0000  
##  1st Qu.:3.080   1st Qu.:2.581   1st Qu.:16.89   1st Qu.:0.0000  
##  Median :3.695   Median :3.325   Median :17.71   Median :0.0000  
##  Mean   :3.597   Mean   :3.217   Mean   :17.85   Mean   :0.4375  
##  3rd Qu.:3.920   3rd Qu.:3.610   3rd Qu.:18.90   3rd Qu.:1.0000  
##  Max.   :4.930   Max.   :5.424   Max.   :22.90   Max.   :1.0000  
##        am              gear            carb      
##  Min.   :0.0000   Min.   :3.000   Min.   :1.000  
##  1st Qu.:0.0000   1st Qu.:3.000   1st Qu.:2.000  
##  Median :0.0000   Median :4.000   Median :2.000  
##  Mean   :0.4062   Mean   :3.688   Mean   :2.812  
##  3rd Qu.:1.0000   3rd Qu.:4.000   3rd Qu.:4.000  
##  Max.   :1.0000   Max.   :5.000   Max.   :8.000
library(ggplot2)
head(data)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
cleanup = theme(panel.grid.major = element_blank(),
                panel.grid.minor = element_blank(),
                panel.background = element_blank(),
                axis.line.x = element_line(color = "black"),
                axis.line.y = element_line(color = "black"),
                legend.key = element_rect(fill = "white"),
                text = element_text())

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. As shown in the bar, the percentage of each type of cyl is 43.8%(type8), 34.4%(type4), and 21.9%(type6).
# place the code to import graphics here
data$cyl = factor(data$cyl, level = c(4,6,8), labels = c("4","6","8"))
  ggplot(data=data, mapping=aes(x="type of cyl",fill=cyl))+
  geom_bar(stat="count",width=0.5,position='stack')+
  coord_polar("y", start=0)+
  geom_text(stat="count",aes(label = scales::percent(..count../32)), size=4, position=position_stack(vjust = 0.5))

  1. Create a bar graph using ggplot, that shows the number of each carb type in mtcars. As shown in the histogram, the type 2 and 4 (each has 10) in carb are the more frequent type in the mtcars dataset, followed by type 1 (7), type3 (3), type 6 and 8 (each 1).
# place the code to import graphics here
data$carb <- factor(data$carb,levels = c(1,2,3,4,5,6,7,8), labels = c("1","2","3","4","5","6","7","8"))
bar1= 
  ggplot(data, aes(data$carb))+
  geom_bar(stat="count")+
  cleanup
bar1

  1. Next show a stacked bar graphusing ggplot of the number of each gear type and how they are further divided out by cyl. As shown in the stacked bar,in the dataset, most cars have type 3 gear (15), followed by type 4 gear (12) and type 5 gear (5). It’s not hard to find that cars with type 3 and type 5 gear consist all three different cyl types, while the type 4 gear only consist two cyl types.
# place the code to import graphics here
data$gear = factor(data$gear, level = c(3,4,5), labels = c("3","4","5"))
bar2= 
  ggplot(data,aes(x=data$gear,fill=data$cyl))+
  geom_bar(stat="count",width=0.5,position='stack')
  xlab("type of gear")+
  cleanup+
  scale_fill_manual(name = "type of cyl",
                    labels = c("4","6","8"),
                    values = c("4","6","8"))
## NULL
bar2

  1. Draw a scatter plot using ggplot showing the relationship between wt and mpg.
    As shown in the scatter plot, there is a negative relationship between ‘wt’ and ‘mpg’.
# place the code to import graphics here
scatter1 = 
  ggplot(data, aes(data$wt, data$mpg))+
  geom_point()+
  xlab("wt in dataset")+
  ylab("mpg in dataset")+
  cleanup
scatter1

  1. Design a visualization of your choice using ggplot using the data and write a brief summary about why you chose that visualization. The reason why I chose bar is because I wanted to see the breakdown of cyl visually by each type of carb.
# place the code to import graphics here
bar3= 
  ggplot(data,aes(x=data$carb,fill=data$cyl))+
  geom_bar(stat="count",width=0.5,position='stack')
  xlab("type of carb")+
  cleanup+
  scale_fill_manual(name = "type of cyl",
                    labels = c("4","6","8"),
                    values = c("4","6","8"))
## NULL
bar3