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.# place the code to import graphics here
x<-
data(mtcars)
x1<-mtcars$carb[mtcars$carb==1]
length(x1)
## [1] 7
x2<-mtcars$carb[mtcars$carb==2]
length(x2)
## [1] 10
x3<-mtcars$carb[mtcars$carb==3]
length(x3)
## [1] 3
x4<-mtcars$carb[mtcars$carb==4]
length(x4)
## [1] 10
x5<-mtcars$carb[mtcars$carb==6]
length(x5)
## [1] 1
x8<-mtcars$carb[mtcars$carb==8]
length(x8)
## [1] 1
x<-c(7,10,3,10,1,1)
piepercent<- paste(round(100*x/sum(x), 2), "%")
pie(x, labels = piepercent, main = "The proportion of carswith different carb value",col = rainbow(length(x)))
legend("topright", c("carb value=1","carb value=2","carb value=3","carb value=4","carb value=6","carb value=8"), cex = 0.8,
fill = rainbow(length(x)))
#Summary: We can find in the pie chart: When the carb value is equal to 2 or 4, the car type is the most, accounting for 31.25%; when the carb value is equal to 6 or 8, the car color type only accounts for 3.12% of the total.
gear type in mtcarsand write a brief summary.# place the code to import graphics here
H1<-mtcars$gear[mtcars$gear==3]
length(H1)
## [1] 15
H2<-mtcars$gear[mtcars$gear==4]
length(H2)
## [1] 12
H3<-mtcars$gear[mtcars$gear==5]
length(H3)
## [1] 5
H<-c(15,12,5)
M<-c("3","4","5")
barplot(H, xlab="each 'gear' type", ylab="the number of 'gear' type", main='bar chart', names.arg=M, col="blue")
#summary: We can find in the column: All cars are divided into three types of gear type. When the gear value is equal to 3, there are up to 15 types of cars; 12 cars have 4 gears; finally, only 5 cars have 5 gears.
gear type and how they are further divided out by cyland write a brief summary.# place the code to import graphics here
H1<-mtcars$gear[mtcars$gear==3&mtcars$cyl==4]
length(H1)
## [1] 1
H2<-mtcars$gear[mtcars$gear==3&mtcars$cyl==6]
length(H2)
## [1] 2
H3<-mtcars$gear[mtcars$gear==3&mtcars$cyl==8]
length(H3)
## [1] 12
H4<-mtcars$gear[mtcars$gear==4&mtcars$cyl==4]
length(H4)
## [1] 8
H5<-mtcars$gear[mtcars$gear==4&mtcars$cyl==6]
length(H5)
## [1] 4
H6<-mtcars$gear[mtcars$gear==4&mtcars$cyl==8]
length(H6)
## [1] 0
H7<-mtcars$gear[mtcars$gear==5&mtcars$cyl==4]
length(H7)
## [1] 2
H8<-mtcars$gear[mtcars$gear==5&mtcars$cyl==6]
length(H8)
## [1] 1
H9<-mtcars$gear[mtcars$gear==5&mtcars$cyl==8]
length(H9)
## [1] 2
colors <- c("green", "orange", "brown")
gear <- c("3", "4", "5")
cyl <- c("cyl=4", "cyl=6", "cyl=8")
values <- matrix(c(1,8,2,2,4,1,12,0,2), nrow = 3, ncol = 3, byrow = TRUE)
barplot(values, main = "stacked bar graph", names.arg = gear, xlab = "each 'gear' type", ylab = "number", col = colors)
legend("topright", cyl, cex = 0.8, fill = colors)
#Summary:In the stacked bar graph we can see that when the number of gears is equal to 3, cyl equals 8 is the most common but there are still two other cases of cyl. Further, when the car has 4 gears, there are only two cases, that is, cyl is equal to 4 or 6. Finally, if the car has 5 gears, three things can happen.
wt and mpgand write a brief summary.# place the code to import graphics here
x<-mtcars$wt
y<-mtcars$mpg
plot(x,y,xlab = "wt",ylab = "mpg",main = "scatter plot with wt and mpg")
#Summary:In the scatter plot, there seems to be a linear relationship between wt and mpg, ie the value of mpg will decrease as the wt becomes larger. However, if we want to determine if they have a linear relationship, we still need to build a linear model to predict and test these two values.
# place the code to import graphics here
library(corrgram)
## Warning: package 'corrgram' was built under R version 3.5.2
corrgram(mtcars, order=NULL, panel=panel.shade, text.panel=panel.txt,
main="Correlogram")
#Summary:I want to make a correlation visualization about the overall data because the previous questions didn't cover all the data and I wanted to know the correlation between none of the variables. So, I drew data correlation. In the figure, red represents a positive correlation and blue represents a negative correlation. The darker the color, the stronger the correlation. We can clearly see that not all variables present the same correlation.