You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用ggplot2将R中含连续值的data.frame转为颜色矩阵图?

用ggplot2可视化带连续值的Data Frame(入门友好版)

Hey there! 刚接触R和ggplot2完全不用犯愁,我来一步步带你把带连续值的data.frame转换成美观的可视化图形~ 先从基础示例开始,再给你介绍几种最常用的图形类型和美化技巧,都是新手能快速上手的内容。

第一步:准备示例数据

首先咱们先模拟一个带连续值的data.frame(你可以把这个替换成自己的真实数据):

set.seed(123) # 让结果可重复,方便你复现
df <- data.frame(
  time = seq(1, 30, by = 1),          # 连续时间序列
  temperature = rnorm(30, 25, 3),     # 连续数值:温度
  humidity = rnorm(30, 60, 8)         # 连续数值:湿度
)
head(df) # 查看前6行数据

常用可视化类型(附美化代码)

根据你的需求,我整理了几种最适合展示连续值的图形,每个都带完整代码和简单解释:

1. 散点图:展示两个连续变量的相关性

适合看比如温度和时间的关系,或者湿度和温度的关联:

library(ggplot2)

ggplot(df, aes(x = time, y = temperature)) +
  geom_point(color = "#2A9D8F", size = 3, alpha = 0.7) + # 自定义点的颜色、大小和透明度
  geom_smooth(method = "lm", color = "#E76F51", linetype = "dashed") + # 添加线性拟合线,看趋势
  labs(
    title = "温度随时间的变化",
    x = "时间(天)",
    y = "温度(℃)"
  ) +
  theme_minimal() + # 用简洁的极简主题
  theme(
    plot.title = element_text(hjust = 0.5, size = 14, face = "bold"), # 标题居中、加粗
    axis.title = element_text(size = 12) # 调整坐标轴标签大小
  )

小技巧:如果想同时展示两个数值变量(比如温度和湿度),可以把数据转成长格式,用颜色区分变量:

library(tidyr)
df_long <- pivot_longer(df, cols = c(temperature, humidity), names_to = "metric", values_to = "value")

ggplot(df_long, aes(x = time, y = value, color = metric)) +
  geom_point(size = 2) +
  scale_color_manual(values = c("#2A9D8F", "#F4A261")) + # 自定义配色
  labs(title = "温度与湿度随时间变化", x = "时间(天)", y = "数值", color = "指标") +
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5, size = 14, face = "bold"))

2. 折线图:展示连续变量的变化趋势

如果你的数据是时间序列,折线图是最直观的选择:

ggplot(df_long, aes(x = time, y = value, color = metric)) +
  geom_line(linewidth = 1.2) + # 调整线条粗细
  geom_point(size = 2, fill = "white", shape = 21) + # 给点加白边,更醒目
  scale_color_manual(values = c("#2A9D8F", "#F4A261")) +
  labs(title = "温度与湿度的时间趋势", x = "时间(天)", y = "数值", color = "指标") +
  theme_light() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 14, face = "bold"),
    legend.position = "top" # 把图例移到顶部,更整洁
  )

3. 直方图/密度图:展示单个连续变量的分布

想知道温度的分布情况?用直方图或密度图:

# 直方图(带密度线)
ggplot(df, aes(x = temperature)) +
  geom_histogram(binwidth = 1, fill = "#2A9D8F", color = "white", alpha = 0.7) +
  geom_density(aes(y = ..density.. * nrow(df) * 1), color = "#E76F51", linewidth = 1) + # 叠加密度线
  labs(title = "温度分布情况", x = "温度(℃)", y = "频数") +
  theme_minimal() +
  theme(plot.title = element_text(hjust = 0.5, size = 14, face = "bold"))

# 密度图(更平滑的分布展示)
ggplot(df, aes(x = temperature)) +
  geom_density(fill = "#2A9D8F", alpha = 0.5) +
  labs(title = "温度的密度分布", x = "温度(℃)", y = "密度") +
  theme_minimal()

4. 箱线图/小提琴图:对比分组的连续变量分布

如果你的数据有分组变量(比如把时间分成“上旬”“中旬”“下旬”),可以用这些图对比分布:

# 先给数据加分组变量
df$period <- cut(df$time, breaks = c(0,10,20,30), labels = c("上旬", "中旬", "下旬"))

# 箱线图
ggplot(df, aes(x = period, y = temperature, fill = period)) +
  geom_boxplot(alpha = 0.7) +
  scale_fill_manual(values = c("#2A9D8F", "#F4A261", "#E76F51")) +
  labs(title = "不同时间段的温度分布", x = "时间段", y = "温度(℃)") +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 14, face = "bold"),
    legend.position = "none" # 去掉重复的图例
  )

# 小提琴图(展示更细节的分布形态)
ggplot(df, aes(x = period, y = temperature, fill = period)) +
  geom_violin(alpha = 0.7) +
  geom_jitter(size = 1.5, alpha = 0.5) + # 叠加原始数据点,看到具体数值
  scale_fill_manual(values = c("#2A9D8F", "#F4A261", "#E76F51")) +
  labs(title = "不同时间段的温度分布(小提琴图)", x = "时间段", y = "温度(℃)") +
  theme_minimal() +
  theme(plot.title = element_text(hjust = 0.5, size = 14, face = "bold"),
        legend.position = "none")

5. 热力图:展示两个连续变量的交互关系

如果有两个连续自变量和一个连续因变量,热力图能直观展示数值变化:

# 模拟热力图数据
heat_df <- expand.grid(x = seq(1, 10), y = seq(1, 10))
heat_df$value <- heat_df$x * heat_df$y + rnorm(100, 0, 5)

ggplot(heat_df, aes(x = x, y = y, fill = value)) +
  geom_tile() +
  scale_fill_gradient(low = "#F8F9FA", high = "#2A9D8F") + # 渐变配色
  labs(title = "X与Y对数值的影响热力图", x = "X变量", y = "Y变量", fill = "数值大小") +
  theme_minimal() +
  theme(plot.title = element_text(hjust = 0.5, size = 14, face = "bold"))

通用美化小技巧

最后给你几个新手也能快速用上的美化要点:

  • 选主题:theme_minimal()、theme_bw()、theme_light()都是自带的简洁美观主题,能省很多调整时间。
  • 配颜色:用scale_color_manual()/scale_fill_manual()选和谐的配色(比如低饱和的莫兰迪色),渐变颜色用scale_color_gradient()。
  • 调文本:标题居中、加粗,坐标轴标签清晰,必要时调整字体大小。
  • 去冗余:用theme(panel.grid = element_blank())去掉多余网格线,legend.position = "none"去掉重复图例。

刚接触的话,先从这些基础图形练起,慢慢调整细节,ggplot2的灵活性很强,多试几次就能做出符合你需求的美观图形啦~

内容的提问来源于stack exchange,提问作者Brian Miller

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:19:53