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

使用ggtern绘制单纯形图时的警告与错误问题求助

解决ggtern绘制单纯形时的警告与错误问题

问题描述

使用ggtern在单纯形上绘图时,R持续抛出警告:

In ggplot2::geom_segment(data = lines, ggplot2::aes(x = x1, y = x2, :
Ignoring unknown aesthetics: z and zend

若移除z和zend,则触发错误:

Error in FUN(X[[i]], ...) : object 'V3' not found.

可复现代码如下:

my_matrix <- matrix(c(0.000000, 0.000000, 1.000000,
                      0.666666, 0.000001, 0.333333,
                      1.000000, 0.000000, 0.000000,
                      0.000001, 0.333333, 0.666667,
                      0.333334, 0.333335, 0.333331,
                      0.333332, 0.666668, 0.000000,
                      0.000000, 0.666666, 0.333334,
                      0.666667, 0.333333, 0.000000,
                      0.000000, 1.000000, 0.000000,
                      0.333333, 0.000001, 0.666665),
                    nrow = 10, ncol = 3, byrow = TRUE)

design <- as.data.frame(my_matrix)
lines <- data.frame(x1 = c(0.5, 0, 0.5),
                    x2 = c(0.5, 0.5, 0),
                    x3 = c(0, 0.5, 0.5),
                    xend = c(1, 1, 1)/3,
                    yend = c(1, 1, 1)/3,
                    zend = c(1, 1, 1)/3)
ggtern::ggtern(design, ggplot2::aes(x = V1, y = V2, z = V3)) +
  ggplot2::geom_point(size = 3, color = "blue") +
  ggplot2::geom_segment(data = lines, ggplot2::aes(x = x1, y = x2,
                                                   xend = xend, yend = yend),
                        color = "grey", size = 0.2) +
  ggplot2::theme_bw() + ggtern::theme_nomask() + ggtern::theme_clockwise()

注:因用于玩具R包的绘图函数,故使用package::function()语法,未加载库。当前使用ggplot2 3.4.1和ggtern 3.4.1,需解决警告问题,明确问题根源及是否与ggtern过时有关。


问题根源

  1. 警告原因:使用了原生ggplot2::geom_segment图层,该图层不支持ggtern扩展的z/zend美学映射,但ggtern在处理单纯形绘图时会自动注入这些参数,原生图层无法识别,因此抛出警告。
  2. 错误原因:ggtern的绘图环境会继承全局设定的aes(x=V1,y=V2,z=V3)映射,当调用geom_segment时,会尝试在lines数据框中查找V3列,但该数据框中不存在此列,导致报错。

解决方法

直接使用ggtern专属的ggtern::geom_segment替代原生图层,同时在aes中明确指定z和zend参数,适配单纯形绘图逻辑:

修改后的代码:

my_matrix <- matrix(c(0.000000, 0.000000, 1.000000,
                      0.666666, 0.000001, 0.333333,
                      1.000000, 0.000000, 0.000000,
                      0.000001, 0.333333, 0.666667,
                      0.333334, 0.333335, 0.333331,
                      0.333332, 0.666668, 0.000000,
                      0.000000, 0.666666, 0.333334,
                      0.666667, 0.333333, 0.000000,
                      0.000000, 1.000000, 0.000000,
                      0.333333, 0.000001, 0.666665),
                    nrow = 10, ncol = 3, byrow = TRUE)

design <- as.data.frame(my_matrix)
lines <- data.frame(x1 = c(0.5, 0, 0.5),
                    x2 = c(0.5, 0.5, 0),
                    x3 = c(0, 0.5, 0.5),
                    xend = c(1, 1, 1)/3,
                    yend = c(1, 1, 1)/3,
                    zend = c(1, 1, 1)/3)
ggtern::ggtern(design, ggplot2::aes(x = V1, y = V2, z = V3)) +
  ggplot2::geom_point(size = 3, color = "blue") +
  # 使用ggtern专属的geom_segment并指定完整的三维映射
  ggtern::geom_segment(data = lines, ggplot2::aes(x = x1, y = x2, z = x3,
                                                 xend = xend, yend = yend, zend = zend),
                      color = "grey", size = 0.2) +
  ggplot2::theme_bw() + ggtern::theme_nomask() + ggtern::theme_clockwise()

补充说明

该问题与ggtern是否过时无关,核心是ggtern扩展了ggplot2的美学映射以支持单纯形绘图,但原生ggplot2图层不兼容这些扩展参数,同时全局美学映射的继承导致了数据列匹配错误。使用ggtern专属图层即可完美适配其绘图逻辑,消除警告与错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 09:07:49