data("mtcars")
Variables in the mtcars Dataset mpg: Miles per Gallon (numeric) Represents the fuel efficiency of the car, measured in miles per gallon. cyl: Number of Cylinders (numeric) Indicates the number of cylinders in the car’s engine (typically 4, 6, or 8). disp: Displacement (cubic inches) (numeric) Refers to the engine’s displacement, measured in cubic inches. It represents the total volume of all the cylinders in the engine. hp: Gross Horsepower (numeric) The power output of the engine, measured in horsepower. drat: Rear Axle Ratio (numeric) The ratio of the number of turns of the driveshaft to one turn of the wheels. It affects the car’s acceleration and fuel efficiency. wt: Weight (1000 lbs) (numeric) The weight of the car in thousands of pounds. qsec: 1/4 Mile Time (numeric) The time it takes for the car to travel a quarter-mile, measured in seconds. It is often used to measure acceleration. vs: V/S (Engine Shape) (numeric) A binary variable representing the engine’s configuration: 0 = V-shaped engine, 1 = straight engine. am: Transmission (numeric) A binary variable indicating the type of transmission: 0 = automatic, 1 = manual. gear: Number of Forward Gears (numeric) The number of forward gears in the car’s transmission (typically 3, 4, or 5). carb: Number of Carburetors (numeric) The number of carburetors in the car’s engine.
# Scatter plot of MPG vs Horsepower
plot(mtcars$hp, mtcars$mpg,
main="MPG vs Horsepower",
xlab="Horsepower",
ylab="Miles Per Gallon (MPG)",
pch=19,
col="blue")
# Line plot of MPG over the index of the cars
plot(mtcars$mpg,
type="o",
main="MPG Over Car Index",
xlab="Car Index",
ylab="Miles Per Gallon (MPG)",
col="red")
# Histogram of MPG
hist(mtcars$mpg,
main="Distribution of Miles Per Gallon (MPG)",
xlab="Miles Per Gallon (MPG)",
col="lightblue",
border="black")
# Boxplot of MPG for each cylinder category
boxplot(mpg ~ cyl, data=mtcars,
main="MPG by Number of Cylinders",
xlab="Number of Cylinders",
ylab="Miles Per Gallon (MPG)",
col=c("orange", "lightblue", "lightgreen"))
# Bar plot of the number of cars with different cylinder counts
barplot(table(mtcars$cyl),
main="Number of Cars by Cylinder Count",
xlab="Number of Cylinders",
ylab="Count of Cars",
col="purple")
# Pie chart of the number of cars with different cylinder counts
cylinders <- table(mtcars$cyl)
pie(cylinders,
main="Proportion of Cars by Cylinder Count",
col=c("red", "green", "blue"))
# Pairs plot of selected variables in mtcars
pairs(mtcars[, c("mpg", "hp", "wt", "disp")],
main="Scatterplot Matrix",
col="darkgreen")
# Notched boxplot of MPG by number of cylinders
boxplot(mpg ~ cyl, data=mtcars,
notch=TRUE,
main="MPG by Number of Cylinders (Notched)",
xlab="Number of Cylinders",
ylab="Miles Per Gallon (MPG)",
col=c("pink", "cyan", "yellow"))
## Warning in (function (z, notch = FALSE, width = NULL, varwidth = FALSE, : some
## notches went outside hinges ('box'): maybe set notch=FALSE
# Density plot of MPG
plot(density(mtcars$mpg),
main="Density Plot of Miles Per Gallon (MPG)",
xlab="Miles Per Gallon (MPG)",
col="purple",
lwd=2)
# Generate a histogram of 'mpg' (Miles Per Gallon)
hist(mtcars$mpg,
main="Histogram with Density Plot: MPG",
xlab="Miles Per Gallon",
col="lightblue",
border="black",
probability=TRUE) # Set probability=TRUE to plot density
# Add a density plot over the histogram
lines(density(mtcars$mpg),
col="red",
lwd=2) # lwd sets the line width
# Stacked bar plot of the number of gears by cylinder
gear_cyl <- table(mtcars$cyl, mtcars$gear)
barplot(gear_cyl,
main="Stacked Bar Plot of Gears by Cylinder",
xlab="Number of Gears",
ylab="Count",
col=c("red", "green", "blue"),
legend=rownames(gear_cyl))
Example of Summary Statistics To get a quick summary of these variables, you can use the summary() function in R:
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