簡體   English   中英

R 中的三元圖 - ggter 3.3.0 與舊版本

[英]Ternary Diagrams in R - ggter 3.3.0 vs older versions

我在鏈接中看到了一個非常有趣的三元圖模板,由 Nicholas Hamilton 制作:

三元 plot 和填充輪廓

代碼如下:

#Orignal Data as per Question

library(ggplot2)
library(ggtern)

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|")

問題是隨着此 package 的更新,會出現一些錯誤:

Warning messages:
1: Ignoring unknown aesthetics: weight 
2: Ignoring unknown aesthetics: weight 
3: Removing Layer 1 ('StatDensity2d'), as it is not an approved stat (for ternary plots) under the present ggtern package. 
4: Removing Layer 2 ('GeomDensity2d'), as it is not an approved geometry (for ternary plots) under the present ggtern package.

問題是geom_density2dstat_density2d不再支持並被geom_density_ternstat_density_tern取代。 package 的修改非常大,即使更換它,任何新的錯誤都會發生。 package 的語法發生了很大變化。 有人知道如何翻譯此代碼並使其適合新版本嗎?

我感興趣的 output 是這樣的:

我感興趣的 output 是這樣的:

在此處輸入圖像描述

有人可以幫助我嗎?

提前感謝您的每一次最終支持!

我想我解決了這個問題。 從舊版本和新版本開始,一些命令發生了變化。

我使用geom_density_ternstat_density_tern

我刪除binwidth = 100 ,我仍然不知道它代表什么;

我設置了一個較低的 bdl: bdl=0.02

代碼下方:

library(ggtern)
library(ggplot2)

#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,
bdl = 0.02) + 
  geom_density_tern(aes(weight=d,color=..level..),
                 n=400,
#                 binwidth=100,
bdl = 0.02) +
  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_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|")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM