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 Moodle. 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.
# Loading
data(mtcars)
# Print the first 6 rows
head(mtcars)
## 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
mtcars data set that have different cylinder (cyl) values.mytable <- table(mtcars$cyl) # convert the column to a table
lbls <- names(mytable) # extract the labels
pct <- round(as.vector(mytable)/sum(as.vector(mytable))*100) # calculate percentage of the proportion
lbls <- paste(lbls, "\n", pct, sep="") # add percents to labels
lbls <- paste(lbls,"%",sep="") # add % to labels
pie(mytable, labels = lbls,
main="Car proportion by Cylinders")
carb type in mtcars.counts <- table(mtcars$carb) # convert the column to a table
bp <- barplot(counts, main="Car Distribution by Carburetors",
xlab="Number of Carburetors",
ylab="Number of Cars") # create bar chart
text(bp, 0, round(counts, 1),cex=1,pos=3) # add the data labels
gear type and how they are further divided out by cyl.counts <- table(mtcars$cyl, mtcars$gear) # convert two columns to a table
bp <- barplot(counts, main="Car Distribution by Gears and Cylinders",
xlab="Number of Gears",
ylab="Number of Cars",
col=c("darkblue","red","yellow"),
legend = rownames(counts),
args.legend = list(title = "Cylinder", x = "topright", cex = .7))
wt and mpg.attach(mtcars)
plot(wt, mpg, main="Relationship between Weight and Miles Per Gallon",
xlab="Car Weight ", ylab="Miles Per Gallon ", pch=19)
abline(lm(mpg~wt), col="red") # add a regression line (y~x)
attach(mtcars)
## The following objects are masked from mtcars (pos = 3):
##
## am, carb, cyl, disp, drat, gear, hp, mpg, qsec, vs, wt
boxplot(mpg~gear,data=mtcars, main="Miles Per Gallon by Gears",
xlab="Number of Gears", ylab="Miles Per Gallon")