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

如何为ggplot2热力图的指定黄色数据区域外侧添加贴合轮廓线

你之前用stat_ellipse不符合需求是因为该函数是基于数据点的分布拟合正态椭圆,和xCS的取值边界无关,自然无法贴合你要的黄色区域轮廓。
你需要的黄色区域外边界本质是xCS=50的等值线,直接在原有代码中添加一层stat_contour即可实现,修改后的完整代码如下:

ggplot() + 
  stat_summary_2d(aes(x = plate_x, y = plate_z, z = xCS)) + 
  # 新增xCS=50等值线层,即黄色区域外轮廓
  stat_contour(aes(x = plate_x, y = plate_z, z = xCS), breaks = 50, color = "black", size = 1) +
  scale_fill_distiller(palette = "Spectral", 
                       limits = c(10,90), oob = scales::squish, name = "xCS%") +
  xlim(-1.5,1.5) + ylim(0,4.5) +
  geom_rect(aes(xmin = -0.83, xmax = 0.83, ymin = 1.5, ymax = 3.5), 
            alpha = 0, size = 1, color = "black") +
  geom_segment(aes(x = -0.708, y = 0.15, xend = 0.708, yend = 0.15), 
               size = 1, color = "black") + 
  geom_segment(aes(x = -0.708, y = 0.3, xend = -0.708, yend = 0.15), 
               size = 1, color = "black") + 
  geom_segment(aes(x = -0.708, y = 0.3, xend = 0, yend = 0.5), size = 1, 
               color = "black") + 
  geom_segment(aes(x = 0, y = 0.5, xend = 0.708, yend = 0.3), size = 1, 
               color = "black") + 
  geom_segment(aes(x = 0.708, y = 0.3, xend = 0.708, yend = 0.15), size = 1, 
               color = "black") + 
  theme_bw() + labs(x = "Plate Side (in)",
                    y = "Plate Height (in)",
                    title = "Probability of a Called Strike") + 
  facet_grid(~stand)

如果你的原始数据点比较稀疏,绘制出的等值线有锯齿不够平滑,可以先对xCS做空间插值后再绘制等值线,操作代码如下:

# 加载所需依赖包
library(akima)
library(dplyr)
library(purrr)

# 按facet分组做插值,避免不同分组数据互相干扰
interp_data <- 原始数据框名 %>% 
  group_by(stand) %>% 
  summarise(interp_res = list(interp(x = plate_x, y = plate_z, z = xCS, nx = 100, ny = 100))) %>% 
  mutate(interp_df = map(interp_res, ~as.data.frame(interp2xyz(.x)))) %>% 
  unnest(interp_df)

# 用插值后的数据绘图
ggplot() + 
  stat_summary_2d(aes(x = plate_x, y = plate_z, z = xCS), data = 原始数据框名) + 
  # 用插值后的数据绘制更平滑的轮廓线
  geom_contour(aes(x = x, y = y, z = z), data = interp_data, breaks = 50, color = "black", size = 1) +
  # 剩余参数、图层和原有代码完全一致即可
  scale_fill_distiller(palette = "Spectral", 
                       limits = c(10,90), oob = scales::squish, name = "xCS%") +
  xlim(-1.5,1.5) + ylim(0,4.5) +
  geom_rect(aes(xmin = -0.83, xmax = 0.83, ymin = 1.5, ymax = 3.5), 
            alpha = 0, size = 1, color = "black") +
  geom_segment(aes(x = -0.708, y = 0.15, xend = 0.708, yend = 0.15), 
               size = 1, color = "black") + 
  geom_segment(aes(x = -0.708, y = 0.3, xend = -0.708, yend = 0.15), 
               size = 1, color = "black") + 
  geom_segment(aes(x = -0.708, y = 0.3, xend = 0, yend = 0.5), size = 1, 
               color = "black") + 
  geom_segment(aes(x = 0, y = 0.5, xend = 0.708, yend = 0.3), size = 1, 
               color = "black") + 
  geom_segment(aes(x = 0.708, y = 0.3, xend = 0.708, yend = 0.15), size = 1, 
               color = "black") + 
  theme_bw() + labs(x = "Plate Side (in)",
                    y = "Plate Height (in)",
                    title = "Probability of a Called Strike") + 
  facet_grid(~stand)

插值代码中的nx、ny参数控制插值网格密度,数值越大最终生成的轮廓线越平滑,可根据实际显示效果调整。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 18:33:00