RStudio GGplot Cheat Sheet | RStudioDataLab
Discover the ultimate RStudio GGplot Cheat Sheet at RStudioDataLab. Enhance your data visualization skills with quick tips and essential commands.
Table of Contents
Scatter Plot (geom_point)
Code
ggplot(mtcars, aes(x = mpg, y = wt)) +geom_point(color = "darkblue", size = 3) +ggtitle("MPG vs Weight")
Copy
Line Chart (geom_line)
Code
ggplot(mtcars, aes(x = seq_along(mpg), y = mpg)) +
geom_line(color = "green", linewidth = 1) +
ggtitle("Line Chart of MPG")
Copy
Bar Chart (geom_bar)
Code
ggplot(mtcars, aes(x = factor(cyl))) +
geom_bar(fill = "orange") +
ggtitle("Cylinder Count Bar Chart")
Copy
Column Chart (geom_col)
Code
ggplot(mtcars, aes(x = factor(cyl), y = mpg)) +
geom_col(fill = "purple") +
ggtitle("Average MPG by Cylinders")
Copy
Histogram (geom_histogram)
Code
ggplot(mtcars, aes(x = mpg)) +
geom_histogram(bins = 10, fill = "seagreen", color = "black") +
ggtitle("MPG Distribution")
Copy
Density Plot (geom_density)
Code
ggplot(mtcars, aes(x = mpg)) +
geom_density(fill = "blue", alpha = 0.5) +
ggtitle("MPG Density Plot")
Copy
Box Plot (geom_boxplot)
Code
ggplot(mtcars, aes(x = factor(cyl), y = mpg)) +
geom_boxplot(fill = "seagreen") +
ggtitle("MPG by Cylinders")
Copy
Violin Plot (geom_violin)
Code
ggplot(mtcars, aes(x = factor(cyl), y = mpg)) +
geom_violin(fill = "pink") +
ggtitle("MPG Violin Plot")
Copy
Area Chart (geom_area)
Code
ggplot(mtcars, aes(x = seq_along(mpg), y = mpg)) +
geom_area(fill = "lightgreen", alpha = 0.5) +
ggtitle("MPG Area Chart")
Copy
Stacked Area Chart (geom_area)
Code
ggplot(mtcars, aes(x = seq_along(mpg), y = mpg, fill = factor(cyl))) +
geom_area(position = "stack") +
ggtitle("Stacked MPG by Cylinders")
Copy
Stacked Bar Chart (geom_bar + position="stack")
Code
ggplot(mtcars, aes(x = factor(cyl), fill = factor(gear))) +
geom_bar(position = "stack") +
ggtitle("Stacked Bar: Cylinders by Gear")
Copy
Grouped Bar Chart (geom_bar + position="dodge")
Code
ggplot(mtcars, aes(x = factor(cyl), fill = factor(gear))) +
geom_bar(position = "dodge") +
ggtitle("Grouped Bar: Cylinders by Gear")
Copy
Dot Plot (geom_dotplot)
Code
ggplot(mtcars, aes(x = mpg)) +
geom_dotplot(binwidth = 1, fill = "darkgreen") +
ggtitle("Dot Plot of MPG")
Copy
Jitter Plot (geom_jitter)
Code
ggplot(mtcars, aes(x = factor(cyl), y = mpg)) +
geom_jitter(width = 0.2, color = "purple") +
ggtitle("Jitter Plot: MPG by Cylinders")
Copy
Bubble Chart (geom_point with size)
Code
ggplot(mtcars, aes(x = wt, y = mpg, size = hp)) +
geom_point(alpha = 0.7, color = "darkred") +
scale_size(range = c(2, 10)) +
ggtitle("Bubble: Weight vs MPG by HP")
Copy
Heatmap (geom_tile)
Code
library(dplyr)
mtcars %>%
group_by(cyl, gear) %>%
summarise(mean_mpg = mean(mpg)) %>%
ggplot(aes(x = factor(cyl), y = factor(gear), fill = mean_mpg)) +
geom_tile() +
scale_fill_gradient(low = "white", high = "blue") +
ggtitle("Heatmap: Mean MPG by Cyl & Gear")
Copy
Contour Plot (geom_contour)
Code
ggplot(mtcars, aes(x = mpg, y = wt)) +
geom_density_2d(color = "darkorange") +
ggtitle("Contour Plot of MPG vs Weight")
Copy
2D Density Plot (geom_density2d)
Code
ggplot(mtcars, aes(x = mpg, y = wt)) +
geom_density_2d_filled(alpha = 0.7) +
ggtitle("2D Density Plot of MPG vs Weight")
Copy
Pie Chart (geom_bar + coord_polar)
Code
ggplot(mtcars, aes(x = "", fill = factor(cyl))) +
geom_bar(width = 1) +
coord_polar(theta = "y") +
theme_void() +
ggtitle("Pie Chart of Cylinders")
Copy
Donut Chart (geom_bar + coord_polar)
Code
ggplot(mtcars, aes(x = 2, fill = factor(cyl))) +
geom_bar(width = 1) +
coord_polar(theta = "y") +
xlim(0.5, 2.5) +
theme_void() +
ggtitle("Donut Chart of Cylinders")
Copy
Polar Area Chart (coord_polar)
Code
ggplot(mtcars, aes(x = factor(cyl), fill = factor(cyl))) +
geom_bar(width = 1) +
coord_polar() +
theme_void() +
ggtitle("Polar Area: Cylinders")
Copy
Radar Chart (coord_polar + geom_line)
Code
library(dplyr)
mtcars %>%
group_by(cyl) %>%
summarise(mpg = mean(mpg)) %>%
ggplot(aes(x = factor(cyl), y = mpg, group = 1)) +
geom_line(color = "blue") +
coord_polar() +
ggtitle("Radar Chart: Avg MPG by Cyl")
Copy
Error Bar Plot (geom_errorbar)
Code
library(dplyr)
mtcars %>%
group_by(cyl) %>%
summarise(mean_mpg = mean(mpg), sd_mpg = sd(mpg)) %>%
ggplot(aes(x = factor(cyl), y = mean_mpg)) +
geom_col(fill = "skyblue") +
geom_errorbar(aes(ymin = mean_mpg - sd_mpg, ymax = mean_mpg + sd_mpg), width = 0.2) +
ggtitle("Error Bars: MPG by Cyl")
Copy
Crossbar Plot (geom_crossbar)
Code
library(dplyr)
mtcars %>%
group_by(cyl) %>%
summarise(mean_mpg = mean(mpg), sd_mpg = sd(mpg)) %>%
ggplot(aes(x = factor(cyl), y = mean_mpg)) +
geom_crossbar(aes(ymin = mean_mpg - sd_mpg, ymax = mean_mpg + sd_mpg),
fill = "orange") +
ggtitle("Crossbar: MPG by Cyl")
Copy
Point Range Plot (geom_pointrange)
Code
library(dplyr)
mtcars %>%
group_by(cyl) %>%
summarise(mean_mpg = mean(mpg), sd_mpg = sd(mpg)) %>%
ggplot(aes(x = factor(cyl), y = mean_mpg)) +
geom_pointrange(aes(ymin = mean_mpg - sd_mpg, ymax = mean_mpg + sd_mpg),
color = "darkred") +
ggtitle("Point Range: MPG by Cyl")
Copy
Linerange Plot (geom_linerange)
Code
library(dplyr)
mtcars %>%
group_by(cyl) %>%
summarise(ymin = min(mpg), ymax = max(mpg)) %>%
ggplot(aes(x = factor(cyl))) +
geom_linerange(aes(ymin = ymin, ymax = ymax), size = 1.5, color = "purple") +
ggtitle("Linerange: MPG Range by Cyl")
Copy
Ribbon Plot (geom_ribbon)
Code
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
geom_ribbon(aes(ymin = predict(lm(mpg ~ wt)) - 5,
ymax = predict(lm(mpg ~ wt)) + 5),
alpha = 0.2, fill = "blue") +
ggtitle("Ribbon: Confidence Band")
Copy
Smooth Line Plot (geom_smooth)
Code
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
geom_smooth(method = "loess", color = "red", se = FALSE) +
ggtitle("Smooth Line: Weight vs MPG")
Copy
Quantile Regression (geom_quantile)
Code
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
geom_quantile(quantiles = c(0.25, 0.5, 0.75), color = "darkgreen") +
ggtitle("Quantile Regression Lines")
Copy
Q-Q Plot (geom_qq)
Code
ggplot(mtcars, aes(sample = mpg)) +
geom_qq(distribution = qnorm) +
geom_qq_line(color = "red") +
ggtitle("Q-Q Plot of MPG")
Copy
Rug Plot (geom_rug)
Code
ggplot(mtcars, aes(x = mpg, y = wt)) +
geom_point() +
geom_rug(sides = "bl", color = "red") +
ggtitle("MPG vs Weight with Rug Marks")
Copy
Text Label Plot (geom_text)
Code
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
geom_text(aes(label = car), check_overlap = TRUE, size = 3, vjust = -1) +
ggtitle("Car Labels on Weight vs MPG")
Copy
Label Plot (geom_label)
Code
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
geom_label(aes(label = cyl), fill = "yellow", alpha = 0.5) +
ggtitle("Cylinder Labels on Weight vs MPG")
Copy
Choropleth Map (geom_tile)
Code
# Using cyl as "regions"
ggplot(mtcars, aes(x = factor(cyl), y = 1, fill = mpg)) +
geom_tile() +
scale_fill_gradient(low = "white", high = "blue") +
theme_void() +
ggtitle("Pseudo-Choropleth (Cyl as Regions)")
Copy
Hexagonal Binning (geom_hex)
Code
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_hex(bins = 10) +
scale_fill_gradient(low = "lightblue", high = "darkblue") +
ggtitle("Hex Binning: Weight vs MPG")
Copy
2D Bin Plot (geom_bin2d)
Code
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_bin2d(bins = 8) +
scale_fill_gradient(low = "lightgreen", high = "darkgreen") +
ggtitle("2D Binning: Weight vs MPG")
Copy
Count Overlap (geom_count)
Code
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_count(color = "purple") +
ggtitle("Overlap Counts: Weight vs MPG")
Copy
Segment Plot (geom_segment)
Code
ggplot(mtcars) +
geom_segment(aes(x = wt, y = mpg, xend = mean(wt), yend = mean(mpg)),
color = "gray") +
geom_point(aes(x = wt, y = mpg), color = "red") +
ggtitle("Segments to Mean")
Copy
Curve Plot (geom_curve)
Code
ggplot(mtcars) +
geom_curve(aes(x = min(wt), y = min(mpg),
xend = max(wt), yend = max(mpg)),
curvature = 0.3, color = "blue") +
xlim(1, 6) + ylim(10, 35) +
ggtitle("Curve from Min to Max")
Copy
Step Plot (geom_step)
Code
ggplot(mtcars, aes(x = seq_along(mpg), y = sort(mpg))) +
geom_step(color = "darkgreen") +
ggtitle("Step Plot of Sorted MPG")
Copy
Function Plot (geom_function)
Code
ggplot(mtcars, aes(x = wt)) +
geom_point(aes(y = mpg)) +
geom_function(fun = function(x) 37 - 5*x, color = "red") +
ggtitle("Linear Function Overlay")
Copy
ECDF Plot (stat_ecdf)
Code
ggplot(mtcars, aes(x = mpg)) +
stat_ecdf(geom = "step", color = "purple") +
ggtitle("ECDF of MPG")
Copy
Path Plot (geom_path)
Code
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_path(color = "orange") +
ggtitle("Path Plot: Weight vs MPG")
Copy
Polygon Plot (geom_polygon)
Code
library(dplyr)
mtcars %>%
arrange(wt) %>%
ggplot(aes(x = wt, y = mpg)) +
geom_polygon(fill = "lightblue", alpha = 0.5) +
ggtitle("Polygon: Weight vs MPG")
Copy
Reference Line (geom_hline)
Code
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
geom_hline(yintercept = mean(mtcars$mpg), color = "red") +
geom_vline(xintercept = mean(mtcars$wt), color = "blue") +
ggtitle("Reference Lines at Means")
Copy
Rectangular Annotation (geom_rect)
Code
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
geom_rect(aes(xmin = 3, xmax = 4, ymin = 15, ymax = 25),
fill = "yellow", alpha = 0.2) +
ggtitle("Rectangular Highlight")
Copy
Tile Plot (geom_tile)
Code
library(dplyr)
mtcars %>%
group_by(cyl, gear) %>%
summarise(mean_mpg = mean(mpg)) %>%
ggplot(aes(x = factor(cyl), y = factor(gear), fill = mean_mpg)) +
geom_tile() +
ggtitle("Tile Plot: Mean MPG")
Copy
Cleveland Dot Plot
Code
ggplot(mtcars, aes(x = mpg, y = reorder(rownames(mtcars), mpg))) +
geom_point(color = "darkred") +
labs(y = "Car") +
ggtitle("Cleveland Dot Plot")
Copy
Mosaic Plot (geom_rect)
Code
library(dplyr)
mtcars %>%
count(cyl, gear) %>%
group_by(cyl) %>%
mutate(xmax = cumsum(n)/sum(n), xmin = lag(xmax, default = 0)) %>%
ggplot() +
geom_rect(aes(xmin = xmin, xmax = xmax, ymin = as.numeric(cyl)-0.4,
ymax = as.numeric(cyl)+0.4, fill = factor(gear))) +
scale_y_continuous(breaks = unique(mtcars$cyl)) +
ggtitle("Mosaic: Cyl vs Gear")
...
Treemap (geom_tile)
Code
library(treemapify)
mtcars %>%
group_by(cyl, gear) %>%
summarise(size = n()) %>%
ggplot(aes(area = size, fill = factor(cyl), subgroup = gear)) +
geom_treemap() +
geom_treemap_subgroup_border() +
ggtitle("Treemap: Cyl and Gear")
...
Candlestick Plot
Code
mtcars %>%
group_by(cyl) %>%
summarise(open = min(mpg), high = max(mpg), low = min(mpg), close = max(mpg)) %>%
ggplot(aes(x = factor(cyl))) +
geom_linerange(aes(ymin = low, ymax = high), size = 1) +
geom_rect(aes(ymin = open, ymax = close, xmin = as.numeric(cyl)-0.2,
xmax = as.numeric(cyl)+0.2), fill = "darkgreen") +
ggtitle("Candlestick: MPG by Cyl")
...
Marginal Plot
Code
library(ggExtra)
p - ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point()
ggMarginal(p, type = "histogram")
...
Streamgraph
Code
library(ggstream)
mtcars %>%
mutate(idx = seq_along(mpg)) %>%
ggplot(aes(x = idx, y = mpg, fill = factor(cyl))) +
geom_stream() +
ggtitle("Streamgraph of MPG")
...
Dot Matrix Plot
Code
ggplot(mtcars, aes(x = factor(cyl), y = factor(gear))) +
geom_point(position = position_jitter(width = 0.2, height = 0.2),
size = 3, alpha = 0.7) +
ggtitle("Dot Matrix: Cyl vs Gear")
...
Waffle Chart
Code
library(waffle)
waffle(cyl_counts/2, rows = 4, colors = c("#E69F00", "#56B4E9", "#009E73")) +
ggtitle("Waffle Chart of Cylinders")
...