如何使用 ggtern 2.1.0 获取三元等高线图?

How to get ternary contour plots with ggtern 2.1.0?

我正在试验这个 code 并且我试图让网格线位于图上方。

使用的代码与OP中的相同:

#Orignal Data as per Question
a <- c(0.1, 0.5,0.5, 0.6, 0.2, 0          , 0         , 0.004166667, 0.45) 
b <- c(0.75,0.5,0  , 0.1, 0.2, 0.951612903,0.918103448, 0.7875     , 0.45)
c <- c(0.15,0  ,0.5, 0.3, 0.6, 0.048387097,0.081896552, 0.208333333, 0.10) 
d <- c(500,2324.90,2551.44,1244.50, 551.22,-644.20,-377.17,-100, 2493.04) 
df <- data.frame(a, b, c, d)

#For labelling each point.
df$id <- 1:nrow(df)

#Build Plot
ggtern(data=df,aes(x=c,y=a,z=b),aes(x,y,z)) + 
  stat_density2d(geom="polygon",
                 n=400,
                 aes(fill=..level..,
                 weight=d,
                 alpha=abs(..level..)),
                 binwidth=100) + 
  geom_density2d(aes(weight=d,color=..level..),
                 n=400,
                 binwidth=100) +
  geom_point(aes(fill=d),color="black",size=5,shape=21) + 
  geom_text(aes(label=id),size=3) + 
  scale_fill_gradient(low="yellow",high="red") + 
  scale_color_gradient(low="yellow",high="red") + 
  theme_tern_rgbw() + 
  theme(legend.justification=c(0,1), legend.position=c(0,1)) + 
  guides(fill = guide_colorbar(order=1),
         alpha= guide_legend(order=2),
         color="none") + 
  labs(  title= "Ternary Plot and Filled Contour",
         fill = "Value, V",alpha="|V - 0|")

#Save Plot
ggsave("TernFilled.png")

是否可以让网格线高于等高线?

更新

为了在顶部显示网格线,我更新到了 ggtern 2.1.0。更新后,我不得不更改 stat_density2d -> stat-density_tern 和 geom_density2d -> geom_density_tern 几个更改以使代码正常工作。虽然它编译了下图,但我无法重现上面显示的以前的图。我必须添加 na.rm = TRUE 才能使代码正常工作

library(ggtern)
#Orignal Data as per Question
a <- c(0.1, 0.5,0.5, 0.6, 0.2, 0          , 0         , 0.004166667, 0.45) 
b <- c(0.75,0.5,0  , 0.1, 0.2, 0.951612903,0.918103448, 0.7875     , 0.45)
c <- c(0.15,0  ,0.5, 0.3, 0.6, 0.048387097,0.081896552, 0.208333333, 0.10) 
d <- c(500,2324.90,2551.44,1244.50, 551.22,-644.20,-377.17,-100, 2493.04) 
df <- data.frame(a, b, c, d)

#For labelling each point.
df$id <- 1:nrow(df)

#Build Plot
ggtern(data=df,aes(x=c,y=a,z=b),aes(x,y,z)) + 
  stat_density_tern(geom="polygon",
                 n=400,
                 aes(fill=..level..,
                     weight=d,
                     alpha=abs(..level..)),
                 na.rm = TRUE) + 
  geom_density_tern(aes(weight=d,color=..level..),
                 n=400,
                  na.rm = TRUE) +
  geom_point(aes(fill=d),color="black",size=5,shape=21) + 
  geom_text(aes(label=id),size=3) + 
  scale_fill_gradient(low="yellow",high="red") + 
  scale_color_gradient(low="yellow",high="red") + 
  # theme_tern_rgbw() + 
  theme(legend.justification=c(0,1), legend.position=c(0,1)) + 
  guides(fill = guide_colorbar(order=1),
         alpha= guide_legend(order=2),
         color="none") + 
  labs(  title= "Ternary Plot and Filled Contour",
         fill = "Value, V",alpha="|V - 0|")

#Save Plot
ggsave("TernFilled.pdf")

为什么我不能用以前的 ggtern 版本和更新版本获得绘图? binwidth 被什么替换了?

自该代码上线以来,发生了一些变化。

首先,关于你的问题,最值得注意的是,核密度现在默认是根据反对数比 space 计算的,这可以通过 base='identity'争论。

其次,density_tern 几何遵循与 ggplot2 相同的路径,在使用 if 'h' 参数时,因此,binwidth 现在没有意义。

这是一个示例,它呈现的结果更接近您的初始预期:

#Build Plot
ggtern(data=df,aes(x=c,y=a,z=b),aes(x,y,z)) + 
  stat_density_tern(geom="polygon",color='black',
                    n=400,h=0.75,expand = 1.1,
                    base='identity',
                    aes(fill   = ..level..,weight = d),
                    na.rm = TRUE) + 
  geom_point(color="black",size=5,shape=21) +
  geom_text(aes(label=id),size=3) + 
  scale_fill_gradient(low="yellow",high="red") + 
  scale_color_gradient(low="yellow",high="red") + 
  theme_rgbw() + 
  theme(legend.justification=c(0,1), legend.position=c(0,1)) +
  theme_gridsontop() + 
  guides(fill = guide_colorbar(order=1),color="none") + 
  labs(  title= "Ternary Plot and Filled Contour",fill = "Value, V")