R corrplot crops bottom axis label - r

When I use corrplot::corrplot() to plot a correlation matrix, the bottom label (1) on the y-axis is half cut off, because the bottom of the plot is at the very bottom of the plotting area, and the 1 is centered on the bottom axis. I'd like to use the plot for publication. How do I give a bit more space at the bottom so that this bottom y-axis label is not cut off?
Thanks in advance for the plot and for help with the above. This is a very nice plot except for the above issue.
Larry Hunsicker

Although no reproducible example was provided, we can show here a generic example of how to deal with this. Here a corrplot, in which the bottom label on the color scale is cut off:
M = cor(mtcars)
corrplot(M)
We can solve this by increasing the margin size using mar parameter in corrplot, to give enough space around the figure for labels. We also need to specify par(xpd=TRUE) to allow labels to be printed within the margin areas. Note that the behaviour of corrplot with respect to graphical parameters is somewhat inconsistent. Some parameters need to be specified in a par statement preceding corrplot, otherwise they are not respected if specified within the corrplot statement itself. Other parameters only work if they are specified within the corrplot statement. ?corrplot will tell you which graphical parameters get over-ridden by default values if not specified in corrplot - these are the ones that will have to be specified inside corrplot.
par(xpd=TRUE)
corrplot(M, mar = c(2, 0, 1, 0))

Related

Save Filled Area of Polygon in R

I am plotting polygon in R and saving it.Problem, I am facing is that the whole plot is saved as png file but I want to save only the filled area in the polygon.
Is there a way for that ?
x<-c(0.000000000,0.010986328,0.006351471,-0.004634857)
y<-c(0.000000000,0.007232612,0.012841203,0.006199415)
file_name = paste("~/Downloads/Plot", ".png", sep="")
png(file_name,width=1280,height = 720,units="px",res=200)
plot(x,y,axes=FALSE,ylab='',xlab='')+polygon(x,y,col="#FF0000FF")
dev.off()
If you're drawing a monofigure plot (which is the default), then I believe there are three possible sources of spacing that can cause a plot element to not extend to the edges of the graphics device:
1: data coordinate limits that are larger than the extent of the plot element.
2: "internal spacing", which is best thought of as an expansion of the plot area that sits inside the margins.
3: margins. This is normally where axes, ticks, tick labels, axis labels, titles, and sometimes legends are drawn.
All of these sources of spacing can be eliminated with the following customizations:
1: set the xlim and ylim graphics parameters to perfectly fit the target plot element.
2: set xaxs='i',yaxs='i', which can be done with either a preemptive par() call or on the initial plot() call.
3: zero the margins with mar=c(0,0,0,0). This must be done with par() prior to the initial plot() call.
Example:
## generate data
pts <- data.frame(x=c(0.2,0.4,0.9,0.7),y=c(0.5,0.4,0.5,0.6));
## precompute plot parameters
xlim <- range(pts$x);
ylim <- range(pts$y);
## draw plot
par(mar=c(0,0,0,0));
plot(NA,xlim=xlim,ylim=ylim,xaxs='i',yaxs='i',axes=F,ann=F);
points(pts$x,pts$y,pch=21L);
polygon(pts$x,pts$y,col='red',pch=21L);
Multifigure plots can incur one additional source of spacing, namely outer margins, but it looks like that doesn't concern you for this problem. In any case, I'm pretty sure outer margins always default to zero anyway.
See par() for the relevant documentation.
It looks like I misunderstood the question. What you want is a transparent background, which is different from simply fitting the image size to the plot element.
You can use the png() function to set the background to be transparent by passing bg='transparent', as explained on the documentation page.
For example, here's my fitted image saved with a transparent background:
Note that not all image viewers will correctly detect and/or clearly depict the transparency of the background. I would highly recommend GIMP, which is basically a free Photoshop knockoff, albeit markedly lighter in features. GIMP depicts transparent regions as a kind of checkerboard of grey squares, which looks like this:

R: Resize 2 x 2 plot output

I have 4 maps to plot and would like to plot them in a single plot area so I use the code
par(mfrow=c(2,2), bty="n")
However, the result is lots of useless blank space around my maps (which become also too small).
The image below shows how much space is taken up by each map (blue rectangle) relative to the plot area.
Is it possible to go from the current situation (cf. 2 x 2 plot fit) to the desired situation (cf. Desired better fit)?
I tried changing width and height parameters (dev.new(width=x, height=y)) but with no luck.
I would suggest using the layout function, though using par is fine too. I give a simple example below, in which you can adjust par(mar=c()) settings for each individual plot, bringing them closer together. layout allows you to adjust relative heights of plots with the height parameter, which might be useful when trying to get panel plots to be all the same height, when you have the x-axis only for the lower tier plots.
y=runif(100)
x=runif(100)
layout(matrix(c(1,2,3,4), byrow=TRUE, ncol=2,nrow=2))
par(mar=c(3,3,0.5,0.5))
plot(y,x, xaxt='n')
par(mar=c(3,3,0.5,0.5))
plot(y,x, xaxt='n')
par(mar=c(3,3,0.5,0.5))
plot(y,x)
par(mar=c(3,3,0.5,0.5))
plot(y,x, yaxt='n')

qcc pareto.chart: bars are squashed - how to adjust the y axis?

I've produced a pareto.chart using the QCC package in R. In the default plot, the Y axis is scaled too large & for that reason the bars are too small. Most of the plot is wasted to empty white space. I assume this is a result of the long tail (right-skew) in the data ?
How is it possible to re-scale the Y-Axis so that the bars of the chart will be taller and more prominent (and differences between the bars more visible) ?
This is my first question and I can't post images yet. Please follow the link to an illustration of the problem:
https://www.dropbox.com/s/t8bwhmoxmwl1aic/pareto-axis.png
Thanks!
Keith
If you type help(pareto.chart) you will see there is an option ylim to specify the limits if the y-axis. If you provide some sample data I could try it out.
Otherwise, try including in your function call
pareto.chart(..., ylim=c(0,10000))
and see if that rescales your y-axis.

R-Heatmap.2 Remove huge space left between title and actual heatmap after disabling column dendrogram

I am plotting a 759*12 double matrix twoway.expr.005 using heatmap.2()
library(gplots)
dist2 <- function(x, ...){as.dist(1-cor(t(x), method="pearson"))}
heatmap.2(x=twoway.expr.005,col=bluered(75), main="Heatmap:759 genes\nTwosided Pval<0.05",tracecol= NULL, cexCol=0.8,cexRow=0.5,labCol=labs,distfun=dist2,scale="row",key=F,dendrogram='row',Colv=F)
But because I am setting dendrogram='row' (column dendrogram turned off) and key=F, my heatmap is leaving a huge whitespace between the title of the plot and the actual plot when I try to save it as a PDF.
I tried setting lhei as per the suggestion. I have used lhei=c(1,4) but it still shows me a LOT of space between the title and the plot:
heatmap.2(x=twoway.expr.005,col=bluered(75), main="Heatmap:759 genes\nTwosided Pval<0.05",tracecol= NULL, cexCol=0.8,cexRow=0.5,labCol=labs,distfun=dist2,scale="row",key=F,dendrogram='row',Colv=F,lhei=c(1,4))
Setting lhei=c(1,5) completely throws away the title:
heatmap.2(x=twoway.expr.005,col=bluered(75), main="Heatmap:759 genes\nTwosided Pval<0.05",tracecol= NULL, cexCol=0.8,cexRow=0.5,labCol=labs,distfun=dist2,scale="row",key=F,dendrogram='row',Colv=F,lhei=c(1,5))
I think Heatmap.2 is designed in such a way that the title is always placed above the column dendrogram. So if the column dendrogram is disabled, it leaves an empty space but the position of the title is still above the "disabled" column dendrogram. Is there any way to hack the code so that the title is not placed above the column dendrogram (which may be the solution to this problem)? What else can be done to remove the space between the title & the actual plot?
The lhei argument to heatmap.2 can be used to fix this. It takes a vector of length 2 that represent the relative heights of the rows of the layout of the plot.
Heatmap.2 divides the plot area in to a four blocks using the layout function. Essentially, you want to control the height of the first one relative to the second. To do this, set lhei to something like c(1, 10). This will make the bottom row, which is where the heatmap is, 10 times as tall as the top row which normally has the dendogram for the rows and the color key.
The documentation for layout has more details.
You can use title instead of setting main from within heatmap.2
title("Your title", line= -2).
You can adjust the distance by changing the value of line.
Doesn't help with left to right centering, which is still off without both dendrograms, but you'll be closer.

`cex.lab` Axis label exceeds plot region

I want to create a plot with magnified axis labels using cex.lab=2 but the label exceeds the plot region. Any ideas on how can I solve this?
Here is an example of the issue:
plot(1:10,1:10,ylab=~gamma,cex.lab=2)
Which produces a graph with a beheaded $\gamma$
I have done some search before asking the question both in google and in this site but my google foo betrayed me this time.
You have to set larger margin of your plot window. That can be achieved with function par() and argument mar=. Numbers correspond to margin starting with bottom, then left margin, upper and right margin.
par(mar=c(5,5,1,1))
plot(1:10,1:10,ylab=~gamma,cex.lab=2)

Resources