ggplot2 是 R 语言中最流行的数据可视化包之一,它基于图形语法理论,通过图层叠加的方式构建图形。ggplot2 的核心思想是将数据、映射、几何对象和美学属性分离,使图形构建更加灵活和模块化。这份DeepSeek提供的教程涵盖了ggplot2的基本用法,包括:①基本图形类型:散点图、折线图、柱状图、箱线图、直方图、密度图;②图形美化:颜色设置、标题标签、主题定制;③多图组合:分面和图形组合;④图形保存。

## 安装和加载必要的包
# install.packages(c("ggplot2", "RColorBrewer", "patchwork", "export"))
library(ggplot2)
library(RColorBrewer)
library(patchwork)

## 模拟数据集创建代码
set.seed(123)
n <- 200  # 总样本量

# 数据框创建
sim_data <- data.frame(
  group = rep(c("A", "B", "C"), length.out = n),  # 循环填充直到达到n
  category = rep(c("X", "Y"), length.out = n),     # 循环填充直到达到n
  value = c(rnorm(n/2, mean = 50, sd = 10),       # 前100个观测
            rnorm(n/2, mean = 70, sd = 15)),           # 后100个观测
  time = rep(1:10, each = n/10),                  # 10个时间点,每个20个观测
  score = runif(n, min = 0, max = 100)            # n个随机数
)

# 检查数据结构
str(sim_data)
head(sim_data)

## 1. 基本图形绘制
# 1.1 散点图
p1 <- ggplot(sim_data, aes(x = time, y = value)) + geom_point()
p2 <- ggplot(sim_data, aes(x = time, y = value, color = group)) + 
  geom_point(size = 3, alpha = 0.7)
p3 <- ggplot(sim_data, aes(x = time, y = value, color = group, shape = category)) +
  geom_point(size = 3)
p3

# 1.2 折线图
p4 <- ggplot(sim_data, aes(x = time, y = value)) + geom_line()
p5 <- ggplot(sim_data, aes(x = time, y = value, color = group)) + 
  geom_line(size = 1)
p6 <- ggplot(sim_data, aes(x = time, y = value, color = group)) +
  geom_line(size = 1) + geom_point(size = 2)
p6

# 1.3 柱状图
p7 <- ggplot(sim_data, aes(x = group)) + geom_bar()
p8 <- ggplot(sim_data, aes(x = group, fill = category)) +
  geom_bar(position = "dodge")
p9 <- ggplot(sim_data, aes(x = group, fill = category)) +
  geom_bar(position = "stack")
p10 <- ggplot(sim_data, aes(x = group, fill = category)) +
  geom_bar(position = "fill")
p10

# 1.4 箱线图
p11 <- ggplot(sim_data, aes(x = group, y = value)) + geom_boxplot()
p12 <- ggplot(sim_data, aes(x = group, y = value, fill = group)) +
  geom_boxplot(alpha = 0.7) + geom_jitter(width = 0.2, alpha = 0.5)
p12

# 1.5 直方图
p13 <- ggplot(sim_data, aes(x = value)) +
  geom_histogram(binwidth = 5, fill = "blue", color = "black")
p14 <- ggplot(sim_data, aes(x = value, fill = group)) +
  geom_histogram(binwidth = 5, alpha = 0.7, position = "identity")
p14

# 1.6 密度图
p15 <- ggplot(sim_data, aes(x = value)) +
  geom_density(fill = "blue", alpha = 0.5)
p16 <- ggplot(sim_data, aes(x = value, fill = group)) +
  geom_density(alpha = 0.5)
p16

## 2. 图形美化
# 2.1 颜色设置
display.brewer.all()
p17 <- ggplot(sim_data, aes(x = time, y = value, color = group)) +
  geom_point(size = 3) + scale_color_brewer(palette = "Set1")
p18 <- ggplot(sim_data, aes(x = time, y = value, color = group)) +
  geom_point(size = 3) + scale_color_manual(values = c("A" = "red", "B" = "blue", "C" = "green"))
p18

# 2.2 标题和标签
p19 <- ggplot(sim_data, aes(x = time, y = value, color = group)) +
  geom_point(size = 3) +
  labs(title = "Value over Time by Group",
       subtitle = "Simulated Data Example",
       x = "Time Point",
       y = "Measured Value",
       color = "Experimental\nGroup")
p19

# 2.3 主题设置
my_theme <- theme(
  plot.title = element_text(size = 16, face = "bold", hjust = 0.5),
  axis.title = element_text(size = 12),
  axis.text = element_text(size = 10),
  legend.title = element_text(size = 12),
  legend.position = "bottom",
  panel.background = element_rect(fill = "white"),
  panel.grid.major = element_line(color = "grey90"),
  panel.grid.minor = element_blank()
)

p20 <- ggplot(sim_data, aes(x = time, y = value, color = group)) +
  geom_point(size = 3) + labs(title = "Custom Theme Example") + my_theme
p20

## 3. 多图组合

# 3.1 分面
p21 <- ggplot(sim_data, aes(x = time, y = value)) + geom_point() + facet_grid(group ~ .)
p22 <- ggplot(sim_data, aes(x = time, y = value)) + geom_point() + facet_grid(. ~ group)
p23 <- ggplot(sim_data, aes(x = time, y = value)) + geom_point() + facet_grid(category ~ group)
p24 <- ggplot(sim_data, aes(x = time, y = value)) + geom_point() + facet_wrap(~ group + category, ncol = 2)
p24

# 3.2 图形组合
p_box <- ggplot(sim_data, aes(x = group, y = value)) + geom_boxplot()
p_density <- ggplot(sim_data, aes(x = value, fill = group)) + geom_density(alpha = 0.5)

combined_plot1 <- p_box + p_density  # 并排排列
combined_plot2 <- p_box / p_density  # 上下排列
combined_plot3 <- (p_box + p_density) / p_box  # 复杂布局
combined_plot3

## 4. 综合示例
final_plot <- ggplot(sim_data, aes(x = time, y = value, color = group, shape = category)) +
  geom_point(size = 3, alpha = 0.8) +
  geom_smooth(method = "lm", se = FALSE) +
  scale_color_brewer(palette = "Dark2") +
  facet_wrap(~ group, ncol = 1) +
  labs(title = "Comprehensive Example Plot",
       subtitle = "Showing trends over time by group and category",
       x = "Time Point",
       y = "Measurement Value",
       color = "Group",
       shape = "Category") +
  theme_minimal() +
  theme(
    plot.title = element_text(size = 16, face = "bold", hjust = 0.5),
    plot.subtitle = element_text(size = 12, hjust = 0.5),
    legend.position = "bottom",
    strip.text = element_text(face = "bold")
  )
final_plot

## 5. 图形保存
ggsave("final_plot.png", final_plot, width = 10, height = 8, dpi = 300)

# 使用export包保存多种格式
graph2png("my_plot.png", width = 8, height = 6)
graph2pdf("my_plot.pdf", width = 8, height = 6)
graph2ppt("my_plot.pptx", width = 8, height = 6)

## 显示部分图形
print(p1)
print(p12)
print(p19)
print(final_plot)