Category: gplots

In the R post, we will present how to create your own color palettes and how to work with other palettes such as RColorBrewer, wesanderson and hex codes from http://www.colorcombos.com for exciting color palettes.

Advertisements

There are several color palettes available in R such as rainbow(), heat.colors(), terrain.colors(), and topo.colors(). We can visualize these as 3D pie charts using the plotrix R package.

Other popular color palettes include the RColorBrewer package that has a variety of sequential, divergent and qualitative palettes and the wesandersonpackage that has color palettes derived from his films.

# To get an idea of the colors available
head(colors())
length(colors()) # 657
# To see all 657 colors as a color chart you can source the R script to generate a pdf version in your working directory

# We can create choose a palette based on the R chart as follow:
mycols <- colors()[c(8, 5, 30, 53, 118, 72)] #
# or you could enter the color names directly
# mycols <- c("aquamarine", "antiquewhite2", "blue4", "chocolate1", "deeppink2", "cyan4")
# You could also get and store all distinct colors in the cl object and use the sample function to select colors at random
cl <- colors(distinct = TRUE)
set.seed(15887) # to set random generator seed
mycols2 <- sample(cl, 7)

You can also create color palettes with hex color codes. A great example of this is to work with popular color palettes available on the http://www.colorcombos.com website. This website has various palettes you can choose from and even derive color palettes from your favorite websites. For example, let’s grab the color palette from the rjbioinformatics.com website at http://www.colorcombos.com/grabcolors.html .

After entering the URL of our website, we will receive the hex codes for the color scheme used on the website.

We can even export the colors as little pencils 🙂

You can also choose from hundred of color schemes based on your color of choice. For example, we will also create a color palette based on the color olive – ColorCombo382.

We can also create a color palette with the colorRampPalette() to use for heatmaps and other plots. For this example, we will use the leukemia dataset available in the GSVAdata package, which corresponds to microarray data from 37 human acute leukemias where 20 of these cases are Acute lymphoblastic leukemia (ALL) and the other 17 are ALL’s with Mixed leukemia gene rearrangements. For more information on the study please see Armstrong et al. Nat Genet 30:41-47, 2002.

In this post, we will learn how to create venn diagrams for gene lists and how to retrieve the genes present in each venn compartment with R.

In this post, we will learn how to create venn diagrams for gene lists and how to retrieve the genes present in each venn compartment with R.

In this particular example, we will generate random gene lists using the molbiotools gene set generator but you can use your own gene lists if you prefer. Specifically, we will generate a random list of 257 genes to represent those that are upregulated in condition and another list of 1570 genes to represent those that are upregulated in condition B.

Then, we will sort and paste the gene lists in an excel document we will save as randomGeneLists.xlsx.

Now, let’s load the data into R using the gdata package.

library("gdata")
geneLists <- read.xls("randomGeneLists.xlsx", sheet=1, stringsAsFactors=FALSE, header=FALSE)
head(geneLists)
# Notice there are empty strings to complete the data frame in column 1 (V1)
tail(geneLists)
# To convert this data frame to separate gene lists with the empty strings removed we can use lapply() with our home made function(x) x[x != ""]
geneLS <- lapply(as.list(geneLists), function(x) x[x != ""])
# If this is a bit confusing you can also write a function and then use it in lapply()
removeEMPTYstrings <- function(x) {
newVectorWOstrings <- x[x != ""]
return(newVectorWOstrings)
}
geneLS2 <- lapply(as.list(geneLists), removeEMPTYstrings)
# You can print the last 6 entries of each vector stored in your list, as follows:
lapply(geneLS, tail)
lapply(geneLS2, tail) # Both methods return the same results
# We can rename our list vectors
names(geneLS) <- c("ConditionA", "ConditionB")
# Now we can plot a Venn diagram with the VennDiagram R package, as follows:
require("VennDiagram")
VENN.LIST <- geneLS
venn.plot <- venn.diagram(VENN.LIST , NULL, fill=c("darkmagenta", "darkblue"), alpha=c(0.5,0.5), cex = 2, cat.fontface=4, category.names=c("A", "B"), main="Random Gene Lists")
# To plot the venn diagram we will use the grid.draw() function to plot the venn diagram
grid.draw(venn.plot)
# To get the list of gene present in each Venn compartment we can use the gplots package
require("gplots")
a <- venn(VENN.LIST, show.plot=FALSE)
# You can inspect the contents of this object with the str() function
str(a)
# By inspecting the structure of the a object created,
# you notice two attributes: 1) dimnames 2) intersections
# We can store the intersections in a new object named inters
inters <- attr(a,"intersections")
# We can summarize the contents of each venn compartment, as follows:
# in 1) ConditionA only, 2) ConditionB only, 3) ConditionA & ConditionB
lapply(inters, head)

Now you are ready, to review the genes in each section of the venn diagram separately. Alternatively, you can always use Venny web tool that is a great way to start looking at your data and then write a modified version of this R script to make a more exhaustive figure or facilitate downstream analysis in your script.