#introduction
# mean of mtcars data set
mean(mtcars$mpg)
## [1] 20.09062
# median of mtcars data set
median(mtcars$hp)
## [1] 123
# sum of mtcars data set
sum(mtcars$cyl == 8)
## [1] 14
# range of mtcars data set
range(mtcars$wt)
## [1] 1.513 5.424
sd(mtcars$mpg)
## [1] 6.026948
plot(mtcars$hp, mtcars$mpg, main="Horsepower vs MPG",
xlab="Horsepower", ylab="Miles per Gallon", col="blue", pch=19)
boxplot(mpg ~ cyl, data=mtcars, main="MPG by Cylinder Count",
xlab="Cylinders", ylab="Miles per Gallon", col=c("orange","green","purple"))
hist(mtcars$wt, main="Distribution of Car Weights",
xlab="Weight (1000 lbs)", col="lightblue", breaks=10)
{r} barplot(table(mtcars$gear), main=“Number of Cars by Gear Type”, xlab=“Gears”, ylab=“Count”, col=c(“red”,“green”,“blue”))
## bar plot showing the count of cars by gear type.
barplot(table(mtcars$gear), main="Number of Cars by Gear Type",
xlab="Gears", ylab="Count", col=c("red","green","blue"))
# install.packages("ggplot2")
library(ggplot2)
df <- as.data.frame(table(mtcars$cyl))
colnames(df) <- c("Cylinders", "Count")
ggplot(df, aes(x = "", y = Count, fill = factor(Cylinders))) +
geom_bar(stat = "identity", width = 1) +
coord_polar("y", start = 0) +
labs(title = "Pie Chart: Cars by Cylinder Count",
fill = "Cylinders") +
theme_void()
## line charts
# install.packages("ggplot2") # if needed
library(ggplot2)
df <- mtcars
df$model <- rownames(df)
df$model <- factor(df$model, levels = df$model) # preserve original order
ggplot(df, aes(x = model, y = mpg, group = 1)) +
geom_line() +
geom_point() +
labs(title = "MPG by car (mtcars)", x = "Car model", y = "MPG") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))