Last active
December 22, 2015 06:29
-
-
Save Loyale/6431630 to your computer and use it in GitHub Desktop.
Ternary plot with cummeRbund and ggplot2 (adapted from http://srmulcahy.github.io/2012/12/04/ternary-plots-r.html)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require(ggplot2) | |
require(grid) | |
require(scales) | |
ternary <- function(t, l, r, clr = clr, pts = pts, labels = c("top", "left", "right"), showGrid = TRUE){ | |
library(ggplot2) | |
ddf <- data.frame(t, l, r) | |
# convert points to cartesian coordinates | |
ddf$y <- (10 / 100)* t * 0.86603 | |
ddf$x <- (ddf$y * 0.57735) + ((10 / 100) * r) | |
ddf$clr <- clr | |
ddf$pts <- pts | |
# Find distance relative to each vertex | |
vt<-c(5,8.66) | |
vl<-c(0,0) | |
vr<-c(10,0) | |
# border | |
x1 <- c(0, 0 , 5) | |
x2 <- c(10, 5, 10) | |
y1 <- c(0, 0, 8.66) | |
y2 <- c(0, 8.66, 0) | |
bdf <- data.frame(x1, x2, y1, y2) | |
# ternary grid | |
x1 <- seq(1, 9, 1) | |
x2 <- seq(0.5, 4.5, 0.5) | |
x3 <- seq(5.5, 9.5, 0.5) | |
x4 <- seq(4.5, 0.5, -0.5) | |
y1 <- 0 | |
y2 <- seq(0.866, 7.794, 0.866) | |
y3 <- seq(7.794, 0.866, -0.866) | |
tdf <- data.frame(x1, x2, x3, x4, y1, y2, y3) | |
#Axes | |
# plot ternary diagram | |
if(showGrid == TRUE){ | |
base <- ggplot(ddf) + | |
# plot axis grid | |
geom_segment(data = tdf, aes(x = x1, y = y1, xend = x2, yend = y2), | |
linetype = "dashed", size = 0.25, colour = "grey50") + | |
geom_segment(data = tdf, aes(x = x1, y = y1, xend = x3, yend = y3), | |
linetype = "dashed", size = 0.25, colour = "grey50") + | |
geom_segment(data = tdf, aes(x = x4, y = y3, xend = x3, yend = y3), | |
linetype = "dashed", size = 0.25, colour = "grey50") + | |
# plot border | |
geom_segment(data = bdf, aes(x = x1, y = y1, xend = x2, yend = y2)) + | |
# remove cartesian elements | |
theme_bw() + | |
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), | |
panel.border = element_blank(), axis.ticks = element_blank(), | |
axis.text.x = element_blank(), axis.text.y = element_blank(), | |
axis.title.x = element_blank(), axis.title.y = element_blank()) + | |
# add plot labels | |
geom_text(aes(5, 9, label = labels[1]),data=data.frame()) + | |
geom_text(aes(-1, 0, label = labels[2]),data=data.frame()) + | |
geom_text(aes(11, 0, label = labels[3]),data=data.frame()) | |
} else { | |
# if grid==FALSE don't plot grid | |
base <- ggplot(ddf) + | |
# plot border | |
geom_segment(data = bdf, aes(x = x1, y = y1, xend = x2, yend = y2)) + | |
# remove cartesian elements | |
theme_bw() + | |
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), | |
panel.border = element_blank(), axis.ticks = element_blank(), | |
axis.text.x = element_blank(), axis.text.y = element_blank(), | |
axis.title.x = element_blank(), axis.title.y = element_blank()) + | |
geom_text(aes(5, 9, label = labels[1])) + | |
geom_text(aes(-1, 0, label = labels[2])) + | |
geom_text(aes(11, 0, label = labels[3])) | |
} | |
# plot data on top of base plot | |
p <- base + | |
geom_point(data = ddf, aes(x = x, y = y, colour = clr, pch = pts), size = 1.5) + | |
geom_point(x=5,y=3.5,color="black",size=2) + | |
scale_colour_brewer(palette="Set1") + | |
coord_equal(1) | |
p | |
} | |
#Make Ternary plot | |
cuff<-readCufflinks() | |
myConditions<-c("A","B","C") | |
myGenes<-getGenes(cuff,myGeneIDs) | |
fpkmMat<-fpkmMatrix(myGenes)[,colnames(fpkmMatrix(myGenes)) %in% myConditions] | |
fpkmMat<-t(makeprobs(t(fpkmMat))) | |
fpkmMat<-fpkmMat*100 | |
fpkmMat<-as.data.frame(fpkmMat) | |
labels<-myConditions | |
labels<-c("CP","VZ","SVZ_IZ") | |
p<-ternary(t=fpkmMat[[myConditions[1]]],l=fpkmMat[[myConditions[1]]],r=fpkmMat[[myConditions[1]]],labels=labels,clr="",pts="",showGrid=TRUE) | |
p |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment