How can I create circular cladograms in R? - r

I want to produce a circular cladogram in R. I was trying out the ape package and could produce something like this:
plot(tree,'f', use.edge.length=F)
Now I am not really happy with how the edges look like here. I tried out the evolview webserver, which got me something like this, which looks much nicer, especially regarding the edges.
Can anyone suggest other R packages or a different approach with the ape package, to get similar results to the evolview tree?

The two main differences that stand out to me are the size of the labels and the relative lengths of the edges.
Label size can be controlled using the cex graphical parameter (using par(), or as a parameter to plot()).
Uniform edge length can be added to the tree by replacing the $edge.length property with a vector of 1s:
par(cex = 0.8) # Shrink text
tree$edge.length <- rep_along(1, tree$edge.length)
plot(tree, 'f', use.edge.length = TRUE)

Related

R igraph output vertice is not shown

I am using R igraph package to display gene networks. The plot on Rstudio is like this (I can't post image because I am new user and don't have enough reputation, sorry about that):
R igraph on preview
Now I want to draw this on file to clearly see the changes and there is always an issue on vertices near margin side like this:
part of output pdf file
My code is as follows`
pdf("graph.pdf",width = 20, height = 10)
par(mar = c(9,9,9,9))
plot(finalnet, edge.arrow.size=0.1, edge.curved=FALSE,vertex.size= 3, margin = -0.5)
dev.off()
Update: I have tried square layout and the problem persists, here is my plotting object and square plot.
square plot
rda file for my igraph object
Can anyone give me an suggestion how to solve this issue? To whole net is about 170 vertices but I don't know why it cannot be displayed on output file well. I have tried different plot options in mai, mar but this seems to fail.
The reason you are getting this behavior is because you are specifying margin in your plot call. margin=-0.5 is telling R to extend the plot 0.5 units past the graphics device dimensions, below are three examples:
Your original plotting call, notice the clipping
pdf("withMargin.pdf")
par(mar=c(9,9,9,9))
plot(g, margin=-0.5)
dev.off()
Without the call to par, problem still presists but now youuse the entire dimension of the graphics device.
png("withoutPar_Margin.png")
#par(mar=c(9,9,9,9))
plot(g, margin=-0.5)
dev.off()
Lastly, removing the margin in plot
png("withoutplotMargin.png")
par(mar=c(9,9,9,9))
plot(g)
dev.off()
You're specifying a rectangular size for what looks like a square object. Try a square size, as in
pdf("graph.pdf")
This will use the defaults, which are square.
But, it's hard to know for sure since you haven't given us the object to troubleshoot for you.

How to plot a graph with labels that don't superimpose on each others

I am new at R but I haven't find any topic that could answer my question.
I want to plot a graph named g, using the package igraph. However the result is not fully satisfying as the nodes names aren't all readable.
This is the code I used to plot :
plot(g, layout_nicely(g), asp=FALSE, vertex.shape=V(g)$shape, vertex.label.dist=0.3,
vertex.label.degree=-pi/2)
And this is the result
I've tried to reduce the number of characters of each label, or to reduce their font sizes but it wasn't enough. I also used several layout such as layout.kamada.kawai, layout.fruchterman.reingold but the labels are always too close to each others.
Is there any way to choose a better position for the labels or to increase the length of the edges ?
Any help would be appreciated

igraph plotting: vertices too close in Kamada-Kawai layout

I'm plotting a directed network from an edge list, and have so far created a tree-like plot (see here).
It looks good, however all of the nodes are too close together. I would like to keep the shape of it while spreading out the nodes more. Here's the code that got me the image above:
library(igraph)
ref <- read.csv("my-ref.csv", as.is=T)
el <- graph.data.frame(ref, directed=T)
lay.kk <- layout.kamada.kawai(el, niter=1000, kkconst=50)
plot.igraph(el, lay=lay.kk, vertex.label=NA, vertex.size=2, vertex.color="black")
I've tried messing around with kkconst, but that doesn't seem to change anything. Any tips are greatly appreciated!
The Kamada-Kawai layout does not really work well for disconnected graphs because the disconnected components tend to "drift away" from each other. Since igraph scales the entire plot to fit within the canvas, the farther the components are from each other, the closer the nodes will be within the components. Try the Fruchterman-Reingold layout instead.
As Tamás suggested, you might get better results with layout.fruchterman.reingold(). You can fine tune this function with the following parameters:
require(igraph)
g <- erdoss.renyi.game(n = 100, p.or.m = 0.04)
lo <- layout.fruchterman.reingold(g, repulserad = vcount(g)^2.8,
area = vcount(g)^2.3, niter = 1000)
plot(g, layout = lo, vertex.size = 3, vertex.frame.color = NULL,
vertex.label.dist = 0.5, vertex.label.cex = 0.7, edge.width = 0.5)
These values resulted a low overlap, clear but compact layout for me. Try to change them a bit, to see their effect on the layout. Those parameters I set for plot() also help to make the visualization more clear.
There is a similar question here to which I have posted the following answer
Option 1: make the vertices smaller
node.size= c(10,10,10)
plot(net, vertex.size=node.size*0.25)
Option 2 (in case the distances between the vertices are not important to you):
# Use the tkplot option to edit your graph in GUI
tkplot (net)
tkplot GUI will allow you to interactively change the layout types.
Note: tkplot outputs the graph as eps. If you want to edit it further or export it to pdf I suggest using inkscape (I use it for all my graph editing - just save the graph as pdf in RStudio and edit it in inkscape).
For the case of eps if you are on a windows machine you will need to tweak inkscape to open this format. A very short and simple process which is detailed here:

Link tip labels to phylogenetic tree using dots and fix overcrowded tip labels

I'm attempting to produce an ancestral reconstruction using the ape and phytools package in Rstudio. My problem is that in my phylogenetic tree the tip labels / species names are overcrowded and illegible. Currently, my tree has a dataset of 262 species.
An example nexus file of the data can be found here.
The Ancestral reconstruction tree I have produced so far is here: http://i.imgur.com/WFoEu7S.png.
Each species has a character state of 0 or 1 and has node and tip labels addressing each state. Eventually i'd like to color the branches with their respective character state(which I have as either red or black.)
Ideally, I wish to produce a non-ultrametric tree similar to a previous question on stack overflow in this link here.
I've tried implementing the R code from this link for my own tree with little success.
Below is my code in R. I am still learning R and am unfamiliar with certain plotting methods and suspect that may be the issue here:
tree = read.nexus("test_nexus")
dichot_tree = multi2di(tree)
dichot_tree$edge.length<-runif(n=nrow(dichot_tree$edge),min=0,max=1)
dichot_tree$edge.length[dichot_tree$edge.length<1]<-1
domest = read.nexus.data("test_nexus")
aceDISCRETE<-ace(as.numeric(domest), dichot_tree, type="discrete")
plot(dichot_tree, cex=0.5, label.offset=1, no.margin=TRUE)
tiplabels(pch=22, bg=as.numeric(domest),cex=1, adj=1)
nodelabels(pie=aceDISCRETE$lik.anc, piecol=c("black", "red"), cex=0.25)
There are a couple possible ways to make the tip labels more readable.
First, you could decrease the font size (that would be the cex parameter of the plot function).
Second, you are using RStudio, and it looks like you currently have your plot area as a square.
You can adjust the different panels to make the plot area a very tall rectangle, which would extend your tree when you plot it.
Alternatively, you could create an external plot area (I use windows() and you can specify height and width.)
Alternatively, when saving a plot in RStudio, you should be able to change the output height/width/aspect ratio. You should be able to make it much taller here as well.

Heatmap generation in r

I need some suggestions regarding the heatmap generation with heatmap.2 in R. I have a matrix for gene expression values which has 15,616 rows and 27 columns for generating the heatmap. The problem is the code I am using is creating the heatmap but the visualization is not good as the matrix size is large. So can you give me suggestions as to how to get a proper heatmap out of such a huge matrix? I am attaching the command I am using to generate the heatmap and the warnings I am receiving. It would be great if someone can help me with the adjustment of the dimensions to generate a proper heatmap.
color <- colorpanel(100,low="blue",mid="white",high="red")
heatmap.2(data4,Rowv="none",col=color,trace='none',
density.info="none",scale="row",labRow=NULL,
lmat=rbind( c(0, 3), c(2,1), c(0,4) ), lhei=c(1.5, 4, 2 ))
Warning message:
In heatmap.2(data4, Rowv = "none", col = color, trace = "none", :
Discrepancy: Rowv is FALSE, while dendrogram is `column'. Omitting row dendogram.
It would be nice to have suggestions regarding a proper visualization of the heatmap with the colour key panel small and the image more distinct on columns which are my conditions and the image shifted towards a bit right as when I am generating it is a bit shifted to left. I am unable to upload the image of the heatmap as I am new to the forum and don't have that privilege. I am unable to judge the appropriate values for the matrix while generating the heatmap.
Your question is a bit too vague to get a detailed answer, however, here are a couple of things to help you out:
Colours. Typically, you want the mid point to be zero. So you probably want to try something like:
breaks = c(seq(min(data4), 0, length.out=128),
seq(0, max(data4), length.out=128))
heatmap.2(..., col=bluered(255), breaks=breaks,...)
Your matrix is too large - well make it smaller. Typically, only "differentially expressed genes" are shown in the heatmap. So select the top 50 genes or so, and plot them.
I'm afraid I can't help you resolve your issue with heatmap.2, as I myself found it does not perform overly well with specific tasks and data sets.
I would recommend looking at ComplexHeatmaps which might potentially be more suited to such large volumes of data. It also has extensive supporting documents on how to use it that can be found here.
As of writing this, the following command with get you the very most up-to-date version from GitHub, though a stable version is available on Bioconductor.
Most Recent (you must use force = TRUE):
library(devtools)
install_github("jokergoo/ComplexHeatmap", force = TRUE)
library(ComplexHeatmap)
Stable:
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
BiocManager::install("ComplexHeatmap", version = "3.8")
I know it is sub-optimal to be told to change packages but it is a more comprehensive tool AND is very well explained in the support file. The creator jokergoo also appears to be highly attentive. Moreover, I am not sure how effectively / easily heatmap.2 can be coerced into displaying what I assume would be 421,632 data cells. I have just switched to ComplexHeatmaps and find it to be very thorough.
I'm going to leave some sample images below to show you the scope of ComplexHeatmaps given the amount of data you appear to have.
Simple heatmap example (doesn't have to be flashy):
Complex heatmap example (i):
Complex heatmap example (ii):

Resources