head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
There are 11 variables in this dataset. Here is a description of each variable:
mpg - Miles per gallon (mpg) of the car. This is a continuous variable representing the fuel efficiency of the car.
cyl - Number of cylinders in the car’s engine. This is a categorical variable, indicating whether the car has 4, 6, or 8 cylinders.
disp - Displacement of the engine in cubic inches. This is a continuous variable representing the total volume of all the cylinders in the engine.
hp - Horsepower of the car’s engine. This is a continuous variable representing the power output of the car’s engine.
drat - Rear axle ratio. This is a continuous variable that represents the ratio of the drive axle’s rotational speed to the engine’s rotational speed.
wt - Weight of the car in 1000 pounds. This is a continuous variable representing the weight of the car.
qsec - Quarter mile time. This is a continuous variable representing the time it takes for the car to complete a quarter mile.
vs - Engine type. This is a categorical variable, where 0 indicates a V-shaped engine and 1 indicates a straight engine.
am - Transmission type. This is a categorical variable, where 0 indicates an automatic transmission and 1 indicates a manual transmission.
gear - Number of gears in the car’s transmission. This is a categorical variable indicating how many gears are in the car’s transmission system.
carb - Number of carburetors in the car. This is a continuous variable indicating the number of carburetors installed in the car.
##Static Visualization
library(ggplot2)
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point(aes(color = as.factor(cyl), size = hp, alpha = 1)) +
geom_smooth(method = "lm", se = FALSE, color = "black") +
labs(title = "Scatter Plot of MPG vs Weight",
x = "Weight (1000 lbs)",
y = "Miles per Gallon (mpg)",
color = "Cylinders",
size = "Horsepower") + scale_color_manual(values = c("4" = "#21548A" , "6" = "#77AC30", "8" ="#D95319"))
## `geom_smooth()` using formula = 'y ~ x'
This is a scatter plot created using the ggplot2 library in R. It shows the relationship between the miles per gallon (MPG) of cars and their weight (in 1000 lbs).
X-Axis (Weight in 1000 lbs): The horizontal axis represents the weight of the cars, measured in thousands of pounds.
Y-Axis (Miles per Gallon - MPG): The vertical axis shows the fuel efficiency of the cars in miles per gallon.
Scatter Points: Each point in the plot represents a car. The position of each point corresponds to the car’s weight and MPG. Points are colored based on the number of cylinders in the car:
Blue for cars with 4 cylinders.
Green for cars with 6 cylinders.
Red for cars with 8 cylinders.
Horsepower: The size of the points represents the horsepower of the cars, where larger points correspond to higher horsepower (e.g., black dots represent cars with horsepower ranging from 100 to 300).
Trend Line: A black linear regression line (geom_smooth()) is included to show the overall trend of the data. It suggests a negative correlation, indicating that as the weight of a car increases, its MPG tends to decrease.
library(plotly)
##
## 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
p <- ggplot(mtcars, aes(x = wt, y = mpg, color = as.factor(cyl))) +
geom_point(alpha = 1) +
geom_smooth(method = "lm", se = FALSE, color = "black") +
labs(title = "Interactive Plot: MPG vs Weight",
x = "Weight (1000 lbs)",
y = "Miles per Gallon (mpg)",
color = "Cylinders",
size = "Horsepower") +
scale_color_manual(values = c("4" = "#21548A" , "6" = "#77AC30", "8" ="#D95319"))
interactive_plot <- ggplotly(p)
## `geom_smooth()` using formula = 'y ~ x'
interactive_plot
library(ggplot2)
library(gganimate)
library(av)
animated_plot <- ggplot(mtcars, aes(x = wt, y = mpg, color = as.factor(cyl))) +
geom_point(alpha = 1.5) +
geom_smooth(method = "lm", se = FALSE, color = "black" ) +
labs(title = "Animated Plot: MPG vs Weight",
x = "Weight (1000 lbs)",
y = "Miles per Gallon (mpg)",
color = "Cylinders",
size = "Horsepower") + scale_color_manual(values = c("4" = "#21548A" , "6" = "#77AC30", "8" ="#D95319")) +
transition_states(gear, transition_length = 2, state_length = 1) +
ease_aes('linear')
animated_plot
## `geom_smooth()` using formula = 'y ~ x'