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?
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)
Using the ggtern package which I have recently published on CRAN, the following can be achieved:
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With