无法使用ggtern生成等高线数据

无法使用ggtern生成等高线数据,r,plot,R,Plot,我正试图用图库ggtern创建一个三元等高线填充图。使用的代码是: library(ggtern) a <- c(0.000, 0.250, 0.300, 0.300, 0.350, 0.350, 0.360, 0.400, 0.400, 0.420, 0.420, 0.425, 0.450, 0.450, 0.475, 0.480, 0.480, 0.490, 0.500, 0.510, 0.540, 0.540, 0.560, 0.560, 0.570, 0.595, 0.600,

我正试图用图库ggtern创建一个三元等高线填充图。使用的代码是:

library(ggtern)


a <- c(0.000, 0.250, 0.300, 0.300, 0.350, 0.350, 0.360, 0.400, 0.400, 0.420, 0.420, 0.425, 0.450, 0.450, 0.475, 0.480, 0.480, 0.490, 0.500, 0.510, 0.540, 0.540, 0.560, 0.560, 0.570, 0.595, 0.600, 0.630, 0.630, 0.640, 0.665, 0.680, 0.700, 0.720, 0.720, 0.760, 0.765, 0.800, 0.810, 0.850, 0.855, 0.900, 0.950)
b <- c(1.000, 0.250, 0.200, 0.300, 0.150, 0.350, 0.240, 0.100, 0.400, 0.180, 0.280, 0.075, 0.050, 0.450, 0.025, 0.120, 0.320, 0.210, 0.500, 0.090, 0.060, 0.360, 0.140, 0.240, 0.030, 0.105, 0.400, 0.070, 0.270, 0.160, 0.035, 0.120, 0.300, 0.080, 0.180, 0.040, 0.135, 0.200, 0.090, 0.150, 0.045, 0.100, 0.050)
c <- c(0.000, 0.500, 0.500, 0.400, 0.500, 0.300, 0.400, 0.500, 0.200, 0.400, 0.300, 0.500, 0.500, 0.100, 0.500, 0.400, 0.200, 0.300, 0.000, 0.400, 0.400, 0.100, 0.300, 0.200, 0.400, 0.300, 0.000, 0.300, 0.100, 0.200, 0.300, 0.200, 0.000, 0.200, 0.100, 0.200, 0.100, 0.000, 0.100, 0.000, 0.100, 0.000, 0.000) 
d <- c(03.50, 11.40, 13.66, 08.77, 18.23, 07.17, 10.14, 25.80, 06.16, 12.88, 08.45, 32.84, 40.59, 05.49, 60.56, 17.57, 07.20, 12.75, 04.99, 21.81, 26.31, 06.26, 12.74, 08.39, 37.95, 15.79, 05.62, 19.07, 07.30, 10.39, 26.49, 12.24, 06.53, 14.53, 08.76, 18.53, 10.18, 07.63, 12.09, 08.77, 14.78, 10.10, 12.46)
df <- data.frame(a, b, c, d)
df$id <- 1:nrow(df)


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) +
labs(x = "X (%)",y = "Y (%)",z = "Z (%)",title = "Title", size = 3) +
scale_fill_gradient2(low = "green", mid = "yellow", high = "red", midpoint = 10) +
scale_color_gradient2(low = "green", mid = "yellow", high = "red", midpoint = 10) +
theme_custom(base_size = 12, base_family = "", col.T = "black", col.L = "black", col.R = "black", col.BG = "white") +
tern_anticlockwise() +
tern_limits(breaks = seq(0.1,1,by = 0.1)) + #AFFECT ALL SCALES
theme(axis.tern.arrowstart = 0.4,axis.tern.arrowfinish = 0.6) +
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 filled contour plot", fill = "Value, V",alpha = "|V - 0|")


ggsave("ternary_filled_contour.pdf")

为什么会发生此错误以及如何修复它?

Nxkryptor,您已经在构造函数中包含了两次aes(…)函数,这可能不是问题的根源,但我认为应该提及

问题的根源是相对于数据的“binwidth”值无效,请尝试以下操作:

ggtern(data = df,aes(x = c,y = a,z = b)) +
  stat_density2d(geom = "polygon", n = 400, aes(fill = ..level.., weight = d, alpha = abs(..level..))) +
  geom_density2d(aes(weight = d,color = ..level..), n = 400) +
  geom_point(aes(fill = d),color = "black",size = 5,shape = 21) +
  geom_text(aes(label = id),size = 3) +
  labs(x = "X (%)",y = "Y (%)",z = "Z (%)",title = "Title", size = 3) +
  scale_fill_gradient2(low = "green", mid = "yellow", high = "red", midpoint = 10) +
  scale_color_gradient2(low = "green", mid = "yellow", high = "red", midpoint = 10) +
  theme_custom(base_size = 12, base_family = "", col.T = "black", col.L = "black", col.R = "black", col.BG = "white") +
  tern_anticlockwise() +
  tern_limits(breaks = seq(0.1,1,by = 0.1)) + #AFFECT ALL SCALES
  theme(axis.tern.arrowstart = 0.4,axis.tern.arrowfinish = 0.6) +
  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 filled contour plot", fill = "Value, V",alpha = "|V - 0|")
这将产生以下结果

此功能(插值)是今天提交给CRAN的ggtern版本1.0.6.0的一部分

这里是一个等价的解决方案,但使用多元线性回归,而不是核密度估计

ggtern(data = df,aes(x = c,y = a,z = b)) +
  geom_interpolate_tern(aes(value=d,fill=..level..,alpha=..level..),binwidth=5,n=400) + 
  geom_point(aes(fill = d),color = "black",size = 5,shape = 21) +
  geom_text(aes(label = id),size = 3) +
  scale_fill_gradient2(low = "green", mid = "yellow", high = "red", midpoint = 10) +
  theme_bw() + 
  theme_anticlockwise() +
  theme(legend.justification = c(0,1), legend.position = c(0,1)) +
  guides(alpha='none') +
  labs( title = "Ternary Filled Contour Plot", 
        fill  = "Value, V",
        x     = "X",y = "Y",z = "Z") + 
  custom_percent("(%)")
这将产生以下结果:

ggtern(data = df,aes(x = c,y = a,z = b)) +
  stat_density2d(geom = "polygon", n = 400, aes(fill = ..level.., weight = d, alpha = abs(..level..))) +
  geom_density2d(aes(weight = d,color = ..level..), n = 400) +
  geom_point(aes(fill = d),color = "black",size = 5,shape = 21) +
  geom_text(aes(label = id),size = 3) +
  labs(x = "X (%)",y = "Y (%)",z = "Z (%)",title = "Title", size = 3) +
  scale_fill_gradient2(low = "green", mid = "yellow", high = "red", midpoint = 10) +
  scale_color_gradient2(low = "green", mid = "yellow", high = "red", midpoint = 10) +
  theme_custom(base_size = 12, base_family = "", col.T = "black", col.L = "black", col.R = "black", col.BG = "white") +
  tern_anticlockwise() +
  tern_limits(breaks = seq(0.1,1,by = 0.1)) + #AFFECT ALL SCALES
  theme(axis.tern.arrowstart = 0.4,axis.tern.arrowfinish = 0.6) +
  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 filled contour plot", fill = "Value, V",alpha = "|V - 0|")

以下是有关此几何体的一些附加信息: