This R script report harnesses R’s advanced plotting libraries to explore built-in datasets (iris, mtcars, Titanic, volcano) through diverse visualization techniques, including scatter plots, boxplots, histograms, 3D scatter plots, and interactive visualizations. The code uncovers critical patterns, such as species clustering in iris or performance correlations in mtcars, making it a powerful tool for data analysis. By leveraging ggplot2, plotly, and Chart.js, it produces publication-ready HTML output, ideal for data scientists, analysts, and educators aiming to communicate complex insights with clarity and interactivity.
# Load required libraries
library(Hmisc)
library(ggplot2)
library(ggpubr)
library(ggformula)
library(lattice)
library(plot3D)
library(rgl)
library(ggExtra)
library(viridis)
library(plotly)
library(vcd)
library(reshape2)
library(ggmosaic)
library(shape)
library(htmlwidgets)
A scatter plot showing the relationship between sepal length and petal length, colored by species, with regression lines.
ggplot(data = iris,
aes(x = Sepal.Length, y = Petal.Length, colour = Species)) +
geom_smooth(method = "lm", se = TRUE) +
geom_point(size = 2) +
scale_color_manual(values = c("setosa" = "#FF0000",
"versicolor" = "#0000FF",
"virginica" = "#00FF00")) +
ggtitle("Sepal Length vs Petal Length") +
xlab("Sepal Length (cm)") +
ylab("Petal Length (cm)") +
theme_classic()
A dual-axis plot showing horsepower and quarter mile time against
miles per gallon, reimplemented in ggplot2 for clarity.
library(ggplot2)
# Scale qsec to align with hp for dual axis
scale_factor <- max(mtcars$hp) / max(mtcars$qsec)
ggplot(mtcars, aes(x = mpg)) +
geom_line(aes(y = hp, color = "Horsepower")) +
geom_point(aes(y = hp, color = "Horsepower")) +
geom_line(aes(y = qsec * scale_factor, color = "Quarter Mile Time")) +
geom_point(aes(y = qsec * scale_factor, color = "Quarter Mile Time")) +
scale_y_continuous(name = "Horsepower",
sec.axis = sec_axis(~./scale_factor, name = "Quarter Mile Time (seconds)")) +
scale_color_manual(values = c("Horsepower" = "red", "Quarter Mile Time" = "blue")) +
labs(title = "Car Performance: Horsepower and Quarter Mile Time",
x = "Miles per Gallon",
color = "Metric") +
theme_minimal()
A boxplot showing the distribution of sepal length across iris species.
ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) +
geom_boxplot(alpha = 0.7) +
theme_minimal() +
labs(title = "Sepal Length by Iris Species",
x = "Species",
y = "Sepal Length (cm)") +
scale_fill_discrete(name = "Iris Species")
A histogram showing the distribution of fuel efficiency in the
mtcars dataset.
ggplot(mtcars, aes(x = mpg)) +
geom_histogram(fill = "yellow", colour = "green", bins = 15, alpha = 0.7) +
labs(title = "Distribution of Miles per Gallon",
x = "Miles per Gallon",
y = "Frequency") +
theme_minimal()
A scatter plot with marginal boxplots to show the distribution of sepal dimensions.
p <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point(alpha = 0.7) +
theme_minimal() +
labs(title = "Sepal Dimensions by Species",
x = "Sepal Length (cm)",
y = "Sepal Width (cm)",
color = "Species")
ggMarginal(p, type = "boxplot", groupColour = TRUE)
A bubble chart showing car weight vs. fuel efficiency, with bubble size representing horsepower and color indicating cylinders.
ggplot(mtcars, aes(x = wt, y = mpg, size = hp, color = factor(cyl))) +
geom_point(alpha = 0.7) +
scale_size(range = c(3, 10), name = "Horsepower") +
scale_color_manual(
name = "Cylinders",
values = c("4" = "blue", "6" = "green", "8" = "red"),
labels = c("4 cylinders", "6 cylinders", "8 cylinders")
) +
labs(
title = "Car Weight vs Fuel Efficiency",
subtitle = "Size represents horsepower, color represents cylinders",
x = "Weight (tons)",
y = "Miles per Gallon"
) +
theme_minimal()
A 3D scatter plot of iris flower dimensions, colored by species.
scatter3D(iris$Sepal.Length, iris$Sepal.Width, iris$Petal.Length,
phi = 0, bty = "g", pch = 20, cex = 2,
xlab = "Sepal Length", ylab = "Sepal Width", zlab = "Petal Length",
main = "Iris Flower Dimensions in 3D",
colvar = as.numeric(iris$Species),
col = c("blue", "green", "red"),
clab = "Species")
A violin plot showing the density of sepal length across iris species.
ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) +
geom_violin(alpha = 0.7) +
theme_minimal() +
labs(title = "Distribution of Sepal Length by Species",
x = "Species",
y = "Sepal Length (cm)") +
scale_fill_discrete(name = "Iris Species")
An interactive 3D scatter plot of car characteristics.
plot_ly(mtcars, x = ~mpg, y = ~hp, z = ~wt, color = ~factor(cyl)) %>%
add_markers() %>%
layout(title = "3D Car Characteristics",
scene = list(xaxis = list(title = 'Miles per Gallon'),
yaxis = list(title = 'Horsepower'),
zaxis = list(title = 'Weight (tons)')))
A scatter plot with a continuous color scale for petal length.
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Petal.Length)) +
geom_point(size = 3) +
scale_color_viridis(name = "Petal Length") +
labs(title = "Sepal Dimensions Colored by Petal Length",
x = "Sepal Length (cm)",
y = "Sepal Width (cm)") +
theme_minimal()
A hexbin plot showing the density of data points for MPG and horsepower.
ggplot(mtcars, aes(x = mpg, y = hp)) +
geom_hex(bins = 15) +
scale_fill_viridis(name = "Count") +
labs(title = "Density of Car MPG and Horsepower Values",
x = "Miles per Gallon",
y = "Horsepower") +
theme_minimal()
A 2D density contour plot for sepal dimensions.
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
geom_density_2d_filled(alpha = 0.7) +
labs(title = "Density Contours of Sepal Dimensions",
x = "Sepal Length (cm)",
y = "Sepal Width (cm)") +
theme_minimal()
A mosaic plot showing survival rates by passenger class.
data(Titanic)
titanic_df <- as.data.frame(Titanic)
ggplot(titanic_df) +
geom_mosaic(aes(weight = Freq, x = product(Class), fill = Survived)) +
labs(title = "Titanic Survival Rates by Passenger Class",
x = "Passenger Class",
y = "Proportion") +
scale_fill_discrete(name = "Survived") +
theme_minimal()
An interactive 3D sphere plot of iris dimensions, rendered for HTML compatibility.
rgl.open()
rgl.spheres(iris$Sepal.Length, iris$Sepal.Width, iris$Petal.Length,
r = 0.1, color = as.numeric(iris$Species))
rgl.bbox()
title3d("3D Iris Flower Dimensions",
xlab = "Sepal Length", ylab = "Sepal Width", zlab = "Petal Length")
legend3d("topright", legend = levels(iris$Species),
pch = 16, col = 1:3, cex = 1, inset = 0.05)
rglwidget()
A scatter plot with marginal histograms for MPG and horsepower.
p <- ggplot(mtcars, aes(x = mpg, y = hp, color = factor(cyl))) +
geom_point() +
scale_color_discrete(name = "Cylinders") +
labs(title = "MPG vs Horsepower with Marginal Histograms",
x = "Miles per Gallon",
y = "Horsepower") +
theme_minimal()
ggMarginal(p, type = "histogram", groupColour = TRUE)
A heatmap of correlations between mtcars variables,
implemented in ggplot2.
cor_data <- cor(mtcars)
cor_melt <- melt(cor_data)
ggplot(cor_melt, aes(x = Var1, y = Var2, fill = value)) +
geom_tile() +
scale_fill_gradient2(low = "blue", mid = "white", high = "red", midpoint = 0, name = "Correlation") +
labs(title = "Correlation Heatmap of Car Characteristics") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
A topographic image plot of the volcano dataset,
implemented in ggplot2.
volcano_df <- as.data.frame.table(volcano)
ggplot(volcano_df, aes(x = Var1, y = Var2, fill = Freq)) +
geom_tile() +
scale_fill_gradientn(colors = terrain.colors(20), name = "Elevation") +
labs(title = "Topography of Maunga Whau Volcano",
x = "X Coordinate",
y = "Y Coordinate") +
theme_minimal()