Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing lines on a Ternary Plot

Tags:

r

I have a ternary plot that I'm generating like this:

library(vcd)
ternaryplot(abs(replicate(3, rnorm(50))), grid=FALSE)

I don't like the built-in grid so I disabled it, but I want to draw some lines of my own: specifically I want to draw a line from each point of my triangle to the midpoint of the opposite face (i.e. three lines bisecting my triangle and crossing at the center) and I can't quite figure out how to do this. I tried abline() but nothing seems to happen when I do.

How can I draw lines on this plot?

like image 617
nicolaskruchten Avatar asked Dec 20 '22 20:12

nicolaskruchten


2 Answers

Try this. Most of it I gathered from reading the code for ternaryplot:

library(vcd)
ternaryplot(abs(replicate(3, rnorm(50))), grid=FALSE)

top <- sqrt(3)/2
xlim <- c(-0.03, 1.03)
ylim <- c(-1, top)
pushViewport(viewport(width = unit(1, "snpc")))
pushViewport(viewport(width = 0.8, height = 0.8, xscale = xlim, 
                      yscale = ylim, name = "plot"))
grid.lines(c(0.75, 0.00), c(0.5 * top, 0))
grid.lines(c(0.25, 1.00), c(0.5 * top, 0))
grid.lines(c(0.50, 0.50), c(1.0 * top, 0))
upViewport(2)

enter image description here

like image 130
flodel Avatar answered Dec 22 '22 09:12

flodel


Using the ggtern package which I have recently published on CRAN, the following can be achieved:

Output

Which can be produced with the following code:

library(ggtern)
DATA <- data.frame(x = c(1,0,0),
                   y = c(0,1,0),
                   z = c(0,0,1),
                   xend = c(0,.5,.5),
                   yend = c(.5,0,.5),
                   zend = c(.5,.5,0),
                   Series = c("yz","xz","xy"))
ggtern(data=DATA,aes(x,y,z,xend=xend,yend=yend,zend=zend)) + 
  geom_segment(aes(color=Series),size=1) +
  scale_color_manual(values=c("darkgreen","darkblue","darkred")) +
  theme_bw() + theme_nogrid() + 
  theme(legend.position=c(0,1),legend.justification=c(0,1)) + 
  labs(title = "Sample Midpoint Segments")
like image 34
Nicholas Hamilton Avatar answered Dec 22 '22 09:12

Nicholas Hamilton