Draw() in complexheatmap changes the order of row annotations - r

I am trying to add row annotations to my heatmap using complex heatmap.
s=Heatmap(total_df,
cluster_columns = FALSE,
column_split = factor(col_split$chr,levels = orderss),
column_order = row.names(col_split),
show_column_names = FALSE,
)
and my annotation heatmap is
anno=Heatmap(order_df[,'type'],name='type' ,
width = unit(1, "cm"),col=colors)
If i draw my annotation heatmap, it returns correct order
but when i try to combine it with primary heatmap using draw(), it changes the entire order.
draw(s+anno)
It would be great if anybody has an idea what is causing this. Thank you very much in advance. :)

Related

heatmap.2 change color key value / how to use key.xtickfun()

I am trying to customize the color key of my heaatmap.
I was able to correct the position and the size of it (yay), but i am at a loss on how to
change the values that are shown. The color key shows something called "colum Z scale", some default setting i presume.
I would like the color key to show the min and the max value of my data and some points in between.
Unfortunately, this must be so easy that no one cares to explain it in any guide anywere i checked (yes, feeling quite dumb about it... ).
I am quite sure that it should be possible to control this with the key.xtickfun function... but since i am new to R (and programming alltogehter) i cannot understand the documentation well enough to solve the problem on my own, so i am hoping to finde a little help here.
The code for my heatmap and the librarys is use are as follows:
library(ggplot2)
library(dplyr)
library(RColorBrewer)
library("gplots")
library(lattice)
heatmap.2(data_matrix
, dendrogram = "none"
, Rowv = FALSE
, Colv = FALSE
, scale = "column"
, trace = "none"
, margins = c(9,11)
, cexRow = 0.9
, cexCol = 0.9
, col = colors
, density.info = "none"
, keysize = 0.75
, lmat = rbind(c(3,4), c(2,1))
, key.xlab = "Wow, i can change the text! -_- Not what i was searching for"
, key.xtickfun=function() {
breaks <- parent.frame(n=1)$breaks
return(list(
at=parent.frame()$scale01(c(breaks[1],
breaks[length(breaks)])),
labels=c(as.character(breaks[1]),
as.character(breaks[length(breaks)]))
))
}
)
I tried to play around a bit with the variables in the function, but there are just too many possibilities and dependencies... it is like trying to catch the only fish in the ocean, using one tiny stick.... while beeing blindfolded....
So, if anyone can explain to me how to control the values of the Color key, i would be very thankful.
Best,
S.

R spplot: colorbar rather than legend for SpatialPoint data

I am plotting a SpatialPoint dataframe in R using spplot, and I would like to use a colorbar rather than a legend, to portray color values. (It's more efficient, and I want the map to "match" previous, raster data maps.) I'm sure this is possible, but can find no examples of it online. Could anyone give me a hint?
My current code is:
my.palette <- brewer.pal(n = 9, name = "Spectral")
my.palette<- rev(my.palette)
pols1 <- list("sp.lines", as(ugborder, 'SpatialLines'), col = gray(0.4), lwd = 1)
pols2 <- list("sp.polygons", as(water_ug, 'SpatialPolygons'), fill = 'skyblue1',col="transparent", first = FALSE)
spplot(ughouseszn,zcol="lzn_sg_clng",cex = .75,
key.space="right", digits=1,
par.settings = list(axis.line = list(col = 'transparent')),
xlim = bbox(ugborder)[1, ],ylim = bbox(ugborder)[2, ],
col.regions = my.palette, cuts=8,
sp.layout=list(pols1, pols2))
Where ugborder and water_ug give Uganda's borders and water, ughouseszn is a SpatialPointsDataframe, and the resulting map is here:
(As a side note, I'm hoping that adding a colorbar will lead to a more efficient use of space -- right now there's a lot of extra space at the top and bottom of Uganda's border, which is useless, and also does NOT appear when I map raster data using spplot, with the same pols1 and pols2.)
If "lzn_sg_clng" is converted to a factor, does this give you the map you desire? (rendering by the category rather than a graduated scale).

Control plot layout in foodweb plot from mvbutils R-package

I would like to visualize how functions in my own R package depend on each other. For this purpose I use the foodweb() function from the mvbutils package.
I can get the right functional dependencies out without a problem but the plot looks a bit messy, with lines crossing each other and function names not aligned vertically or horizontally.
Is there a way to control the layout of the plot similar to the way this works in the igraph package?
Example
dirPath <- "~/dev/stackoverflow/46910042"
setwd(dirPath)
## Download example Package
urlPackage <- "https://github.com/kbroman/qtlcharts/archive/master.zip"
download.file(urlPackage, destfile = "master.zip")
unzip("./master.zip", exdir = dirPath, overwrite = TRUE)
## Install or load mcbutils
if (!require(mvbutils)) install.packages("mvbutils")
thefiles = list.files(path = "./qtlcharts-master/R/", full.names = TRUE)
thefiles
## Now we load all the package files into memory, so we can have
## foodweb generate a map of the package functions.
sapply(thefiles, source)
## Generate plot
par(mar = rep(0.1, 4))
foodweb(border = TRUE, boxcolor = "pink", lwd = 1.5, cex = 0.8)
Plot Output:
Michael,
One option is to look behind the curtains of foodweb. The mvbutils::foodweb function returns an object of (S3) class foodweb. This has three components:
funmat a matrix of 0s and 1s showing what (row) calls what (column). The dimnames are the function names.
x shows the x-axis location of the centre of each function’s name in the display, in par("usr") units
level shows the y-axis location of the centre of each function’s name in the display, in par("usr") units.
thus one approach we can take is to call foodweb but tell it not to create a plot rather return a foodweb object. This then allows us to manipulate the data directory or via graphics::plot() externally of the defaults provided by the mvbutils::foodweb() function.
Why? Well, to do what you suggest my sense is three options exist:
You can either play with mvbutils::foodweb() parameters.
Work with data structure returned with another plotting package.
Use graphics::par() and graphics::plot to manipulate the plot size and attributes of the foodweb structure returned.
It would be great to know your preference. Excluding, that my sense was to provide a base example:
Plot Package Example
In the case of using graphics::plot, you need to go look at how you manipulate graphics:par. par() allows you to set or query graphical parameters. For example, if we want to clean up the function plot you might choose to modify the grahics::par() fin parameter to increase the figure region dimensions, (width, height), in inches. A simple example but my sense it helps map out and demonstrate the options available to you.
## Generate plot
if (!require(qtlcharts)) install.packages("qtlcharts")
## Here we specify `asNamespace` to get the package internals
fw <- foodweb( where = asNamespace( "qtlcharts"),
plotting = FALSE,
)
#Display foodweb structure
str(fw)
# Expand plot figure region dimensions...
par(fin = c(9.9,7))
# Plot fw strucuture
plot(fw,
border = TRUE,
expand.xbox = 1,
boxcolor = "pink", lwd = 1.5, cex = 0.8)
Plot Output example
Note that the function names are not spaced out. Note I cut the top and bottom white of plot here. In this case, you can play with the par constraints such as margin to get the plot you want.
Pruning your plot
Another option within the constraints of mvbutils::foodweb is to use the prune and rprune option to simplify your plots. These are super poweful and useful especially the regular expression version.
if (!require(qtlcharts)) install.packages("qtlcharts")
fw <- foodweb( where = asNamespace( "qtlcharts"),
plotting = FALSE)
str(fw)
par(fin = c(9.9,7))
plot(fw,
border = TRUE,
expand.xbox = 1,
boxcolor = "pink", lwd = 1.5, cex = 0.8)
fw <- foodweb( where = asNamespace( "qtlcharts"),
rprune = "convert_", ## search on `convert_` to negate use `~convert_`
plotting = FALSE)
str(fw)
par(fin = c(9.9,7))
plot(fw,
border = TRUE,
expand.xbox = 1,
boxcolor = "pink", lwd = 1.5, cex = 0.8)
Hoping the above information points you in the right direction.
T.
Because of the fact that there are many data, connections etc, the plot is squeezed in order to fit in the screen, hence it becomes messy.
What I would suggest is to save it in a PDF or PNG with big enough width and Height and then you can zoom in. This will save you a lot of time.
E.G.
## Generate plot
pdf( "mygraph.pdf", width = 50, height = 80 )
par(mar = rep(0.1, 4))
foodweb(border = TRUE, boxcolor = "pink", lwd = 1.5, cex = 0.8)
dev.off()
In addition, you can play with the plot options of foodweb.
Hope it helps.

Superheat Use Given Cluster Group

I generated dendrogram and have obtained k clusters. I wanted to obtain heatmap by using the existing dendrogram and clusters. I want to display my heatmap in multiple pages so I did them one by one in separate calls. To make the colour chart comparable, I also set pal.values. Here’s my code sample:
sh <- superheat(X = cbind(dt[grep("Action", colnames(dt))], profile),
heat.pal = brewer.pal(8, 'RdBu')[8:1],
heat.pal.values = c(-1.4:2.3),
bottom.label = 'variable',
grid.hline.col = '#F0EBEB',
grid.vline.col = '#F0EBEB',
smooth.heat = TRUE,
membership.rows = hc$cluster,
column.title = 'factors', row.title = 'Clusters',
bottom.label.text.size = 1.5, bottom.label.text.angle = 90)
sh$membership.rows == hc$cluster
From there, when I did checking if the memberships are matched, its displayed a mix of TRUE AND FALSE. I couldn’t find out what’s wrong with my code. Would you please enlighten me?

how to make a biplot without label in R

I used
biplot(prcomp(data, scale.=T), xlabs=rep("·", nrow(data)))
but it did not work to omit the labels.
Even if I remove the labels my plot is so messy and ugly which can be seen below!
I also need to show the percentage of PCs on axes
I used the following command to plot the image
biplot(prcomp(data, scale.=T), xlabs=rep("·", nrow(data)), ylabs = rep("·", ncol(data)))
Try this one
\devtools::install_github("sinhrks/ggfortify")
library(ggfortify)
ggplot2::autoplot(stats::prcomp(USArrests, scale=TRUE), label = FALSE, loadings.label = TRUE)

Resources