2015-09-04 98 views
2

我想创建一个三元轮廓填充图库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 

为什么会出现这种错误发生,以及如何解决它?请帮助

+0

欢迎SO。 +1提供数据集来演示问题。今后,请提供一个*最小*可重现的例子。见[这篇文章](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610)如何做到这一点。实际上,这意味着删除与该问题无关的所有代码。 – jlhoward

回答

2

当这样的事情发生时,显而易见的方法是解构代码,以查看错误发生的位置。如果你已经这样做了,你会知道它是在拨打stat_density2d(...)。我无法使版本的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|") 

通知轮廓填充怎么都是黄色的。发生这种情况的原因是,您尝试使用两种不同的填充方式:z级(来自列b)和填充来自列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|") 

编辑:添加为每个操作的评论。

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

+0

Hi @jlhoward!,谢谢你的回复。我需要填充z级和d列中的点,我需要将值范围改为60-90。你能帮我做这个吗?你的帮助将非常感激。 –

+0

这与第二张图片有何不同?(顺便说一句:它看起来像我刚刚发布了两次相同的代码 - 我的错误,它已被编辑,所以现在第二个代码段确实会生成第二个图像)。要更改颜色值范围,请使用'limits = c(60,90 )'调用'scale_color_gradient2(...)',并将中点改为,例如75. – jlhoward

+0

嗨@jlhoward!我需要从第d列填充z级别,并将颜色范围也更改为60-90。我在调用scale_color_gradient2(...)时改变了限制= c(60,90)。但没有对我工作。您能否将这些更改通过代码发布给我。你的帮助将非常感激。 –

4

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

将会产生以下的输出:

example contour

+0

hi @jlhoward! 。非常感谢你的帮助 。我需要根据d变量(从d列)填充z级和梯度的轮廓线。我需要一个像以前一样的绘图,用d填充的轮廓线和渐变(不是插值几何图形)。但是这个插值几何图也非常出色。谢谢 –