Row labels not showing in heatmap using Heatmaply - r

I am using heatmaply to create a heatmap but unfortunately my row labels are not showing as names. Row labels are row numbers. How can I make my row names from column 1 show on the heatmap ?
Here is my code:
the row names are still not showing as labels, but rather as numbers (1,2,3...):
heatmaply(mtcars, k_col =10, k_row =1, row.names(mtcars) <- mtcars[,1], cexRow = 0.1, cexCol=10, margins =c(100,100))
Please advise
Thanks

I don't know what your heatmap should look like (I could not generate a map with the code you provided). However, I was able to generate a graph with the row labels (Mazda RX4, etc.) by simply removing row.names from your code:
heatmaply(mtcars, k_col =10, k_row =1, cexRow = 0.1, cexCol=10, margins =c(100,100))

Well the secret is using labRow= cars[,1] in the code as follows:
heatmaply(cars, k_col=14, k_row=1,labRow= cars[,1], cexRow=10, cexCol=10, margins=c(50,50), scale_fill_gradient_fun=ggplot2::scale_fill_gradient2(low="navy blue", high ="red",midpoint=1, limits=c(0,2.2)))

Related

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).

R multi boxplot in one graph with value (quantile)

How to create multiple boxplot with value shown in R ?
Now I'm using this code
boxplot(Data_frame[ ,2] ~ Data_frame[ ,3], )
I tried to use this
boxplot(Data_frame[ ,2] ~ Data_frame[ ,3], )
text(y=fivenum(Data_frame$x), labels =fivenum(Data_frame$x), x=1.25)
But only first boxplot have value. How to show value in all boxplot in one graph.
Thank you so much!
As far as I understand your question (it is not clear how the fivenum summary should be displayed) here is one solution. It presents the summary using the top axis.
x <- data.frame(
Time = c(1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3),
Value = c(5,10,15,20,30,50,70,80,100,5,7,9,11,15,17,19,17,19,100,200,300,400,500,700,1000,200))
boxplot(x$Value ~ x$Time)
fivenums <- aggregate(x$Value, by=list(Time=x$Time), FUN=fivenum)
labels <- apply(fivenums[,-1], 1, function(x) paste(x[-1], collapse = ", "))
axis(3, at=fivenums[,1],labels=labels, las=1, col.axis="red")
Of course you can additionally play with the font size or rotation for this summary. Moreover you can break the line in one place, so the label will have smaller width.
Edit
In order to get what have you posted in the comment below you can add
text(x = 3 + 0.5, y = fivenums[3,-1], labels=fivenums[3,-1])
and you will get
however it won't be readable for other boxplots.

Heatmap for a large matrix and get the clear labels

How to draw a heatmap from a matrix of 305 columns and 865 rows in R.
The code I have written for the matrix is
nba <- read.csv("mydata.csv", sep=",")
row.names(nba) <- nba[,1]
nba <- nba[,2:865]
nba_matrix <- data.matrix(nba)
nba_heatmap <- heatmap(nba_matrix, Rowv=NA, Colv=NA, col = brewer.pal(9, "Blues"), scale="column", margins=c(5,10))
Now the code gives me the heatmap as shown bellow, but the labels are not clear. Please help me to get a clear heatmap.
since you stated that you need all the labels, the only way I see is reducing the font size. You can do this by setting the cexCol and cexRow parameters in your call to heatmap(); for example like this:
heatmap(as.matrix(iris[,1:3]),cexRow = 0.1, cexCol = 0.1,)

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)

heatmap in R how to resize columns labels?

I have a data.matrix that is approximately 4000 rows and 100 columns. I am doing a heatmap of the data like:
data<-heatmap(data_matrix,Rowv=NA,Colv=NA,col=cm.colors(256),scale="column",margins=c(5,10))
But the problem that I got is that the labels that appear in the column are too grouped, so it is impossible to visualize them correctly. How I can resize the heatmap so I can see the values of the labels of the column? I tried to print it in pdf, but it only appears a black stripe.
Thanks
I am including a figure of the heatmap, the portion that I want to see are the labels that are in the right part, but they are too close together.
First of all it's better to put your output directly to a PDF file - you may use other image formats but PDF is the best because it is a vector output and you can zoom as much as you want:
pdf("Your-file.pdf", paper="a4", width=8, height=8)
Then it's better to use pheatmap( = pretty heatmap) package. It makes really better heatmaps with a color key besides your heatmap. Finally although the pheatmap() function tries to reduce the label size while you have many rows, but it fails for really large number of rows. So I use the code below for really high - but not too high - number of rows:
library(pheatmap)
library(gplots)
if (nrow(table) > 100) stop("Too many rows for heatmap, who can read?!")
fontsize_row = 10 - nrow(table) / 15
pheatmap(table, col=greenred(256), main="My Heatmap", cluster_cols=F,
fontsize_row=fontsize_row, border_color=NA)
You may change fontsize_col for the column labels. You have many interesting options like display_numbers to have the values inside the cells of your heatmap. Just read ?pheatmap.
This is an example generated by the default parameters of pheatmap() command:
Finally note that too many rows are easy to read on a display, but useless for print.
In Rstudio you can easily resize the graphic window, same holds for Rgui. Alternatively, if you save the plot to file you can use a bigger size for your graphics, e.g. bigger width and height when calling pdf or png.
You can use cexRow = and cexCol =.
You can get more information into ??heatmap.2
# Row/Column Labeling
margins = c(5, 5),
ColSideColors,
RowSideColors,
cexRow = 0.2 + 1/log10(nr),
cexCol = 0.2 + 1/log10(nc),
labRow = NULL,
labCol = NULL,
srtRow = NULL,
srtCol = NULL,
adjRow = c(0,NA),
adjCol = c(NA,0),
offsetRow = 0.5,
offsetCol = 0.5,
colRow = NULL,
colCol = NULL
If you use pheatmap (https://www.rdocumentation.org/packages/COMPASS/versions/1.10.2/topics/pheatmap) you can spread out those labels by adjusting the cellheight parameter.
If you are doing this in R notebook, even though the entire heat map will not display in your output window when you run the code, when you save the heat map to your computer using the filename parameter, pheatmap will automatically calculate the optimal size for the output file so that your entire heatmap will be displayed in your output file. If this size is not to your liking you can adjust using width and height parameters, but it is unlikely you will want to do this.

Resources