首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在R中有没有ggtern的替代方案?

在R中有没有ggtern的替代方案?
EN

Stack Overflow用户
提问于 2021-07-12 16:45:30
回答 2查看 84关注 0票数 0

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

代码语言:javascript
复制
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")
代码语言:javascript
复制
Error: geom_point requires the following missing aesthetics: x and y

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-07-12 17:20:15

您可以使用函数手动绘制这些点:(我在https://en.wikipedia.org/wiki/Ternary_plot中使用了公式)

我不熟悉stat_density_tern的输出,所以我不确定从那部分能得到什么。

代码语言:javascript
复制
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()

票数 2
EN

Stack Overflow用户

发布于 2021-07-20 16:44:27

这对我很有效!卸载ggternggplot2,然后

代码语言:javascript
复制
install_version("ggplot2", version = "3.3.0", repos = "http://cran.us.r-project.org")

install.packages("ggtern")

library(ggtern)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68344404

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档