问题描述
看起来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()
,
这对我有用!卸载 ggtern
和 ggplot2
然后
install_version("ggplot2",version = "3.3.0",repos = "http://cran.us.r-project.org")
install.packages("ggtern")
library(ggtern)