Add hierarchical clustering to correlation plot? - r

I've generated correlation heatmaps using the following code:
corrplot(data, method = "color",
type = "lower", order = "hclust", col=colorRampPalette(c("blue","white","red"))(400), tl.col = 'black',
cl.ratio = 0.2, tl.srt = 45)
I also have hierarchical clustering results from pvclust and would like to add those results to the correlation plot so that each cluster is grouped together by a box on the correlation plot. Does anyone know how to do this? Thanks!!

Related

Adding a 95% confidence interval to NMDS plot

I am trying to plot an NMDS plot of species community composition data with ellipses which represent 95% confidence intervals. I generated the data for my NMDS plot using metaMDS and successfully have ordinations generated using the basic plot functions in R (see code below). However, I am struggling to get my data to plot successfully using ggplot2 and this is the only way I have seen 95% CIs plotted on NMDS plots. I am hoping someone is able to help me correct my code so the ellipses show 95% CIs, or could point me in the right direction for achieving this using other methods?
My basic code for plotting my NMDS plot:
orditorp(dung.families.mds, display = "sites", labels = F, pch = c(16, 8, 17, 18) [as.numeric(group.variables$Heating)], col = c("green", "blue", "orange", "black") [as.numeric(group.variables$Dungfauna)], cex = 1.3)
ordiellipse(dung.families.mds, groups = group.variables$Dungfauna, draw = "polygon", lty = 1, col = "grey90")
legend("topleft", "stress = 0.1329627", bty = "n", cex = 1)
My ordination:
I realize this question is old, but I found this post useful for plotting confidence ellipses during my work, and maybe it will help you. Plotting ordiellipse function from vegan package onto NMDS plot created in ggplot2
Edit: Below I have copied the code from the second part of Didzis Elferts's answer on the link above.
Where "sol" is the metaMDS object:
First, make NMDS data frame with group column.
NMDS = data.frame(MDS1 = sol$points[,1], MDS2 = >sol$points[,2],group=MyMeta$amt)
Next, save result of function ordiellipse() as some object.
ord<-ordiellipse(sol, MyMeta$amt, display = "sites", >kind = "se", conf = 0.95, label = T)
Data frame df_ell contains values to show ellipses. It is calculated again with function veganCovEllipse which is hidden in vegan package. This function is applied to each level of NMDS (group) and now it uses arguments stored in ord object - cov, center and scale of each level.
df_ell <- data.frame()
for(g in levels(NMDS$group)){
df_ell <- rbind(df_ell, cbind(as.data.frame(with(NMDS[NMDS$group==g,],
veganCovEllipse(ord[[g]]$cov,ord[[g]]$center,ord[[g]]$scale)))
,group=g))
}
Plotting is done the same way as in previous example. As for the calculating of coordinates for elipses object of ordiellipse() is used, this solution will work with different parameters you provide for this function.
ggplot(data = NMDS, aes(MDS1, MDS2)) + geom_point(aes(color = group)) +
geom_path(data=df_ell, aes(x=NMDS1, y=NMDS2,colour=group), size=1, linetype=2)

Rstudio - corrplot highlight ony high correlation values in the plot

I'm trying to highlight only correlation values <-.7 and >0.7 in my correlation plot, but I'm getting trouble on it.
I have this data like this
Parn_Spt_comp_2
And I did a Pearson correlation with this code:
par_spt.pearson2 <-cor(Parn_Spt_comp_2[,3:14], method = "pearson")
par_spt.pearson2
After I plotted the values with the code below:
corrplot(par_spt.pearson2, method="color", addCoef.col = "black" ,type="lower", sig.level = 0.05,
number.cex = 1.2, tl.col = "black",tl.cex = 1.25, number.font = 1, tl.srt = 0, tl.offset = 0.3, pch=19, addgrid.col="black",col=colorRampPalette(c("white","coral3"))(10), cl.cex=1.25, mar = c(0,0,0,0))
And display it:
But I would like to show only correlation values in the interval mentioned above, and the values >-0.7 and <0.7 do not appear in the plot. I tried many things, but it still not working. I would be grateful if you help me.
Thank you in advance!

How can I display the p-value in a correlation Plot in the right size?

I have plotted a correlation matrix resulting from the function "rcorr" with "corrplot". Everything comes out fine so far, except one thing. I would like to display the p-value for either all, or for at least the cells, where the p-value is higher than the previously defined significance level of 0.005. The "insig" function does display the the p-values for the cells with p<sig, but I can't change the size of the plotted numbers. I tried to use any of the given arguments of the "corrplot" function, but it has no effect of the size of the p-value text. Does anyone know how I could change the size and how I can control the position of the p-values in each cell?
Here is the code I am using:
my_data <- FS[,2:27]
# Mark the insignificant coefficients according to the specified p-value significance level
library(corrplot)
library(Hmisc)
FScor <- rcorr(as.matrix(my_data[,1:25],method = c("spearman")))
M_Konz <- FScor$r
p_matK <- FScor$P
CorrKonz <- corrplot(M_Konz, type = "upper", order = "hclust", insig = "p-value",
p.mat = p_matK, sig.level = 0.005,
tl.cex = 0.7,tl.col = "black",
title = "")
Unfortunately corrplot does not give you direct control over the size of the p-values, but you can get it by setting the graphics parameter directly. You did not provide reproducible data so I'll use an example from the corrplot vignette. Your code has a problem with the rcorr line since method= is not an argument of as.matrix or rcorr.
M <- rcorr(as.matrix(mtcars), type="spearman")
Mr <- M$r
Mp <- M$P
oldp <- par(cex=.75)
Mcorr <- corrplot(Mr, type = "upper", order = "hclust", insig = "p-value",
p.mat = Mp, sig.level = 0.005, tl.cex = 1.25, tl.col = "black",
cl.cex=1.25, title = "")
par(oldp)
The mtcars data frame is included with R. We set the graphical parameter cex to .75 (default is 1) and then increase the size of the variable labels with tl.cex and the legend labels with cl.cex. After drawing the plot we reset the default graphics parameters.

Add significant correlations to corrplot graph in R

For my data I made a correlation plot. I want to visualize the correlations with the color method. Additionaly I would like to add the correlation coefficient, however, I only want to visualize them for the significant correlations, in all other cases I would like to show a cross.
This is what I have done:
GM_young <- replicate(6, rnorm(11))
colnames(GM_young2) <- c('PA_rest','FL-rest','MW_rest','PA_con','FL_con', 'MW_con')
corrGM_young<-cor(GM_young) # gives you a matrix with correlations
p.mat <- cor.mtest(GM_young) # matrix of the p-value of the correlation
# make corrplot
corrplot(corrGM_young, method = "color", type="lower",order = "hclust", p.mat = p.mat, sig.level = 0.01, diag=FALSE , tl.col="black",tl.cex = 0.8, cl.cex = 0.8,cl.ratio=0.3,tl.srt = 45, title="Gastrocnemius Young", mar=c(0,0,2,0), addCoef.col = TRUE)
However, now I get the coefficients for all the correlations. I only want to visualize the 0.74. The other boxes I want to visualize using the colors and the cross as they are not significant.
Can someone help me with this? Thank you

R corrplot: weird error with significance test

I am desperately trying to solve an issue with R corrplot. I have a 10 * 22 matrix that I would like to visualize using corrplot, but adding significance level just produces crosses outside of the boarder of the visual. I could produce correlation plots without significant tests using the following script:
corrplot(matrix, method = "circle",tl.cex=0.4, tl.col = "black", col = color(10))
However, the following script produces crosses outside of the border of the visual:
res1 <- cor.mtest(corredata, conf.level = .95)
corrplot(matrix, p.mat = res1$p, method = "circle", tl.cex=0.6, tl.col = "black",
col = color(10), sig.level = 0.05, insig = "pch")
Is there something wrong with my code?

Resources