在 R 中有没有 ggtern 的替代品?

问题描述

看起来ggtern没有与新版本的ggplot2同步。 因此我们不能使用 ggtern。

library(ggtern)
set.seed(1)
plot <- ggtern(data = data.frame(x = runif(100),y = runif(100),z = runif(100)),aes(x,y,z))
plot + stat_density_tern(geom = 'polygon',n         = 200,aes(fill  = ..level..,alpha = ..level..)) +
  geom_point() +
  theme_rgbw() +
  labs(title = "Example Density/Contour Plot")    +
  scale_fill_gradient(low = "blue",high = "red")  +
  guides(color = "none",fill = "none",alpha = "none")
Error: geom_point requires the following missing aesthetics: x and y

除了 R 中的 ggtern 之外,还有没有人可以找到其他三元图的选项?

解决方法

手动,您可以使用函数绘制点:(我使用了 https://en.wikipedia.org/wiki/Ternary_plot 处的公式)

我不熟悉 stat_density_tern 的输出,所以我不确定该部分的预期结果。

library(tidyverse)
tern <- function(df) {
  df %>% mutate(x_pos = 0.5 * (2*y + z) / (x+y+z),y_pos = sqrt(3) / 2 * z / (x+y+z)) 
}

tern(plot) %>%
  ggplot(aes(x_pos,y_pos)) +
  geom_point() +
  annotate("path",x = c(0,0.5,1,0),y = c(0,sqrt(3)/2,0)) +
  coord_equal()

enter image description here

,

这对我有用!卸载 ggternggplot2 然后

install_version("ggplot2",version = "3.3.0",repos = "http://cran.us.r-project.org")

install.packages("ggtern")

library(ggtern)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...