ggplot2 builds plots layer by layer. Every plot has data + aesthetics (aes()) + geometry (geom_*()). Produces publication-quality graphics.
Load: library(ggplot2) (base R auto-loaded)
| Function | Signature | Description |
|---|---|---|
| ggplot() | ggplot(data, aes(x,y,color,...)) → gg | Initialize plot with data and aesthetics |
| aes() | aes(x=col, y=col, color=col, fill=col) | Map data columns to visual properties |
| geom_point() | geom_point(aes, size, alpha, shape) | Scatter plot points |
| geom_line() | geom_line(aes, linewidth, linetype) | Line chart |
| geom_bar() | geom_bar(aes(x), stat='count') | Bar chart (counts) |
| geom_col() | geom_col(aes(x,y)) | Bar chart (values) |
| geom_histogram() | geom_histogram(aes(x), bins=30) | Histogram |
| geom_density() | geom_density(aes(x,fill), alpha) | Density curve |
| geom_boxplot() | geom_boxplot(aes(x,y)) | Box and whisker plot |
| geom_violin() | geom_violin(aes(x,y)) | Violin plot |
| geom_smooth() | geom_smooth(method='lm', se=TRUE) | Smoothed trend line |
| geom_text() | geom_text(aes(label=col)) | Text labels at data points |
| geom_hline() | geom_hline(yintercept=val) | Horizontal reference line |
| geom_vline() | geom_vline(xintercept=val) | Vertical reference line |
| facet_wrap() | facet_wrap(~col, ncol=2) | Multiple panels by one variable |
| facet_grid() | facet_grid(row~col) | Grid of panels by two variables |
| scale_x_log10() | scale_x_log10() | Logarithmic x-axis |
| scale_color_manual() | scale_color_manual(values=c(...)) | Custom discrete colors |
| labs() | labs(title, x, y, color, caption) | Add title and axis labels |
| theme() | theme(text, axis, legend, panel) | Customize non-data elements |
| theme_minimal() | theme_minimal() | Clean minimal theme |
| theme_bw() | theme_bw() | Black/white theme |
| coord_flip() | coord_flip() | Swap x and y axes |
| ggsave() | ggsave('plot.png', width=8, height=5, dpi=300) | Save plot to file |
Run with Rscript script.R.
library(ggplot2)
set.seed(123)
df <- data.frame(
x = rnorm(200, mean=50, sd=10),
y = rnorm(200, mean=70, sd=15),
group = sample(c("A","B","C"), 200, replace=TRUE),
score = sample(60:100, 200, replace=TRUE)
)
# Scatter with trend line
p1 <- ggplot(df, aes(x=x, y=y, color=group)) +
geom_point(alpha=0.6, size=2) +
geom_smooth(method="lm", se=TRUE) +
geom_hline(yintercept=mean(df$y), linetype="dashed", color="gray50") +
labs(title="Scatter Plot by Group", x="Predictor X", y="Response Y",
caption="MyWebUniversity — R Reference") +
theme_minimal(base_size=12)
ggsave("scatter.png", p1, width=8, height=5, dpi=150)
cat("Saved scatter.png\n")
# Histogram with density
p2 <- ggplot(df, aes(x=score)) +
geom_histogram(aes(y=after_stat(density)), bins=20,
fill="steelblue", alpha=0.7, color="white") +
geom_density(color="red", linewidth=1) +
geom_vline(xintercept=mean(df$score), color="orange", linetype="dashed") +
labs(title="Score Distribution", x="Score", y="Density") +
theme_bw()
ggsave("histogram.png", p2, width=7, height=4, dpi=150)
cat("Saved histogram.png\n")
# Faceted boxplot
p3 <- ggplot(df, aes(x=group, y=score, fill=group)) +
geom_boxplot(alpha=0.7) +
facet_wrap(~group, scales="free_x") +
labs(title="Score by Group") +
theme_minimal() + theme(legend.position="none")
ggsave("boxplot.png", p3, width=9, height=4, dpi=150)
cat("Saved boxplot.png\n")
# Rscript ggplot_demo.R (install.packages("ggplot2"))