0

看起来 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 之外,有没有人可以找到其他三元图选项?

4

2 回答 2

2

手动,您可以使用函数绘制点:(我使用了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,0)) +
  coord_equal()

在此处输入图像描述

于 2021-07-12T09:20:14.513 回答
1

这对我有用!卸载ggtern然后ggplot2_

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

install.packages("ggtern")

library(ggtern)
于 2021-07-20T08:44:27.397 回答