Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ternary plot and filled contour

Users, I'd like to have some tips for a ternaryplot ("vcd").

I have this dataframe:

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

and I'm building a ternary plot:

ternaryplot(df[,1:3], df$d)

How can I map the continuous variable d, obtaining a result similar to this one?

enter image description here

like image 985
FraNut Avatar asked Jun 04 '12 09:06

FraNut


People also ask

What does a ternary plot show?

A ternary plot depicts the ratios of three variables as positions in an equilateral triangle.

What is a ternary diagram in geology?

Ternary diagrams are widely used in geology and other sciences to portray the proportions of three items that are constrained to sum to 100%. For example, sandstones are commonly composed of quartz, two main types of feldspar, and various types of rock fragments called lithics, as well as other types of grains.

Why is it important to have a ternary graph?

It is used to graphically visualize 3 ratios according to their position in an equilateral triangle. Ternary plots are basically used in the field of physical chemistry. Majorly if we try to plot 3 different variables on a 2D plane then we have to pass the third variable may be color or size, etc.


1 Answers

I needed to solve a similar problem, which was partially the catalyst for writing a package as an extension to ggplot2, for ternary diagrams. The package is available on CRAN.

The Output for this Problem: enter image description here

Code to Build the Above

#Orignal Data as per Question
a <- c(0.1, 0.5,0.5, 0.6, 0.2, 0          , 0         , 0.004166667, 0.45) 
b <- c(0.75,0.5,0  , 0.1, 0.2, 0.951612903,0.918103448, 0.7875     , 0.45)
c <- c(0.15,0  ,0.5, 0.3, 0.6, 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..),
                 n=400,
                 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|")

#Save Plot
ggsave("TernFilled.png")
like image 87
Nicholas Hamilton Avatar answered Oct 07 '22 20:10

Nicholas Hamilton