Ternary Plot using a vector as density

Viewed 40

I have a dataset like this:

DENSE<-runif(10, min=0, max=100)
CROP<-runif(10, min=0, max=100)
WILD<-runif(10, min=0, max=100)
BetaDiv<-runif(10, min=0, max=1)
df<-data.frame(DENSE, CROP, WILD, BetaDiv)

I use this code to do a simple ternary plot,

ggtern(df[ordered(-df$BetaDiv),],aes(DENSE,CROP,WILD))+
  geom_point(alpha=0.8, aes(color=BetaDiv), size=3)+ scale_color_viridis_c(direction = -1)+
  stat_density_tern(geom = 'polygon',
                    n         = 100,
                    aes(fill  = ..level..,
                        alpha = ..level..))

Ternary

However, I need the density to be given by the variable BetaDiv. That is, that the density shows me where the BetaDiv values are concentrated.

Thanks to every one.

RS.

1 Answers

You need to specify it through aes(value=...) and I would think a hex would make more sense, where it basically cuts your plot into hex bins and calculates the sum of your beta values within each bin.

set.seed(111)
DENSE<-runif(20, min=0, max=100)
CROP<-runif(20, min=0, max=100)
WILD<-runif(20, min=0, max=100)
BetaDiv<-runif(20, min=0, max=1)
df<-data.frame(DENSE, CROP, WILD, BetaDiv)

g = ggtern(df,aes(DENSE,CROP,WILD))+
geom_point(alpha=0.8, aes(color=BetaDiv), size=3)+
scale_color_viridis_c(direction = -1) +
theme_bw()

g +  stat_hex_tern(aes(value=BetaDiv),binwidth=0.15,alpha=0.15,fun="mean")

enter image description here

Related