[英]Ternary plot and filled contour - ggtern
我正在尝试使用ggtern库创建一个三元轮廓填充图。 使用的代码是
a <- c(0.50, 0.625, 0.375, 0.25, 0.5625, 0.125, 0.25, 0.3125, 0.375, 0.4375, 0.1875, 0.3125, 0.375, 0.4375)
b <- c(0.25, 0.1875, 0.3125, 0.375, 0.25, 0.4375, 0.25, 0.375, 0.375, 0.4375, 0.875, 0.3125, 0.25, 0.125)
c <- c(0.25, 0.1875, 0.3125, 0.375, 0.1875, 0.4375, 0.50, 0.3125, 0.25, 0.125, 0.625, 0.375, 0.375, 0.4375)
d <- c(77.82325, 74.59318767, 76.76495015, 76.62122282, 77.95608657, 76.91320817, 68.50986659,8.53724416,80.32237597, 85.43315399, 61.80292426, 74.71471485, 73.27176908, 67.51782848)
df <- data.frame(a, b, c, d)
df$id <- 1:nrow(df)
#Build Plot
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_density_tern(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|")
但是我收到以下警告消息:
Error: coord_tern requires the following missing aesthetics: z
为什么会发生此错误,以及如何解决? 请帮忙
@jhoward,感谢您对问题的解释。
我还想在ggtern的最新版本中演示一些其他功能。 请参见下面的插值几何,以进行曲面建模:
ggtern(data = df,aes(x = c,y = a, z = b)) +
geom_interpolate_tern(aes(value=d,colour=..level..),bins=50) +
geom_point(aes(color=d),size=10) +
geom_text(aes(label=round(d,0)),size=3) +
theme_bw() +
theme(legend.position=c(0,1),
legend.justification=c(0,1)) +
scale_colour_gradient(low='green',high='red') +
labs( title = "Ternary filled contour plot", colour = "Value, V")
产生以下输出:
当发生这种情况时,显而易见的方法是对代码进行解构,以查看错误的确切位置。 如果这样做,您将知道它正在调用stat_density2d(...)
。 我ggtern
无法使ggtern
版本的stat_density2d(...)
正常工作(尽管我很想看看别人做的例子)。
如果删除该调用,并将fill=..level..
美化到geom_density_tern(...)
,则会得到以下信息:
library(ggtern)
ggtern(data = df,aes(x = c,y = a, z = b)) +
geom_density_tern(aes(fill=..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|")
注意轮廓填充是如何全黄的。 发生这种情况的原因是,您试图将fill用于两种不同的事物:z级别(来自b
列)和点,其中fill来自d
列。 ggplot
(扩展名为gggtern
)创建单个填充比例。 由于df$b
大约在(0.1,1)范围内,而df$d
大约在(60,90)范围内,因此轮廓填充水平全部在该范围的低端,因此为黄色。
如果您确实希望轮廓填充和点使用不同的调色板,则可以对点使用颜色美感:
library(ggtern)
ggtern(data = df,aes(x = c,y = a, z = b)) +
geom_density_tern(aes(fill=..level..),n = 400) +
geom_point(aes(color = d),size = 8,shape = 20) +
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 = 2), color = guide_colorbar(order = 1)) +
labs( title = "Ternary filled contour plot", color = "Value, V", fill = "|V - 0|")
编辑:根据OP的评论添加。
ggtern(data = df,aes(x = c,y = a, z = b)) +
geom_density_tern(aes(fill=..level..),n = 400) +
geom_point(aes(color = d),size = 8,shape = 20) +
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", limits=c(60,90),midpoint = 75) +
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 = 2), color = guide_colorbar(order = 1)) +
labs( title = "Ternary filled contour plot", color = "Value, V", fill = "|V - 0|")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.