resize and adjust the heatmap in heatmap.2 - r

I've generated a heatmap like this:
The X-axis and Y-axis labels don't show completely.
My code is here:
heatmap.2(x,col=blueyelred,colsep=c(1:6),rowsep=(1:62),
sepwidth=c(0.05,0.05), sepcolor="white", trace="none",
Rowv=F,Colv=F, scale="none", dendrogram="none",key=F,
lhei = c(0.05,5),margins=c(1,8))
Is there any way to adjust it.
Thanks!
Cam

A few tips:
to reduce the font size, use the cexRow and cexCol arguments, e.g.
heatmap.2(x, ...., cexRow=0.5)
Adjust the values in the hlei and margins arguments.

I see that you are already using the margins parameter, what if you just modify the arguments as #csgillespie suggested?
e.g., margins = c(8, 8)
Maybe also modifying the general plot margins before calling the heatmap() function could help
e.g., par(mar=c(10,4,4,2))
or for the outer margin
par(oma=c(10,4,4,2))
where
par(mar=c(bottom,left,top,right))

Related

How do I edit the dendrogram whilst using heatmap.2 function in r

I am trying to create a heatmap to represent the change of gene expression over a period of time. the code I have used is this:
coul <- colorRampPalette(brewer.pal(8, "Reds"))(25)
heatmap.2(dm, dendogram=c("row"),Colv=NA, xlab="Time points", ylab="Genes of interest", scale="row", col=coul, tracecol = NA)
As one cant really see the branches of the dendogram that well, I was wondering whether you can somehow stretch it out to become more visible?
I was also wondering how to remove the "colour key and histogram" label.
Many thanks!
if you don't mind the color guide being wide, you just use the lwid option, you specify a vector that decides the ratio of dendrogram to heatmap, below I use c(3,3), which means 1:1.
set.seed(100)
x = matrix(rnorm(1000),100,10)
heatmap.2(x,trace="none",Colv=NA,dendrogram=c("row"),tracecol = NA)
heatmap.2(x,trace="none",Colv=NA,
dendrogram=c("row"),lwid=c(3,3),tracecol = NA,keysize=0.75)
One way to narrow the margins of the color guide is to use key.par, where you set the margins on the right to be larger (I use 10 in example below).
heatmap.2(x,trace="none",Colv=NA,dendrogram=c("row"),
lwid=c(3,3),tracecol = NA,keysize=0.75,key.par=list(mar=c(3,3,3,10)))

R: Automatically expand margins in VIM::aggr plots

I'm trying to visualise missing data with the R package VIM.
I'm using R version 3.4.0 with RStudio
I've used the function aggr() but the colnames of my dataframe seem to be too long. Thus, some labels of the x axis don't appear.
I would like to increase the space at the bottom of the x axis.
library(VIM)
aggr(df)
Here is my dataframe df and the plot I obtain
I've tried with par() function but it doesn't change anything.
aggr(df,mar=c(10,5,5,3))
or
par(mar=c(10,5,5,3))
g=aggr(df,plot=FALSE)
plot(g)
I can reduce the font size with cex.axis but then labels are too small.
aggr(df,cex.axis=.7)
Here is the plot with small axis labels:
I've not find a lot of examples using aggr() that's why I ask for your help.
Thank you in advance.
I think you are looking for a graphical parameter oma which will allow you to resize the main plot. The help reference states:
For plot.aggr, further graphical parameters to be passed down. par("oma") will be set appropriately unless supplied (see par).
In your case you could do something like:
aggr(df, prop = T, numbers = F, combined = F,
labels = names(df), cex.axis = .9, oma = c(10,5,5,3))
Obviously, you need to play around with cex.axis and other parameters to find out what works best for your data.

R-plot a centered legend at outer margins of multiple plots

I want to plot a centered legend outside of the plotting area in a device having multiple plots. There has been many questions (with slight variations) asked in SO about changing the position of legend in a R plot.
For example:
1) R - Common title and legend for combined plots
2) Common legend for multiple plots in R
3) Plot a legend outside of the plotting area in base graphics?
etc.
Now what I understood from the above questions is that I got to set the option xpd = T or xpd = NAto plot legends at the outer margins. However when I try this, it somehow does not work for me ..
par(mfrow=c(1,2),oma=c(0,3,0,0),xpd=TRUE)
plot(c(5,10),col=c("red","blue"),pch=20,cex=2,bty="n",xlab="",ylab="")
barplot(c(5,10),col=c("red","blue"))
mtext(text="My two plots",side=3,cex=2,outer=TRUE,line=-3)
legend("top",legend=c("A", "B"),fill=c("red","blue"),ncol=2,xpd=NA,bty="n") # Option 1
legend(x=0.01,y=11,legend=c("A", "B"),fill=c("red","blue"),ncol=2,xpd=TRUE,bty="n") # Option 2
Now my question is, how does xpd exactly work ? as I am unable to figure out why shouldn't the legend not be placed outside the plot area with xpd=T.
I apologize in advance if some consider this as a duplicate of the above questions !!
Help is much appreciated
Ashwin
Option #1 is likely the route you should take, with xpd=NA. It does not automatically place the legend in the outer margins, but it allows you to place the legend anywhere you want. So, for example, you could use this code to place the legend at the top of the page, approximately centered.
legend(x=-1.6, y=11.6, legend=c("A", "B"), fill=c("red", "blue"), ncol=2, xpd=NA, bty="n")
I chose these x and y values by trial and error. But, you could define a function that overlays a single (invisible) plot on top of the ones you created. Then you can use legend("top", ...). For example
reset <- function() {
par(mfrow=c(1, 1), oma=rep(0, 4), mar=rep(0, 4), new=TRUE)
plot(0:1, 0:1, type="n", xlab="", ylab="", axes=FALSE)
}
reset()
legend("top", legend=c("A", "B"), fill=c("red", "blue"), ncol=2, bty="n")
I also had a hard time to get coordinates on the margins. I think I found a solution, you can specify coordinates for the legend using:
getCoords() function.
Look also to legend_margin function from plotfunctions package.
So combining the solution from Jean V. Adams with one of these functions should get you there.
Hope that works :)

heatmap.2 (gplots) how to change horizontal size of the color key and add a legend

I am producing heatmaps with heatmap.2. I know how to controls many of the parameters but still I have not found a way of making the key of color only wider or putting it as a strip in a side or bottom of the plot.
With keysize it modifies both height and width proportionally.
Also when using ColSideColors I am using legend() to put the color labels, but 'topright' is not at the top-right. I know that this is something about the plot area, margins etc, but I have not found yet a good explanatory text of how heatmap.2 plot is structured and how to positioned things by coordinates and how to deal with oma, mar etc. Depending on the margins, samples, tree depth, etc. the legend could be placed in an open area or overlaps a bit of the heatmap. Any point to good texts for understanding theses issues with R graphics would be truly appreciated.
The coded used is:
df<- data.frame( x1=rnorm(120,mean=rep(1:3,each=4),sd=0.2)
,x2=rnorm(120,mean=rep(1:3,each=4),sd=0.2)
,x3=rnorm(120,mean=rep(1:3,each=4),sd=0.2)
,x4=rnorm(120,mean=rep(1:3,each=4),sd=0.2)
,x5=rnorm(120,mean=rep(1:3,each=4),sd=0.2)
,x6=rnorm(120,mean=rep(1:3,each=4),sd=0.2)
,x7=rnorm(120,mean=rep(1:3,each=4),sd=0.2)
,x8=rnorm(120,mean=rep(1:3,each=4),sd=0.2)
,y1=rnorm(120,mean=rep(c(1,2,1),each=4),sd=0.2)
,y2=rnorm(120,mean=rep(c(1,2,1),each=4),sd=0.2)
,y3=rnorm(120,mean=rep(c(1,2,1),each=4),sd=0.2)
,y4=rnorm(120,mean=rep(c(1,2,1),each=4),sd=0.2)
,y5=rnorm(120,mean=rep(c(1,2,1),each=4),sd=0.2)
,y6=rnorm(120,mean=rep(c(1,2,1),each=4),sd=0.2)
,y7=rnorm(120,mean=rep(c(1,2,1),each=4),sd=0.2)
,y8=rnorm(120,mean=rep(c(1,2,1),each=4),sd=0.2)
)
dataMatrix <- as.matrix(df)[sample(1:120),]
heatmap.2(dataMatrix
, col=rev(brewer.pal(11,"RdBu"))
, density.info="none"
, key=TRUE
, symkey=FALSE
, trace="none"
, cexRow=1
, scale='row'
, margins =c(10,9)
, ColSideColors=c(rep("red", ncol(df)/2), rep("green", ncol(df)/2))
, main="Log2_intensities median centered"
, keysize=0.9)
legend('topright', c("x", "y"),lty=1, col=c("red", "green"), cex=0.8)
There is the way to organise your plot described here: Moving color key in R heatmap.2 (function of gplots package)
When having a ColSideColors, heatmap.2 will draw the plot as
1 ColSideColors
2 heat map
3 ... so on as in the link above
Never tested with the RowSideColors. In the other words you can organise the plot using lmat, lwid, lhei parameters of heatmap.2. this could give you some space for your legend.
You could try adding the 'inset' parameter and playing with the sizes to move the legend further right (first value) and/or further towards the top (second value) e.g:
legend('topright', inset = c(.02,.02), etc...)

R Barplot with one bar - how to plot correctly

I want to plot a regular bar plot in R, but with just one bar. What I don't like is the fact that the bar width gets to be the width of the whole plot. I want the bar to be "thinner", but I don't know how to do it.
Default command is:
barplot(percentage, col=c("brown4"))
where percentage is a fraction. I tried using xlim parameter, but it gets very messy (bar goes completely to the right or left). For example, I tried:
barplot(percentage, col=c("brown4"), xlim=c(0.5,1))
but this stretches the bar even more. I am an R noob.
You may try to play around with the width of the device you plotting to. E.g.:
# plot to a Windows graphic device
windows(height = 10, width = 4)
barplot(0.5)
# plot to PDF
pdf(height = 10, width = 2)
barplot(0.5)
dev.off()
You may also try width together with xlim
barplot(0.5, width = 0.1, xlim = c(0, 1))
The width argument to barplot says:
width optional vector of bar widths. Re-cycled to length the number of bars drawn. Specifying a single value will have no visible effect unless xlim is specified.
So specify both width and xlim. The interaction of these two is not obvious (to me) so you will probably need to play around with them until they look like you want them to.
percentage <- 0.25
barplot(percentage, width=0.2, xlim=c(0,1.2), col="brown4")

Resources