Reduce whitespace in corrplot in R - r

I have a 1 row 2 column plot which I need to insert into a report. When I insert the plot I notice there's a large white area around both correlation plots. Is there an argument in the corrplot function to reduce this? I've read here about using knitr hook functions to crop pdfs but that a bridge too far for my coding capabilities. I've messed around with the par() arguments such as omi and mai but when I export the image to TIFF I still get a huge white space area.
My code so far looks as follows, please note this is hacked together from various sources and dputs to both correlation matrices can be found here and here:
col <- colorRampPalette(c("#BB4444", "#EE9988", "#FFFFFF", "#77AADD", "#4477AA"))
par(mfrow=c(1,2),omi=c(0,0,0,0),mai=c(0.1,0.1,0.1,0.1))
corrplot(LX0088U21A1_24hrCor, method = "color", col = col(200),
type = "upper", order = "original", number.cex = 0.85,
addCoef.col = "black", # Add coefficient of correlation
tl.col = "black", tl.srt = 90,
number.digits = 1,
# Text label color and rotation
# hide correlation coefficient on the principal diagonal
diag = FALSE,
#title = "Intraday Correlation LX0088U21A1",
mar=c(0,0,1,0))
legend(0.5,5,ncol = 1, cex = 1.5,legend = "(a)", box.lwd = 0, box.col = "white")
corrplot(LX0088U09A3_24hrCor, method = "color", col = col(200),
type = "upper", order = "original", number.cex = 0.75,
addCoef.col = "black", # Add coefficient of correlation
tl.col = "black", tl.srt = 90,
number.digits = 1,
# Text label color and rotation
# hide correlation coefficient on the principal diagonal
diag = FALSE,
#title = "Intraday Correlation LX0088U09A3",
mar=c(0,0,1,0))
legend(0.5,5,ncol = 1, cex = 1.5,legend = "(b)", box.lwd = 0, box.col = "white")
Any help is greatly appreciated.

Related

How can I merge two different graphics from corrplot

I generated two different graphics which have different labels, is it possible two merge it?
library(corrplot)
par(mfrow=c(1,2))
col<- colorRampPalette(c("#78CB65","#EBF6DF","#AEDAAA"))
corrplot(as.matrix(samp4), is.corr = FALSE, type = 'lower', method = 'color', addCoef.col = 'black', number.cex=0.50,
col=col(11),
tl.col = "black",
tl.cex = 0.8,
title = "OrthoANI")
corrplot(as.matrix(ddh2), is.corr = FALSE, type = 'upper', method = 'color', addCoef.col = 'black', number.cex=0.50,
col=col(11),
tl.col = "black",
tl.cex = 0.8,
title = "ddh")
I would like two merge it like this
I tried making only one data from ddh2 and samp4, but the problem is that the threshold and values are different, so the label has to be different
thanks

Is it possible to add a grouping label to a corrplot?

Is it possible to add a label that applies to a few variables in a corrplot? This is an example of what I'm after. In that case it is the word "Petals" on the y-axis.
Below is the code to make the rest of the plot.
library(corrplot)
library(Hmisc)
iris_c <- iris[, c(1:4)]
test <- rcorr(as.matrix(iris_c))
corrplot(test$r, type="lower", order= "original",
p.mat = test$P, sig.level = 0.01, insig = "blank",
col= colorRampPalette(c("blue","white", "red"))(100), tl.col = 'black', cl.ratio = 0.8, tl.srt = 45, diag = FALSE, method = "square")
You can use title() to adjust axis labels, which is what you're looking for here. After running your code above, just run:
title(ylab = "Petals")

R corrplot: Plot correlation coefficients along with significance stars?

Using R corrplot, I have not found a solution where the correlation coefficients in the boxes are plotted together with their significances, i.e. 0.84***
Here is the code plotting only the significance stars. How can the correlation coefficients be added there?
M<-cor(mtcars)
res1 <- cor.mtest(mtcars, conf.level = .95)
corrplot(cor(mtcars),
method="square",
type="lower",
p.mat = res1$p,
insig = "label_sig",
sig.level = c(.001, .01, .05),
pch.cex = 0.8,
pch.col = "red",
tl.col="black",
tl.cex=1,
outline=TRUE)
If I add, as by the first answer suggested, addCoef.col = "black",
the text overlays the significance stars so they cannot really be seen anymore:
The position of the significance stars is defined by the place_points function within the corrplot function.
Problem:
If both, the correlation coefficients and the significance level should be displayed, they overlap (I took yellow for the stars since I have some issues with colour vision...).
library(corrplot)
#> corrplot 0.90 loaded
M<-cor(mtcars)
res1 <- cor.mtest(mtcars, conf.level = .95)
corrplot(cor(mtcars),
method="square",
type="lower",
p.mat = res1$p,
insig = "label_sig",
sig.level = c(.001, .01, .05),
pch.cex = 0.8,
pch.col = "yellow",
tl.col="black",
tl.cex=1,
addCoef.col = "black",
tl.pos="n",
outline=TRUE)
Created on 2021-10-13 by the reprex package (v2.0.1)
Quick and temporary (you have to re-do this step everytime you newly loaded the corrplot package) solution:
Change the place_points function within the corrplot function. To do so, run:
trace(corrplot, edit=TRUE)
Then replace on line 443
place_points = function(sig.locs, point) {
text(pos.pNew[, 1][sig.locs], pos.pNew[, 2][sig.locs],
labels = point, col = pch.col, cex = pch.cex,
lwd = 2)
with:
# adjust text(X,Y ...) according to your needs, here +0.25 is added to the Y-position
place_points = function(sig.locs, point) {
text(pos.pNew[, 1][sig.locs], (pos.pNew[, 2][sig.locs])+0.25,
labels = point, col = pch.col, cex = pch.cex,
lwd = 2)
and then hit the "Save" button.
Result:
library(corrplot)
#> corrplot 0.90 loaded
#change the corrplot function as described above
trace(corrplot, edit=TRUE)
#> Tracing function "corrplot" in package "corrplot"
#> [1] "corrplot"
M<-cor(mtcars)
res1 <- cor.mtest(mtcars, conf.level = .95)
corrplot(cor(mtcars),
method="square",
type="lower",
p.mat = res1$p,
insig = "label_sig",
sig.level = c(.001, .01, .05),
pch.cex = 0.8,
pch.col = "yellow",
tl.col="black",
tl.cex=1,
addCoef.col = "black",
tl.pos="n",
outline=TRUE)
Created on 2021-10-13 by the reprex package (v2.0.1)
You only have to add the option addCoef.col = "black" to corrplot
To fix the colour clash of the stars I adapted this approach to have bigger stars with a white colour. I use this with the following colours and layout. I can't avoid the overlap but it is legible enough for me to work with:
cex.before <- par("cex")
par(cex = 0.7)
col <- colorRampPalette(c("#BB4444", "#EE9988", "#FFFFFF", "#77AADD", "#4477AA"))
corrplot::corrplot(cor(mtcars),
method="color",
col=col(200),
type="lower",
# Combine with significance
p.mat = res1$p,
insig = "label_sig",
sig.level = c(.001, .01, .05),
pch.cex = 3, # Increase size of stars
pch.col = "white", # Colour of stars
# hide correlation coefficient on the principal diagonal
diag=FALSE,
addCoef.col = "black", # Add coefficient of correlation
tl.col="black", tl.srt=45, #Text label color and rotation
tl.cex = 1/par("cex"), cl.cex = 1/par("cex") #Reduce text size of coefficients
)
par(cex = cex.before)

contourplot color and labels options in Lattice for R

I am quite new to Lattice and I am stuck with some possibly basic coding. I am using shapefiles and geoTIFFS to produce maps of animals distribution and in particular I have:
1 x point shapefile
2 x geoTIFF
1 x polygon shapefile
I am overlapping a levelplot of one of the geoTIFF (UD generated with adehabitatHR) with a contourplot of the same geoTIFF at specific intervals (percentile values), a contourplot of the second geoTIFF (depth raster from ETOPO2) for three specific values (-200, -1000 and -2000), the point shapefile (animal locations) and the polygon shapefile (land). All works fine but I need to change the font size of contour plot labels, their length (i.e. from 0.12315 to 0.123) and positioning for all the contourplots. For the depth contourplot I would like to change the style of each line in something like "continous line", "dashed line" and "point line", and for the contourplot of the UD I would like to change the color of each line using a yellow to red palette.
As far as I understand, I should use panel functions to implement these changes (e.g. Controlling z labels in contourplot) but i am not quite sure how to do it. Part of my code to generate the "plot":
aa <-
quantile(
UD_raster,
probs = c(0.25, 0.75),
type = 8,
names = TRUE
)
my.at <- c(aa[1], aa[2])
depth<-c(-100, -200, -2000)
levelplot(
UD_raster,
xlab = "",
ylab = "",
margin = FALSE,
contour = FALSE,
col.regions = viridis(100),
main = "A",
maxpixels = 2e5
) + layer(sp.polygons(Land, fill = "grey40", col = NA)) + layer(sp.points(locations, pts = 2, col = "red")) + contourplot(
UD_raster,
at = my.at,
labels = TRUE,
margin = FALSE
) + contourplot(
ETOPO2,
at = depth,
labels = TRUE,
margin = FALSE
)
A simplified image, with no UD layer and no point shapefile can be found here and as you can see it is pretty messy. Thanks for your help.
So far for the ETOPO2 countourplot I have solved by eliminating the labels and adding the argument lty to style the line. Because I can't figure out how to use lty with different values for each single line in my contour, I have replicated the contourplot function three times on the same surface, one for each contour I am interested into (this was easy because I only need three contours).
For the position, font and font size of the labels of the remaining contourplot I have used
labels = list(cex = 0.8, "verdana"),
label.style = "flat"
To "shorten" the length of the labels I have used the function round where I specify to which decimal digit to round number.
So now my new code looks like:
aa <-
quantile(
UD_raster,
probs = c(0.25, 0.75),
type = 8,
names = TRUE
)
my.at <- c(aa[1], aa[2])
my.at <- round(my.at, 3)
levelplot(
UD_raster,
xlab = "",
ylab = "",
margin = FALSE,
contour = FALSE,
col.regions = viridis(100),
main = "A",
maxpixels = 2e5
) + layer(sp.polygons(Land, fill = "grey40", col = NA)) + layer(sp.points(positions, pts = 2, col = "red")) + contourplot(
UD_raster,
at = my.at,
labels = list(cex = 0.8, "verdana"),
label.style = "flat",
margin = FALSE
) + contourplot(
ETOPO2,
at = -200,
labels = FALSE,
margin = FALSE,
lty = 1,
pretty = TRUE
) + contourplot(
ETOPO2,
at = -1000,
labels = FALSE,
margin = FALSE,
lty = 2,
pretty = TRUE
) + contourplot(
ETOPO2,
at = -2000,
labels = FALSE,
margin = FALSE,
lty = 3,
pretty = TRUE
)
As one could expect, it takes a bit longer to produce the plot. Still no idea on how to change the colors of the UD contourplot.

Neat formatting for Venn diagram in R with unbalanced group sizes

I'm using the VennDiagram R package to try to generate a neatly formatted diagram comparing two groups. I have successfully used this package in the past to compare relatively similarly-sized groups. However, now I'm comparing groups that have significantly different sizes (# of unique elements in the first group is ~3,600, # of unique elements in the second group is ~60, and # of overlapping elements is ~80).
The appearance of my current Venn diagram is that the group with the larger # of elements has this value displayed within its circle, but the labels for the intersection of the two groups and the unique elements in the second group are too large to be included in those regions of the diagram, so instead, they are displayed outside of the diagram with a line connecting them to the associated region. I don't like the appearance of this, and would like to reduce the size of all 3 labels so that they can be displayed within their respective regions of the diagram. However, after having reviewed the associated documentation/examples and publication (Chen & Boutros 2011), I'm still not clear about how to do this. (For example, I see parameters that permit the specification of font size of the figure title and subtitle, but I don't see where the labels' font size can be specified...)
I have attempted workarounds such as trying to make the labels invisible so that I can manually add them in a separate application, but this doesn't seem to be an option...
Any suggestions for how I can reduce the font size of my labels and specify that these labels appear within the regions of the diagram rather than outside of the diagram, will be appreciated. Thanks!
Update: As requested below, I am providing my example code:
library(VennDiagram);
library(grid);
Data <- read.csv('ExampleDataset_VennDiagram.csv')
Dataset1 <- Data[,1]
Dataset2 <- Data[,2]
MyVennDiagram <- venn.diagram(
x = list(
A = Dataset1,
B = Dataset2
),
main = "",
main.cex = NULL,
filename = NULL,
lwd = 2,
fill = c("blue", "green"),
alpha = 0.75,
label.col = "black",
cex=c(2,2,2),
fontfamily = "sansserif",
fontface = "bold",
cat.col = c("blue", "green"),
cat.cex = 0,
cat.fontfamily = "serif",
cat.fontface = "bold",
cat.dist = c(0.05, 0.05),
cat.pos = c(-20, 14),
);
grid.newpage()
grid.draw(MyVennDiagram)
Update: Based on missuse's suggestion below, using ext.text = FALSE works perfectly!
Thanks to everyone who contributed to this thread.
The eulerr library appears to generate nice-looking diagrams, and will definitely be a resource I use in the future -- thanks for sharing.
A possible solution to this is to avoid using euler diagrams.
To illustrate your problem here is some data:
A = sample(1:1000, 500, replace = T)
B = sample(1:10000, 50)
Here is the diagram obtained by
library(VennDiagram);
library(grid)
MyVennDiagram = venn.diagram(
x = list(
A = A,
B = B
),
main = "",
main.cex = NULL,
filename = NULL,
lwd = 2,
fill = c("cornflowerblue", "pink"),
alpha = 0.75,
label.col = "black",
cex=c(2,2,2),
fontface = "plain",
cat.col = c("cornflowerblue", "pink"),
cat.cex = 0,
cat.fontfamily = "serif",
cat.fontface = "plain",
cat.dist = c(0.05, 0.05),
cat.pos = c(-20, 14),
cat.default.pos = "text",
)
grid.newpage()
grid.draw(MyVennDiagram)
by avoiding scaling of the circles with scaled = FALSE
MyVennDiagram = venn.diagram(
x = list(
A = A,
B = B
),
main = "",
main.cex = NULL,
filename = NULL,
lwd = 2,
fill = c("cornflowerblue", "pink"),
alpha = 0.75,
label.col = "black",
cex=c(2,2,2),
fontface = "plain",
cat.col = c("cornflowerblue", "pink"),
cat.cex = 0,
cat.fontfamily = "serif",
cat.fontface = "plain",
cat.dist = c(0.05, 0.05),
cat.pos = c(-20, 14),
cat.default.pos = "text",
scaled = FALSE
)
grid.newpage()
grid.draw(MyVennDiagram)
As per user20650 suggestion the best option is to use ext.text=FALSE in the original call:
Also check library(eulerr) it accepts a bit different input, here is an illustration:
library(eulerr)
library(tidyverse)
data.frame(dat = unique(c(A, B))) %>%
mutate(A = dat %in% A,
B = dat %in% B) %>%
select(A, B) %>%
euler() %>%
eulerr:::plot.euler(counts = T)
As per user20650 comment acceptable input is also:
plot(euler(setNames(list(unique(A),unique(B)), c("A", "B"))), counts=TRUE)

Resources