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.

Questions

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

  1. Create a box plot using ggplot showing the range of values of 1/4 mile time (qsec) for each tansmission type (am, 0 = automatic, 1 = manual) from the mtcars data set.
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.6.2
mtcars$am<- factor(mtcars$am,
            levels = c(0,1),
            labels= c('Automatic','Manual'))


p<-ggplot(mtcars,aes(x = am,y = qsec,fill =am))+geom_boxplot()+labs(x='Transmission Type',y ='1/4 mile time (qsec)')+ggtitle("1/4 mile time by different Transmission Type")
legend_title<-'Transmission\n Type'
p+scale_fill_discrete(legend_title)

Comments

This graph above show the 1/4 mile time by different Transmission Type

  1. Create a bar graph using ggplot, that shows the number of each carb type in mtcars.
t <- table(mtcars$carb)
data2 <- as.data.frame(t)

names(data2) <- c('carb','number')
data2
##   carb number
## 1    1      7
## 2    2     10
## 3    3      3
## 4    4     10
## 5    6      1
## 6    8      1
p2<- ggplot(data = data2,aes(x = carb,y = number))+
    geom_bar(stat = 'identity',width = 0.5,
             fill = 'steelblue')
p2+ggtitle("number of each type of carb in mtcars")

Comments

This show the frequency for each type of carb

  1. Next show a stacked bar graph using ggplot of the number of each gear type and how they are further divided out by cyl.
library(plyr)

p3<-ggplot(mtcars,aes(x = factor(cyl),fill = factor(gear)))+
               xlab("Number of Cyl")+
               ylab("Gear type count")+
               geom_bar()+
    ggtitle('Number of each gear type divided by cyl')
p3+scale_fill_discrete('Gear\nType')

Comments

This graph show the number of different type of Cyl and color of stacked bar represent diferent gear

  1. Draw a scatter plot using ggplot showing the relationship between wt and mpg.
p4<- ggplot(mtcars,aes(x = wt,y = mpg))+
    geom_point()+
    ggtitle("relationship between wt and mpg")
p4

Comments

This graph demonstrate the relationship between wt and mpg

  1. Design a visualization of your choice using ggplot using the data and write a brief summary about why you chose that visualization.
p5<-ggplot(mtcars,aes(x = factor(gear),y = hp,fill = factor(gear)))+
                geom_boxplot()+
                xlab('Gear Type')+
                ylab('hp')+
    ggtitle("boxplot of hp by different gear type")+scale_fill_discrete("Gear\nType")
p5

Comments

This graph show the distribution of hp by different gear type