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 and expository nature 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.The graph below is a box plot showing the range of 1/4 mile time for automatic and manual transmission types. We can see that manual cars have less 1/4 mile time than automatic cars.
library(ggplot2)
data(mtcars)
mtcars$am <- factor(mtcars$am, levels=c(0,1), labels=c("Automatic","Manual"))
box_plot<- ggplot(mtcars, aes(x=am, y=qsec), fill=am)+
ggtitle("1/4 Mile Time For Each Transmission Type")+
geom_boxplot(aes(fill=am), outlier.colour = "red")+
labs( x= "", y="1/4 Mile Time")+
theme(plot.title = element_text(color="red", size=14, face="bold.italic"))+
scale_fill_discrete(name="Transmission Type")
box_plot
carb
type in mtcars
.The graph below shows the frequency by carburetor types in total 32 automobiles. We can see that 2 and 4 are the most common carburetor types followed by 1 and 3. There is only one automobile has 6 carburetors and one automobile has 8 carburetors.
# place the code to import graphics here
mtcars$carb <- factor(mtcars$carb, levels=c(1,2,3,4,5,6,7,8), labels=c("1","2","3","4","5","6","7","8"))
bar_graph<- ggplot(mtcars,aes(x=carb), fill=carb)+
ggtitle ("Number of each carburetor type in the mtcars dataset")+
geom_bar(aes(fill=carb))+
labs (x="Carburetor types", y="Frequency")+
scale_y_continuous(breaks = seq(0, 15, by = 1))+
scale_fill_discrete(name="Number of carburetors")
bar_graph
gear
type and how they are further divided out by cyl
.The stacked bar char below shows count of automobiles by cylinders and gears among 32 observations. There are 3 types of cars by forward gear numbers, which are 3, 4, or 5 forward gears. There are also 3 types of cars by cylinder numbers, which are 4, 6, or 8 cylinders. The most common forward gears type is 3 gears, followed by 4 gears and 5 gears. The most common cylinder type is 8 cylinders.
# place the code to import graphics here
mtcars$gear <- factor(mtcars$gear)
mtcars$cyl<- factor(mtcars$cyl)
bar_stacked<- ggplot(mtcars,aes(x=gear, fill=cyl))+
ggtitle ("Cylinders by Gears")+
geom_bar()+
labs(x="Gears", y="Frequency") +
scale_fill_discrete(name="Cylinders Type")+
scale_y_continuous(breaks = seq(0, 15, by = 1))
bar_stacked
wt
and mpg
.Below scatter plot shows the relationship between car weight and fule economy. We can conclude from the graph by saying that as car weight increases, fule economy decreases.
# place the code to import graphics here
scatter<- ggplot(mtcars,aes(x=wt, y=mpg))+
ggtitle ("Weight by Miles/Gallon")+
geom_point()+
labs(x="Weight (1000lbs)")+
geom_smooth(method=lm)
scatter
I want to see how fast a vehicle can run in a straight 1/4 mile and potentially what’s the relationship between 1/4 mile time and horsepower and cylinder. I choose a scatter plot to show the relationship between 1/4 mile time and horsepower color by number of cylinders.
# place the code to import graphics here
bar_chart<- ggplot(mtcars, aes (x=hp, y=qsec,color=cyl))+
ggtitle("1/4 mile time by Gross Horsepower and Cylinders")+
geom_point()+
labs(x="Gross horsepower", y="1/4 mile time") +
geom_smooth(method=lm)
bar_chart