Learn how to transform raw data into meaningful visual stories, starting with basic charts and progressing to advanced interactive dashboards.
Data Visualization in R: A Complete Guide
1. Basic Plots: The Foundation
What: Simple charts for quick data exploration
When to use: Initial data analysis, checking distributions, spotting outliers
# Scatterplot (relationships)
plot(mtcars$wt, mtcars$mpg, main = "Weight vs MPG")
# Histogram (distributions)
hist(mtcars$mpg, breaks = 10, col = "skyblue")
# Boxplot (comparisons)
boxplot(mpg ~ cyl, data = mtcars, col = rainbow(3))
2. ggplot2: Grammar of Graphics
What: Systematic approach to building plots layer by layer
When to use: When you need customizable, publication-quality plots
library(ggplot2)
# Basic ggplot
ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_point(aes(color = Species)) +
labs(title = "Iris Sepal Dimensions",
x = "Length (cm)",
y = "Width (cm)") +
theme_bw()
3. Specialized Charts
What: Advanced chart types for specific scenarios
When to use: When standard plots can't reveal complex relationships
# Heatmap (matrix data)
ggplot(airquality, aes(Month, Day, fill = Temp)) +
geom_tile() +
scale_fill_gradient(low = "white", high = "red")
# Violin plot (distribution + density)
ggplot(iris, aes(Species, Sepal.Length, fill = Species)) +
geom_violin(alpha = 0.7) +
geom_boxplot(width = 0.1)
# Sankey diagram (flows)
library(ggalluvial)
ggplot(as.data.frame(Titanic),
aes(axis1 = Class, axis2 = Sex, y = Freq)) +
geom_alluvium(aes(fill = Survived)) +
geom_stratum()
4. Interactive Visualizations
What: Plots that respond to user input
When to use: Web applications, exploratory data analysis
library(plotly)
# Convert ggplot to interactive
p <- ggplot(mtcars, aes(wt, mpg, color = cyl)) +
geom_point()
ggplotly(p)
# Standalone plotly
plot_ly(iris, x = ~Sepal.Length, y = ~Petal.Length,
color = ~Species, type = "scatter",
mode = "markers")
5. Dashboards & Multi-Plot Layouts
What: Arranging multiple visualizations together
When to use: When you need to tell a complete data story
library(patchwork)
library(cowplot)
# Using patchwork
p1 <- ggplot(mtcars, aes(mpg)) + geom_histogram()
p2 <- ggplot(mtcars, aes(factor(cyl), mpg)) + geom_boxplot()
p1 + p2 # Side by side
p1 / p2 # Stacked
# Using cowplot
plot_grid(p1, p2, labels = c("A", "B"), ncol = 2)
6. Advanced Customization
What: Fine-tuning every visual element
When to use: Final polishing for presentations/publications
# Custom themes
ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_point() +
theme(
panel.background = element_rect(fill = "lightgray"),
panel.grid.major = element_line(color = "white"),
axis.text = element_text(face = "bold"),
plot.title = element_text(size = 20, hjust = 0.5)
) +
labs(title = "Custom Styled Plot")
# Annotations
ggplot(economics, aes(date, unemploy)) +
geom_line() +
annotate("rect", xmin = as.Date("1980-01-01"),
xmax = as.Date("1985-01-01"),
ymin = 0, ymax = Inf, alpha = 0.2, fill = "red") +
annotate("text", x = as.Date("1982-01-01"), y = 12000,
label = "Recession Period", color = "red")
7. Geospatial Visualization
What: Maps and location-based data visualization
When to use: When your data has geographic components
library(sf)
library(maps)
# Simple map
world <- map_data("world")
ggplot(world, aes(long, lat, group = group)) +
geom_polygon(fill = "lightgreen", color = "black")
# Choropleth map
us_map <- map_data("state")
data <- data.frame(region = tolower(state.name),
value = runif(50))
ggplot(us_map, aes(long, lat, group = group)) +
geom_polygon(aes(fill = value), data = data) +
scale_fill_gradient(low = "white", high = "blue")