Author: 235208 Alias: Atonwind
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
if (!require("RColorBrewer")) {
install.packages("RColorBrewer")
library(RColorBrewer)
}
## Loading required package: RColorBrewer
crabTable = as.data.frame(table(mtcars$carb))
names(crabTable)[1] = 'Crab'
crabCount = crabTable$Freq / sum(crabTable$Freq) *100
crabLabel = as.character(crabTable$Crab)
pie(crabCount,paste(crabCount,"%",sep=""),col=brewer.pal(7,"Set1"), main = "The proportion of cars that have different `carb` values ")
legend("topright", as.character(crabTable$Crab), cex = 0.8, fill = brewer.pal(7,"Set1"))
### End of code
Description The pie chart will show the proportion of crab count in the mtcars dataset, the legend will tell the number of crab as type. From the chart, we can conclude that both 2 and 4 crabs have the biggest share at 31.25% out of 32 records. And both 6 and 8 crabs have smallest shart at 3.125%. It would be better to use bar plot to show the detailed numbers of the data as a support.
gear type in mtcarsand write a brief summary.# place the code to import graphics here
gearTable = table(mtcars$gear)
barplot(gearTable, main = "The number of each `gear` type",ylim = c(0,1.1*max(gearTable)))
text(table(mtcars$gear),labels=data.frame(gearTable)$Freq,pos = 3, cex = 0.8)
### End of code
Description From the barplot we can conclude that there 15 car models have 3 gears, and 12 car models have 4 gears, only 5 car models have 5 gears. It is rare to have more gears for a car model.
gear type and how they are further divided out by cyland write a brief summary.# place the code to import graphics here
gearCylCount = table(mtcars$cyl, mtcars$gear)
barplot(gearCylCount, main = "The number of each `gear` type",ylim = c(0,20), col=brewer.pal(4,"Set3"), legend =rownames(gearCylCount))
### End of code
Description For car models with 3 gears, most of the models are 8-cylinder cars, and most of the inline-four engine also have 4 gears, and the types of V6 Engines car models are small. As for car models with 5 gears, the data is not sufficient to draw the conclusion.
wt and mpgand write a brief summary.# place the code to import graphics here
plot(mtcars$wt, mtcars$mpg, main="Relationship between car weight and Fuel economy in automobiles", xlab="Car weight", ylab="MPG(Miles Per Gallon)")
abline(lm(mtcars$mpg~mtcars$wt), col="red")
lines(lowess(mtcars$mpg~mtcars$wt), col="blue")
### End of code
Description The scatter plot shows the relationship between car weight and Fuel economy, the lines added to the plot suggest there is a linear relationship between this 2 factors.
# place the code to import graphics here
if (!require("ggplot2")) {
install.packages("ggplot2")
library(ggplot2)
}
## Loading required package: ggplot2
ggplot(data = mtcars, aes(x=hp, fill=cyl)) + scale_fill_brewer(palette = 15) + geom_bar() + ggtitle("The number of each `hp` type") + coord_flip()
### End of code
Description This fliped barchart can show the horsepower distribution by cylinder counts, we can conclude that the more cylinder the higher horsepower, and there is relatively less types of 6 cylinder car models in the data.