Skip to content

Instantly share code, notes, and snippets.

@rmaia
Last active December 16, 2015 13:09
Show Gist options
  • Save rmaia/5439815 to your computer and use it in GitHub Desktop.
Save rmaia/5439815 to your computer and use it in GitHub Desktop.
Use scatterplot3d to plot a tetrahedral color space
# (very convoluted) function to plot tetrahedral colorspace
require(pavo)
require(scatterplot3d)
require(RColorBrewer)
# get the data
data(sicalis)
tcsdat <- tcs(vismodel(sicalis))
# filter only xyz coordinates
tcsdat.xyz <- tcsdat[,c('x','y','z')]
# make a color palette
pal <- brewer.pal(3, 'Set1')
# TETRAHEDRAL FUNCTION
# note that if points to be plotted are changed, the whole function needs to be
# sourced back into R
tetraplot<-function(Y=NULL,angle=65,scale.y=0.45)
{
par(mar=c(1,1,1,1))
if(!is.null(Y)){
test<-scatterplot3d(x=Y$x, y=Y$y, z=Y$z, box=TRUE, pch=16, color=rgb(0,0,0,.5),
xlim=c(-1.22,.612), ylim=c(-.35,.707), zlim=c(-.25,.75),
axis=F, grid=F, angle=angle, scale.y=scale.y, cex.symbols=1, mar=c(1,1,1,1))
}
if(is.null(Y)){
test<-scatterplot3d(x=0, y=0, z=0, box=TRUE, pch=16, color=rgb(0,0,0,.5),
xlim=c(-1.22,.612), ylim=c(-.35,.707), zlim=c(-.25,.75),
axis=F, grid=F, angle=angle, scale.y=scale.y, cex.symbols=1, mar=c(1,1,1,1))
}
#################################
# INSERT POINT INFORMATION HERE #
#################################
test$points3d(subset(tcsdat.xyz, 'C'), pch=20, col=pal[1])
test$points3d(subset(tcsdat.xyz, 'B'), pch=20, col=pal[2])
test$points3d(subset(tcsdat.xyz, 'T'), pch=20, col=pal[3])
#########################
# END POINT INFORMATION #
#########################
# Vertex coordinates
uv<-test$xyz.convert(0,0,.75)
blue<-test$xyz.convert((-1*sqrt(1.5)),(-1/(2*sqrt(2))),-.25)
green<-test$xyz.convert(0,(1/sqrt(2)),-.25)
red<-test$xyz.convert((.5*sqrt(1.5)),(-1/(2*sqrt(2))),-.25)
no.uv<-test$xyz.convert(0,0,-.25)
# Add text to vertices (can also be changed to points if preferred)
text(red$x,red$y,"red")
text(green$x,green$y,"green")
text(blue$x,blue$y,"blue")
text(uv$x,uv$y,"uv")
# Draw tetrahedron
segments(uv$x,uv$y,red$x,red$y)
segments(uv$x,uv$y,green$x,green$y)
segments(uv$x,uv$y,blue$x,blue$y)
segments(red$x,red$y,green$x,green$y)
segments(green$x,green$y,blue$x,blue$y)
segments(blue$x,blue$y,red$x,red$y)
}
# Call the function to plot
tetraplot()
# angle and scale arguments can be used to modify the plot
tetraplot(, angle=45)
# add legend
legend('topright', pch=20, col=pal, legend=c('crown','breast','throat') )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment