【问题标题】:Is there any alternative to ggtern in R?R中的ggtern有什么替代品吗?
【发布时间】:2021-07-12 08:45:30
【问题描述】:

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

【问题讨论】:

    标签: r ggplot2 tidyverse ggtern


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      这对我有用!然后卸载ggternggplot2

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

      【讨论】:

        猜你喜欢
        • 2020-06-20
        • 1970-01-01
        • 1970-01-01
        • 2023-03-07
        • 1970-01-01
        • 2022-11-10
        • 2011-11-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多