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.
A couple of tips, remember that there may be preprocessing involved in your graphics so you may have to do summaries or calculations to prepare, those should be included in your work.
To ensure accuracy pay close attention to axes and labels, you will be evaluated based on the accuracy of your graphics.
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.
Find the mtcars data in R. This is the dataset that you will use to create your graphics.
mtcars data set that have different cylinder (cyl) values.#Load necessary packages
library(datasets)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
#Calculate proportion of each cylinder
mt<-mtcars
cyl<-table(mt$cyl)
cyl<-as.data.frame(cyl)
total<-sum(cyl$Freq)
Frac<-(cyl$Freq)/total
Pct<-paste(round(100*Frac, 0),"%",sep="")
cyl<-cbind(cyl, Pct)
colnames(cyl)<-c("Cylinder","Frequency","Percentage")
#Make the plot
pie<-ggplot(cyl, aes(x="",y=Frequency,fill=Cylinder)) +geom_bar(stat="identity",width=1,color="white")+coord_polar("y", start=0)
pie+scale_fill_manual(values=c("#CC6666", "#9999CC", "#66CC99"))+geom_text(aes(y=Frequency/1.3+c(0,cumsum(Frequency)[-length(Frequency)]),label=(Percentage)), size=3.5)+ggtitle("Percentage of Cars with Different Number of Cylinders")+theme_void()
carb type in mtcars.library(ggthemes)
bar<-ggplot(mt,aes(carb))+geom_bar(fill="chocolate1")
bar+scale_x_continuous(breaks=seq(1,8,1),name="Number of Carburetors")+scale_y_continuous(breaks=seq(0,10,1), name="Count of Cars")+ggtitle("Count of Cars with Different Number of Carburetors")+ theme_hc()
gear type and how they are further divided out by cyl.cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
bar2<-ggplot(mtcars)+geom_bar(aes(x=factor(gear), fill=factor(cyl)))
bar2+xlab("Number of Gears")+scale_y_continuous(breaks=seq(0,15,1), name="Count of Cars")+ guides(fill=guide_legend("Number of Cylinders"))+scale_fill_manual(values=cbPalette)+theme_bw()+theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank())
wt and mpg.scatter<-ggplot(mtcars, aes(x=wt,y=mpg))
scatter+geom_point(color="red",size=2.3, shape=21)+theme_bw()+xlab("Weight")+ylab("Miles Per Gallon")+ggtitle("Scatter Plot of Weight and Miles Per Gallon")
colnames(mtcars)
## [1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear"
## [11] "carb"
box<-ggplot(mtcars, aes(x=as.factor(vs), y=mpg))+ geom_boxplot(fill="pink", alpha=0.5)
box+scale_x_discrete(breaks=c("0","1"),labels=c("V Engine", "Straight Engine"),name="Engine Type")+ylab("Miles Per Gallon")+theme_bw()+theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank())
I chose box plot to demonstrate the distribution of Miles per gallon of cars with different types of engine. Box plots excels in displaying the important moments of data as well as marking out outliers if there’s any.