Does a bigger engine always mean a better drive? Explore the relationship between fuel economy, horsepower, weight and transmission in this interactive look at the classic mtcars dataset.
Scroll. Hover. Compare. Discover.
Each bubble represents a car. Move your cursor over a point to reveal its model, MPG, horsepower, weight and transmission. Use the legend to isolate transmission types.
p1 <- plot_ly(
cars,
x = ~hp, y = ~mpg,
type = "scatter", mode = "markers",
color = ~transmission,
colors = c("#e69a45", "#238b91"),
size = ~wt, sizes = c(10, 34),
text = ~paste0(
"<b>", model, "</b>",
"<br>MPG: ", mpg,
"<br>Horsepower: ", hp,
"<br>Weight: ", wt, " (1000 lb)",
"<br>Transmission: ", transmission
),
hoverinfo = "text",
marker = list(opacity = 0.82, line = list(color = "white", width = 1))
) %>%
layout(
xaxis = list(title = "Horsepower (hp)", gridcolor = "#e8edf2"),
yaxis = list(title = "Miles per gallon (MPG)", gridcolor = "#e8edf2"),
legend = list(title = list(text = "Transmission")),
margin = list(l = 65, r = 25, b = 60, t = 20),
paper_bgcolor = "rgba(0,0,0,0)",
plot_bgcolor = "rgba(255,255,255,0)"
)
p1p2 <- plot_ly(
cars, x = ~transmission, y = ~mpg,
color = ~transmission,
colors = c("#e69a45", "#238b91"),
type = "box",
boxpoints = "all",
jitter = 0.28,
pointpos = 0,
text = ~model,
hovertemplate = "%{text}<br>MPG: %{y}<extra></extra>"
) %>%
layout(
xaxis = list(title = "Transmission"),
yaxis = list(title = "Miles per gallon (MPG)", gridcolor = "#e8edf2"),
showlegend = FALSE,
margin = list(l = 65, r = 25, b = 60, t = 20),
paper_bgcolor = "rgba(0,0,0,0)",
plot_bgcolor = "rgba(255,255,255,0)"
)
p2engine_summary <- cars %>%
group_by(cylinders) %>%
summarise(
cars = n(),
average_mpg = mean(mpg),
average_hp = mean(hp),
.groups = "drop"
)
p3 <- plot_ly(
engine_summary,
x = ~cylinders, y = ~average_mpg,
type = "bar",
marker = list(color = c("#238b91", "#e69a45", "#4267a8")),
text = ~paste0(
"Cars: ", cars,
"<br>Average MPG: ", round(average_mpg, 1),
"<br>Average HP: ", round(average_hp, 1)
),
hoverinfo = "text"
) %>%
layout(
xaxis = list(title = "Number of cylinders"),
yaxis = list(title = "Average MPG", gridcolor = "#e8edf2"),
margin = list(l = 65, r = 25, b = 60, t = 20),
paper_bgcolor = "rgba(0,0,0,0)",
plot_bgcolor = "rgba(255,255,255,0)"
)
p3A note on the data: mtcars is a
built-in R dataset containing 32 automobiles from the 1970s. It is a
small historical sample, not a modern market-wide comparison. The charts
show associations and summaries, not causal conclusions.
Hover over the points, compare the groups, and see which cars challenge your assumptions.
Document created on 10 November 2026 · Built with R Markdown, Plotly and the mtcars dataset.