# Load necessary libraries
library(plotly)
library(dplyr)
# Create the interactive visualization
plot <- plot_ly(
data = mtcars,
x = ~mpg,
y = ~wt,
color = ~factor(cyl),
size = ~hp,
text = ~paste("Car Name: ", rownames(mtcars), "<br> Cylinders: ", cyl, "<br> Horsepower: ", hp),
hoverinfo = "text",
type = "scatter",
mode = "markers",
marker = list(
opacity = 0.7,
line = list(width = 1, color = 'white')
)
) %>%
layout(
title = "Interactive Scatter Plot of MPG vs. Weight by Cylinders and Horsepower",
xaxis = list(title = "Miles per Gallon"),
yaxis = list(title = "Weight (1000 lbs)"),
legend = list(title = "Cylinders"),
showlegend = TRUE
)
# Display the plot
plot
The interactive scatter plot visualizes the relationship between miles per gallon (mpg) and weight of each car in the mtcars dataset. The color of each point corresponds to the number of cylinders in the car, and the size of each point represents the horsepower of the car.
From the plot, we can make the following observations:
As the weight of the car increases, the miles per gallon tends to decrease. This negative correlation between weight and mpg is expected since heavier cars generally consume more fuel.
Cars with fewer cylinders (indicated by different colors in the legend) tend to have higher miles per gallon compared to cars with more cylinders. This trend is evident as the colors shift from blue (fewer cylinders) to red (more cylinders).
In Addition, The size of the markers represents the horsepower of the cars. We can see that there is considerable variation in horsepower across the dataset.
By hovering over any data point, we can see detailed information about the car, including its name, the number of cylinders, and the horsepower.
# Load necessary libraries
library(ggplot2)
library(gganimate)
library(dplyr)
# Create the animated visualization
plot_data <- mtcars %>%
mutate(cyl = factor(cyl)) # Convert 'cyl' to factor for better visualization
animation <- ggplot(plot_data, aes(x = hp, y = mpg, color = cyl, size = wt)) +
geom_point(alpha = 0.7) +
labs(title = "Horsepower vs. MPG by Cylinders",
x = "Horsepower",
y = "Miles per Gallon") +
theme_minimal() +
transition_states(cyl, transition_length = 2, state_length = 1) +
enter_fade() +
exit_fade()
# Save the animation as a GIF
anim_save("animated_scatterplot.gif", animation)
# Display the animation (optional, use this to preview in RStudio)
animate(animation)
The animated scatter plot visualizes the relationship between horsepower (hp) and miles per gallon (mpg) of each car in the mtcars dataset. The color of each point represents the number of cylinders in the car, and the size of each point represents the weight(wt) of the car.
As the animation progresses, we can observe the following:
It can be seen from the graph that horsepower and fuel efficiency are generally distributed for each group of cars based on the number of cylinders. For example, one can observe the typical trade-off between horsepower and fuel efficiency for cars with different cylinder counts.
It is also noticable that cars with more cylinders tend to have higher horsepower but lower fuel efficiency, while cars with fewer cylinders often have lower horsepower but higher fuel efficiency.
The size of the points (representing weight) may reveal trends between weight, horsepower, and fuel efficiency. For instance, it can be observed that heavier cars tend to have more powerful engines but lower fuel efficiency.
# Load required libraries
library(ggplot2)
library(gganimate)
library(plotly)
library(magick)
# Create the animated plot
mtcars_anim <- ggplot(mtcars, aes(x = mpg, y = hp, color = factor(cyl), group = cyl)) +
geom_line() +
geom_point() +
labs(title = "Fuel Efficiency vs. Horsepower - Animated Line Apparition Visualization",
x = "Miles per Gallon (mpg)",
y = "Horsepower (hp)",
color = "Cylinders") +
transition_reveal(mpg) + # Animation based on mpg values
ease_aes('linear') + # Linear transition
theme_minimal()
# Save the animated plot as a GIF using magick
anim_file <- "mtcars_animation.gif"
animate(mtcars_anim, nframes = 100, width = 600, height = 400, renderer = gifski_renderer(anim_file))
The animated line apparition visualization displays the relationship between fuel efficiency (measured in miles per gallon - mpg) and horsepower (hp) for different cars in the mtcars dataset, categorized by their cylinder counts. The plot is animated, with lines appearing and tracing their paths as the animation progresses, revealing how the relationship between mpg and horsepower evolves across the dataset.
Key observations from the visualization:
Cars with greater number of Cylinders(cyl) generates higher amount of Horsepower(hp)
Higher Horsepower(hp) tends of offer lower mileage or Miles per Gallon (mpg)