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 is preprocessing involved in many 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.
mtcars data set that have different carb values and write a brief summary.# code to prepare the data
require(scales)
## Loading required package: scales
require(dplyr)
## Loading required package: 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
summary(mtcars)
## 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
dim(mtcars)
## [1] 32 11
table(mtcars$carb)
##
## 1 2 3 4 6 8
## 7 10 3 10 1 1
data1 <- mtcars %>%
group_by(carb) %>%
count() %>%
ungroup() %>%
mutate(per=`n`/sum(`n`)) %>%
arrange(carb)
data1$label <- scales::percent(data1$per)
# place the code to import graphics here
require(ggplot2)
## Loading required package: ggplot2
ggplot(data=data1,aes(x = "", y = per, fill = carb))+
geom_bar(stat="identity", width = 1)+
coord_polar("y", start= -1)+
theme_void()+
geom_text(aes(x=1, y = cumsum(per) - per/2, label=label)) +
labs(title = 'Pie chart showing percentage of cars with different number of carburetors')
Summary
In the mtcars dataset, among the 32 cars, the most frequently seened number of carburetors a car has is 2 and 4. 31.2% of the cars have 2 and 31.2% has 4 carburetors. The next frequently seen is 1 carburetors, about 7 cars is equipped with only carburetor. Cars with more than 4 carburetors are rarely seen, only 1 with 6 and 1 with 8, each accounting for 3.1% in this sample.
gear type in mtcarsand write a brief summary.# place the code to import graphics here
# using geom_bar to create bar chart, it includes the count process, therefore no data preparation in this chart creation
require(ggplot2)
ggplot(mtcars, aes(gear)) +
geom_bar(fill = "#FF6666") +
labs(title = 'Bar chart showing Number of Cars with different number of forward gears', x = 'Number of Forward Gears', y = 'Count of Cars')
Summary
In the mtcars dataset, among the 32 cars, most commonly seen number of forward gears equipped on a car is either 3 or 4. 15 cars have 3 forward gears and 12 cars has 4. Only 5 cars have 5 forward gears.
gear type and how they are further divided out by cyland write a brief summary.# code for data preparation
require(plyr)
## Loading required package: plyr
## -------------------------------------------------------------------------
## You have loaded plyr after dplyr - this is likely to cause problems.
## If you need functions from both plyr and dplyr, please load plyr first, then dplyr:
## library(plyr); library(dplyr)
## -------------------------------------------------------------------------
##
## Attaching package: 'plyr'
## The following objects are masked from 'package:dplyr':
##
## arrange, count, desc, failwith, id, mutate, rename, summarise,
## summarize
data3 <- ddply(mtcars, .(mtcars$gear, mtcars$cyl), nrow)
names(data3) <- c("gear", "cyl", "Freq")
# place the code to import graphics here
ggplot(data3, aes(x = gear, y = Freq)) +
geom_bar(stat = 'identity',position = 'stack', aes(fill = cyl)) +
labs(title = 'Stack Bar Chart Showing Number of Cars with different forward gears breakdown by number of cylinders equipped',
x = 'Number of Forward Gears', y = 'Count of Cars')
Summary In mtcars dataset, 15 cars have 3 forward gear, among which 12 of them have 8 cylinders. For those with 4 forward gear, most of them has either 4 or 6 cynlinders. For the car with 5 forward gears, 2 have 8 cylinders, 1 at 6 and 2 has 4. All of the cars have either 4, 6, or 8 cylinders.
wt and mpgand write a brief summary.# place the code to import graphics here
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point() +
geom_smooth(method=lm) +
labs(title = 'Scatterplot showing relationship between Weight(1000lbs) and Miles/(US)gallon in mtcars dataset', x = 'Weight (1000 lbs)', y = 'Miles/(US) gallon')
Summary From the scatterplot, especially adding the smooth line with linear regression function, we can see clearly there’s a linear pattern between weight and miles per gallon in the mtcars dataset. The heavier the car, the less miles per gallon will the car has.
# place the code to import graphics here
ggplot(mtcars, aes(x=hp, y=mpg)) +
geom_point() +
geom_smooth() +
labs(title = 'Scatterplot showing relationship between Gross Horsepower and Miles/(US)gallon in mtcars dataset', x = 'Gross Horsepower', y = 'Miles/(US) gallon')
## `geom_smooth()` using method = 'loess'
Summary I chekced the scatterplot of gross horsepower and miles per gallon to understand their relationship. AS horsepower refers to the power of the car’s engine, usually we would assume a negative correlation between the two. As shown in the plot, with a loose smooth line clearly indicating, we do see the higher the horsepower, the more gas the car consumers per mile.