#Summary statistics of the data set
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
## Warning: package 'ggplot2' was built under R version 4.4.3
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.4.3
##
## 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
#Create a scatter plot
ggplot(mtcars, aes(x = disp, y = mpg, color = am, size = wt)) +
geom_point(alpha = 0.7) +
geom_smooth(method = "lm", se = TRUE) +
labs(
title = "Engine Displacement vs. Fuel Efficiency",
subtitle = "Effect of displacement on MPG by transmission type",
x = "Displacement (cu.in.)",
y = "Miles Per Gallon",
color = "Transmission",
size = "Weight (1000 lbs)"
) +
scale_color_manual(values = c("Automatic" = "#E41A1C", "Manual" = "#0000FF")) +
theme_gray() +
theme(
legend.position = "bottom",
plot.title = element_text(face = "bold"),
plot.subtitle = element_text(face = "italic", size = 10)
)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## `geom_smooth()` using formula = 'y ~ x'
## Warning: The following aesthetics were dropped during statistical transformation: size.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
## the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
## variable into a factor?
#Task 03
#Load required libraries
library(plotly)
## Warning: package 'plotly' was built under R version 4.4.3
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
#Add car names to mtcars data set
mtcars$car_name <- rownames(mtcars)
mtcars$transmission <- ifelse(mtcars$am == 0, "Automatic", "Manual")
#Create a plotly visualization
plot_ly(mtcars,
x = ~wt,
y = ~mpg,
color = ~transmission,
size = ~hp,
text = ~paste("Car:", car_name,
"<br>MPG:", mpg,
"<br>Weight:", wt,
"<br>HP:", hp,
"<br>Cylinders:", cyl,
"<br>Transmission:", transmission),
type = "scatter",
mode = "markers") %>%
layout(
title = "Car Weight vs. Fuel Efficiency",
xaxis = list(title = "Weight (1000 lbs)"),
yaxis = list(title = "Miles per Gallon"),
annotations = list(
list(
x = 1,
y = -0.15,
text = "Source: 1974 Motor Trend US magazine",
showarrow = FALSE,
xref = 'paper',
yref = 'paper',
xanchor = 'right',
font = list(size = 10, color = "blue")
)
)
)
## Warning: `line.width` does not currently support multiple values.
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
#Task 04
#Load libraries
library(gganimate)
## Warning: package 'gganimate' was built under R version 4.4.3
#Create the most basic animation possible
p <- ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point(size = 3) +
labs(title = "Car Weight vs. MPG",
x = "Weight (1000 lbs)",
y = "Miles Per Gallon") +
theme_minimal()
#Animate with the transition
simple_anim <- p +
transition_states(
cyl,
transition_length = 1,
state_length = 1
) +
labs(subtitle = "Cylinders: {closest_state}")
#Render as GIF
animate(
simple_anim,
nframes = 32,
fps = 4,
renderer = av_renderer()
)