微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

R 中的三元图 - ggter 3.3.0 与旧版本

如何解决R 中的三元图 - ggter 3.3.0 与旧版本

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

Ternary plot and filled contour

代码如下:

#Orignal Data as per Question

library(ggplot2)
library(ggtern)

a <- c(0.1,0.5,0.6,0.2,0.004166667,0.45) 
b <- c(0.75,0.1,0.951612903,0.918103448,0.7875,0.45)
c <- c(0.15,0.3,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..),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|")

问题是随着这个包的更新,出现了一些错误

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 代替。包的修改是非常大的,在这一点上,即使是更换它,任何新的错误都会发生。包的语法发生了很大的变化。有人知道如何翻译这段代码并使其适用于新版本吗?

我感兴趣的输出是这样的:

我感兴趣的输出是这样的:

enter image description here

有人可以帮我吗?

预先感谢您对最终的支持

解决方法

我想我解决了这个问题。新旧版本更改了部分命令。

我使用了 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.6,0.2,0.004166667,0.45) 
b <- c(0.75,0.1,0.951612903,0.918103448,0.7875,0.45)
c <- c(0.15,0.3,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..),#                 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|")

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?