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

如何用ggplot的geom_tile实现带填充渐变的分块三角热力图

在ggplot中实现对角线分割的三角热图

当然可以实现,核心思路是将两个数据集的坐标列统一,然后在同一个ggplot图层中叠加两个geom_tile,分别对应上三角(Individual)和下三角(Population)区域。以下是修改后的完整代码:

步骤1:加载并预处理数据

library(ggplot2)
library(dplyr)

# 加载测试数据
fulldf <- read.csv("fulldf.csv")

# 统一两个数据集的坐标列名,方便在同一图层绘制
fulldf_ind <- fulldf %>% 
  filter(Triangle == "Individual") %>% 
  rename(x = Ind_1, y = Ind_2)

fulldf_pop <- fulldf %>% 
  filter(Triangle == "Population") %>% 
  rename(x = Population_1, y = Population_2)

步骤2:保留原颜色配置

# 定义颜色渐变和断点(沿用你原代码的设置)
color_palette <- c("#001260", "#EAEDE9", "#601200")
nHalf <- 4
Min <- -.1
Max <- .1
Thresh <- 0

rc1 <- colorRampPalette(colors = color_palette[1:2], space = "Lab")(nHalf)
rc2 <- colorRampPalette(colors = color_palette[2:3], space = "Lab")(nHalf)
rampcols <- c(rc1, rc2)
rampcols[c(nHalf, nHalf+1)] <- rgb(t(col2rgb(color_palette[2])), maxColorValue = 256) 

rb1 <- seq(Min, Thresh, length.out = nHalf + 1)
rb2 <- seq(Thresh, Max, length.out = nHalf + 1)[-1]
rampbreaks <- c(rb1, rb2)

步骤3:绘制合并的三角热图

combined_plot <- ggplot() +
  # 绘制下三角:Population数据
  geom_tile(data = fulldf_pop, aes(x = x, y = y, fill = as.numeric(Value)), colour = "#000000") +
  # 绘制上三角:Individual数据
  geom_tile(data = fulldf_ind, aes(x = x, y = y, fill = as.numeric(Value)), colour = "#000000") +
  # 离散坐标轴设置,消除间隙
  scale_x_discrete(expand = c(0, 0)) +
  scale_y_discrete(expand = c(0, 0)) +
  # 颜色渐变刻度(和原代码一致)
  scale_fill_gradientn(colors = rampcols, breaks = rampbreaks, limits = c(-.1, .1)) +
  # 分面设置,保留原有的自由缩放和空间
  facet_grid(K ~ CHRType, scales = "free", space = "free") +
  # 主题设置(合并原两个图的主题,仅需设置一次)
  theme(panel.background = element_rect(fill = "#ffffff"),
        panel.border = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.spacing = unit(1, "lines"),
        legend.position = "right",
        legend.key = element_blank(),
        legend.background = element_blank(),
        legend.margin = margin(t = 0, b = 0, r = 15, l = 15),
        legend.box = "vertical",
        legend.box.margin = margin(t = 20, b = 30, r = 0, l = 0),
        axis.title = element_blank(),
        axis.text.x = element_text(color = "#000000", size = 16, face = "bold", angle = 45, vjust = 1, hjust = 1),
        axis.text.y = element_text(color = "#000000", size = 16, face = "bold"),
        axis.ticks = element_line(color = "#000000", linewidth = .3),
        strip.text = element_text(colour = "#000000", size = 24, face = "bold", family = "Optima"),
        strip.background = element_rect(colour = "#000000", fill = "#d6d6d6", linewidth = .3),
        axis.line = element_line(colour = "#000000", linewidth = .3)) +
  # 图例设置(保留原样式)
  guides(fill = guide_legend(title = "", title.theme = element_text(size = 16, face = "bold"),
                             label.theme = element_text(size = 15), reverse = TRUE))

# 输出图形
print(combined_plot)

关键说明:

  • 统一坐标列名是实现合并的核心,确保两个数据集能在同一坐标系下对应正确的位置。
  • 原数据已经按Triangle字段区分了上/下三角区域,因此直接叠加两个geom_tile即可自动形成对角线分割的效果。
  • 合并后仅需设置一次主题和图例,避免重复配置,同时图例会自动合并为一个。

内容的提问来源于stack exchange,提问作者George Pacheco

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 00:06:02