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

如何用R语言Ternary包在三角图中绘制笛卡尔坐标点?

问题描述

需要在三角图中绘制笛卡尔坐标点以匹配过往项目分析,已拥有含x、y值的数据框,可通过Ternary包生成空白三角图。参考文档得知该包支持笛卡尔坐标,但调用TernaryPoints(df[, c('x', 'y')])时出现报错:

Error in CoordinatesToXY(coordinates) : Coordinates must be ternary points

不清楚如何绘制数据框中的点。

空白三角图代码:

par(mfrow = c(1, 2), mar = c(0.3, 0.3, 1.3, 0.3))
TernaryPlot(grid.lines = 0, ticks.col = "white")

示例数据框:

x = c(-0.3463, 0.0002, -0.4330, -0.4329, -0.4330, -0.5773, -0.2885)
y = c(-0.20, 0, 0.25, -0.50, 0.25, -0.50, -0.50)
df = data.frame(x,y)
解决方案

TernaryPoints()函数要求输入的是**三元坐标(a, b, c)**而非笛卡尔坐标,所以直接传入x/y会报错。既然你已经用TernaryPlot()生成了基于基础绘图系统的画布,直接用R基础绘图的points()函数就能绘制笛卡尔坐标点:

# 先绘制空白三角图
par(mfrow = c(1, 2), mar = c(0.3, 0.3, 1.3, 0.3))
TernaryPlot(grid.lines = 0, ticks.col = "white")

# 直接用points()绘制笛卡尔坐标点
points(df$x, df$y, pch = 16, col = "red") # pch和col可按需调整样式

原理是TernaryPlot()会在当前绘图设备上建立符合三角图布局的笛卡尔坐标系,所以基础绘图函数可以直接适配。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 21:05:56