```
#' Make two lists from three
#'
#' This function accepts a list of three lists, each of which has a length of two. It
#' returns a list of length two, each of which has a length three. This is intended to be
#' used to turn three lists of up/down genes into a list of up genes from three sources,
#' and a list of down genes from three sources.
#'
#' @param x A list of three lists, each of which has a length of two
#'
#' @return A list of two lists, each of which has a length of three
#'
#' @examples
#' three2two(list(das = das, cabo = cabo, das_cabo = das_cabo))
#'
#' @export
three2two <- function(x) {
if((length(x) != 3) | (class(x) != "list")) {
stop("x must be a list of length 3")
}
up <- list(
a = x[[1]][[1]],
b = x[[2]][[1]],
c = x[[3]][[1]]
)
down <- list(
a = x[[1]][[2]],
b = x[[2]][[2]],
c = x[[3]][[2]]
)
names(up) <- names(x)
names(down) <- names(x)
venn <- list(up = up, down = down)
return(venn)
}
#' Find overlaps
#'
#' This function takes a list of length three. It returns the lengths
#' of each list, the lengths of their overlaps, and the length of the
#' overlap of all three lists.
#'
#' @param x A list of length three
#'
#' @return A list of the areas and overlaps
#'
#' @examples
#' getOverlaps(mylist)
#'
#' @export
getOverlaps <- function(x) {
circ.area <- sapply(x, length)
overlaps <- list(
com12 = intersect(x[[1]], x[[2]]),
com23 = intersect(x[[2]], x[[3]]),
com13 = intersect(x[[1]], x[[3]]),
com123 = Reduce(intersect, x))
overlaps <- sapply(overlaps, length)
return(c(circ.area, overlaps))
}
#' Make a 3-way venn diagram
#'
#' This function creates a venn diagram from a list of length three.
#'
#' @param x A list of length three
#' @param maintitle The main title of the plot
#' @param cats a character vector of category labels (optional)
#'
#' @return A 3-way venn diagram
#'
#' @examples
#' makeVenn3(up, "Differentially expressed genes")
#'
#' @export
makeVenn3 <- function(x, maintitle, cats = NULL) {
if(class(x) != "list"){
stop("x must be a list")
}
if(length(x) != 3){
stop("x must be a list of length 3")
}
circ.area <- lapply(x, length)
overlaps <- list(
com12 = intersect(x[[1]], x[[2]]),
com23 = intersect(x[[2]], x[[3]]),
com13 = intersect(x[[1]], x[[3]]),
com123 = Reduce(intersect, x))
overlaps <- lapply(overlaps, length)
if(is.null(cats)){
cats <- names(x)
}
grid.newpage()
draw.triple.venn(area1 = circ.area[[1]], area2 = circ.area[[2]], area3 = circ.area[[3]],
n12 = overlaps[[1]], n23 = overlaps[[2]], n13 = overlaps[[3]], n123 = overlaps[[4]],
category = cats, lty = "blank", fill = c("blue", "red", "mediumorchid"),
margin = .05)
grid.text(maintitle, x = unit(0.5, "npc"), y = unit(0.95, "npc"))
}
#' Create two three-way Venn diagrams
#'
#' This function is a wrapper to apply makeVenn3 to a list of two lists (of three lists).
#'
#' @param x A list of two lists, each of which is a list of three lists
#' @param titles A character vector of plot titles
#'
#' @return Two 3-way venn diagrams
#'
#' @examples
#' doubleVenn(list, c("Genes with lfc > 2 and fdr < 0.05", "Genes with lfc < -2 and fdr < 0.05"))
#'
#' @export
doubleVenn <- function(x, titles) {
if((length(x) != 2) | (class(x) != "list")) {
stop("x must be a list of length 2")
}
makeVenn3(x[[1]], maintitle = titles[1])
makeVenn3(x[[2]], maintitle = titles[2])
}
#' Make a 3-way venn diagram for Shiny
#'
#' This function is identical to makeVenn3, but with aesthetic
#' changes for use in a shiny application.
#'
#' @param x A list of length three
#' @param maintitle The main title of the plot
#' @param cats a character vector of category labels (optional)
#'
#' @return A 3-way venn diagram
#'
#' @examples
#' makeShinyVenn3(up, "Differentially expressed genes")
#'
#' @export
makeShinyVenn3 <- function(x, maintitle, cats = NULL, col = NULL) {
if(class(x) != "list"){
stop("x must be a list")
}
if(length(x) != 3){
stop("x must be a list of length 3")
}
areas <- getOverlaps(x)
if(is.null(cats)){
cats <- names(x)
}
if(is.null(col)){
col <- c("blue", "red", "mediumorchid")
}
grid.newpage()
draw.triple.venn(area1 = areas[1], area2 = areas[2], area3 = areas[3],
n12 = areas[4], n23 = areas[5], n13 = areas[6],
n123 = areas[7], lty = "blank",
category = cats, fill = col, margin = .05,
cex = rep(2.5, 7), cat.cex = rep(2.5, 3),
fontfamily = rep("sans", 7),
cat.fontfamily = rep("sans", 3))
grid.text(maintitle,
x = unit(0.5, "npc"),
y = unit(0.95, "npc"),
gp=gpar(fontsize=20, col="black"))
}
#' Make a 3-way up/down venn diagram for Shiny
#'
#' This function is identical to makeShinyVenn3, except that it accepts
#' two lists, each of which is a list of three lists. The two lists are
#' intended to contain genes/similar, separated by direction of change.
#'
#' @param x A list of two lists, each of which is a list of length three
#' @param maintitle The main title of the plot
#' @param cats a character vector of category labels (optional)
#'
#' @return A 3-way venn diagram
#'
#' @examples
#' makeShinyVenn3updown(updown, "Differentially expressed genes")
#'
#' @export
makeShinyVenn3updown <- function(x, maintitle, cats = NULL, col = NULL) {
if(class(x) != "list"){
stop("x must be a list")
}
if (length(x) != 2){
stop("x must be a list of length 2")
}
if (length(x[[1]]) != 3){
stop("x must consist of two lists, each with a length of 3")
}
areas <- sapply(x, getOverlaps) %>% apply(., 1, sum)
if(is.null(cats)){
cats <- names(x[[1]])
}
if(is.null(col)){
col <- c("blue", "red", "mediumorchid")
}
grid.newpage()
draw.triple.venn(area1 = areas[1], area2 = areas[2], area3 = areas[3],
n12 = areas[4], n23 = areas[5], n13 = areas[6],
n123 = areas[7], lty = "blank",
category = cats, fill = col, margin = .05,
cex = rep(2.5, 7), cat.cex = rep(2.5, 3),
fontfamily = rep("sans", 7),
cat.fontfamily = rep("sans", 3))
grid.text(maintitle,
x = unit(0.5, "npc"),
y = unit(0.95, "npc"),
gp=gpar(fontsize=20, col="black"))
}
```

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.