一、先搞懂:R 绘图两大体系
- 基础绘图(base plot):简单、快,但不够美观
- ggplot2(推荐):专业、美观、行业标准,必须学
二、第一步:安装并加载工具包
# 安装(只装一次)
install.packages("ggplot2")
install.packages("dplyr")
# 加载(每次打开 R 都要运行)
library(ggplot2)
library(dplyr)
三、ggplot2 核心语法(万能公式)
所有图都用这一套逻辑,记住就会画所有图:
ggplot(数据, aes(x = 横轴, y = 纵轴, color = 分组)) +
geom_xxx(图形类型) +
labs(标题、坐标轴) +
theme(主题美化)
四、最常用 8 种图(直接复制运行)
我用自带数据集 iris(鸢尾花)演示。
1. 散点图(看两个变量关系)
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, color = Species)) +
geom_point(size = 2) +
labs(title = "花萼长度 vs 花瓣长度")
2. 折线图(看趋势)
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, group = Species, color = Species)) +
geom_line(linewidth = 1)
3. 柱状图(看数量/比较)
iris %>%
group_by(Species) %>%
summarise(mean_len = mean(Petal.Length)) %>%
ggplot(aes(x = Species, y = mean_len, fill = Species)) +
geom_col()
4. 箱线图(看分布、异常值)
ggplot(iris, aes(x = Species, y = Petal.Length, fill = Species)) +
geom_boxplot()
5. 直方图(看数据分布)
ggplot(iris, aes(x = Petal.Length)) +
geom_histogram(bins = 15, fill = "steelblue")
6. 密度图(更平滑的分布)
ggplot(iris, aes(x = Petal.Length, fill = Species)) +
geom_density(alpha = 0.5)
7. 小提琴图(高级分布)
ggplot(iris, aes(x = Species, y = Petal.Length, fill = Species)) +
geom_violin()
8. 热图(相关性)
cormat <- round(cor(iris[,1:4]), 2)
melt_cor <- reshape2::melt(cormat)
ggplot(melt_cor, aes(x = Var1, y = Var2, fill = value)) +
geom_tile() +
geom_text(aes(label = value)) +
scale_fill_gradient2(low = "blue", high = "red")
五、图片美化(一键变专业)
1. 加标题、坐标轴
labs(
title = "主标题",
subtitle = "副标题",
x = "横轴名称",
y = "纵轴名称",
color = "分组名称"
)
2. 换主题
theme_minimal() # 简洁
theme_bw() # 黑白
theme_classic() # 论文风
theme_void() # 无坐标轴
3. 居中标题
theme(plot.title = element_text(hjust = 0.5))
六、导出高清图片(论文/报告用)
ggsave("myplot.png", width = 8, height = 5, dpi = 300)
- 300dpi 高清
- 可导出 PNG/PDF/EPS/SVG
七、完整示例(可直接当模板)
library(ggplot2)
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, color = Species)) +
geom_point(size = 2, alpha = 0.7) +
geom_smooth(method = "lm") +
labs(
title = "鸢尾花性状相关性分析",
x = "花萼长度 (cm)",
y = "花瓣长度 (cm)"
) +
theme_minimal() +
theme(plot.title = element_text(hjust = 0.5))