Hot questions for Using Ggplot2 in ggcorrplot

Top 10 R Open Source / Ggplot2 / ggcorrplot

Question:

I want to reduce the size of markers in ggcorrplot, and reduce the space between text and the plot.

library(ggcorrplot)
data(mtcars)

corr <- round(cor(mtcars), 1)
ggcorrplot(corr,sig.level=0.05 ,lab_size = 4.5, p.mat = NULL, insig = c("pch", "blank"), pch = 1, pch.col = "black", pch.cex =1,
  tl.cex = 14)

Answer:

You can adjust the distance between the axis text and the plot with theme elements. With geom_tile in a standard ggplot, you can adjust the height and width of the tiles. ggcorrplot doesn't seem to accept that adjustment. There may be a way I'm not aware of; I haven't used the package before. My hacky workaround is to just overlay a white grid to create space between the tiles:

ggcorrplot(corr, sig.level=0.05, lab_size = 4.5, p.mat = NULL, 
           insig = c("pch", "blank"), pch = 1, pch.col = "black", pch.cex =1,
           tl.cex = 14) +
  theme(axis.text.x = element_text(margin=margin(-2,0,0,0)),  # Order: top, right, bottom, left
        axis.text.y = element_text(margin=margin(0,-2,0,0))) +
  geom_vline(xintercept=1:ncol(mtcars)-0.5, colour="white", size=2) +
  geom_hline(yintercept=1:ncol(mtcars)-0.5, colour="white", size=2) 

This type of plot is also not that difficult to make as a regular ggplot, and then you'll have full control over the plot elements:

library(reshape2)   

ggplot(melt(corr), aes(Var1, Var2, fill=value)) +
  geom_tile(height=0.8, width=0.8) +
  scale_fill_gradient2(low="blue", mid="white", high="red") +
  theme_minimal() +
  coord_equal() +
  labs(x="",y="",fill="Corr") +
  theme(axis.text.x=element_text(size=13, angle=45, vjust=1, hjust=1, 
                                 margin=margin(-3,0,0,0)),
        axis.text.y=element_text(size=13, margin=margin(0,-3,0,0)),
        panel.grid.major=element_blank()) 

Another hack with ggcorrplot is to cover up and then redraw the tiles using geom_tile so that we can access the height and width arguments:

ggcorrplot(corr, sig.level=0.05, lab_size = 4.5, p.mat = NULL,
           insig = c("pch", "blank"), pch = 1, pch.col = "black", pch.cex =1,
           tl.cex = 14) +
  theme(axis.text.x = element_text(margin=margin(-2,0,0,0)),
        axis.text.y = element_text(margin=margin(0,-2,0,0)),
        panel.grid.minor = element_line(size=10)) + 
  geom_tile(fill="white") +
  geom_tile(height=0.8, width=0.8)

Question:

I've created a corrplot using ggcorrplot with the following code:

library(ggcorrplot)

corCN <- subset(csfBL, BL_Diaggroups==1, 
                select=c("log_ab38", "log_ab40", "log_ab42", "ABratio", "log_YKL", "logNgcomplete", "log_NFL"))

colnames(corCN) <- c("Aß 1-38","Aß 1-40", "Aß 1-42", "Aß 42/40", "YKL-40", "Ng", "NFL")

corrCN <- cor(corCN, method=c("spearman"), use="complete.obs")
p.matCN <- cor_pmat(corCN, method=c("spearman"), use="complete.obs")

CNcorrplot <- ggcorrplot(corrCN, type="lower", lab=TRUE, ggtheme = ggplot2::theme_classic,
                          p.mat=p.matCN, insig ="blank")

CNcorrplot2 <- CNcorrplot + ggtitle("CN") + theme(plot.title = element_text(hjust = 0.5)) 

And the plot looks like this.

The only thing I want to change is that the insignificant values have "NS" in them or are totally blank (so no 0 displayed). Actually I would need some kind of code saying:

if insig=TRUE, lab=FALSE

Answer:

I created a modified version of ggcorrplot called myggcorrplot with the new lab.notsig option (the label for not significant correlations). Download the file here and save it in your working directory as myggcorrplot.r. Then, run the following code:

library(ggcorrplot)  
library(mvtnorm)
# A toy dataset
set.seed(1)
n <- 100
p <- 7
A <- matrix(runif(p^2)*2-1, ncol=p) 
Sigma <- cov2cor(t(A) %*% A)
corCN <- data.frame(rmvnorm(n, mean=rep(0,p), sigma=Sigma))
colnames(corCN) <- c("Aß 1-38","Aß 1-40", "Aß 1-42", "Aß 42/40", "YKL-40", "Ng", "NFL")

corrCN <- cor(corCN, method=c("spearman"), use="complete.obs")
p.matCN <- cor_pmat(corCN, method=c("spearman"), use="complete.obs")

# Load the myggcorrplot function
source("myggcorrplot.r")
# Set in the lab.notsig option the label for not significant correlations
CNcorrplot <- myggcorrplot(corrCN, type="lower", lab=TRUE, 
              ggtheme = ggplot2::theme_classic,
              p.mat=p.matCN, insig ="blank", lab.notsig="NS")

CNcorrplot2 <- CNcorrplot + ggtitle("CN") + 
                   theme(plot.title = element_text(hjust = 0.5)) 
CNcorrplot2

Question:

I am using ggcorrplot to build a correlation matrix, but the output reorders the columns in a way that I don't want. How do I reorder the columns?

For the purposes of this example, I'll be using the 'mtcars' dataset found in R. After the final output is produced, I need to reorder the columns, because it keeps reordering to a format that I don't want.

Note: Code is provided from the website as follows: http://www.sthda.com/english/wiki/ggplot2-quick-correlation-matrix-heatmap-r-software-and-data-visualization

library(ggcorrplot)

mydata <- mtcars

#correlation matrix
cormat <- round(cor(mydata),2)

library(reshape2)
melted_cormat <- melt(cormat)
head(melted_cormat)

library(ggplot2)
ggplot(data = melted_cormat, aes(x=Var1, y=Var2, fill=value)) + 
  geom_tile()

# Get upper triangle of the correlation matrix
get_upper_tri <- function(cormat){
  cormat[lower.tri(cormat)]<- NA
  return(cormat)
}

upper_tri <- get_upper_tri(cormat)

# Melt the correlation matrix
library(reshape2)
melted_cormat <- melt(upper_tri, na.rm = TRUE)
# Heatmap
library(ggplot2)
ggplot(data = melted_cormat, aes(Var2, Var1, fill = value))+
  geom_tile(color = "white")+
  scale_fill_gradient2(low = "blue", high = "red", mid = "white", 
                       midpoint = 0, limit = c(-1,1), space = "Lab", 
                       name="Pearson\nCorrelation") +
  theme_minimal()+ 
  theme(axis.text.x = element_text(angle = 45, vjust = 1, 
                                   size = 12, hjust = 1))+
  coord_fixed()

This is the correlation matrix that I want, but I need to reorder the columns into an order different than the one shown.

Any help would be great. Thanks everyone!


Answer:

Let's assume we want the x-axis to be ordered based on its names (labels) alphabetically and y-axis the same but in reverse order. The following code works (rest of the code will be the same).

ggplot(data = melted_cormat, aes(reorder(Var2,  -desc(as.character(Var2))), 
                                 reorder(Var1,  desc(as.character(Var1))), fill = value))+
  geom_tile(color = "white")+
  scale_fill_gradient2(low = "blue", high = "red", mid = "white", 
                       midpoint = 0, limit = c(-1,1), space = "Lab", 
                       name="Pearson\nCorrelation") +
  theme_minimal()+ 
  theme(axis.text.x = element_text(angle = 45, vjust = 1, 
                                   size = 12, hjust = 1))+
  coord_fixed()

Question:

I am trying to use the ggcorrplot package in R to make a correlation matrix. However, the diagonal in the resulting plot is different than the input correlation matrix.

For example, here is a basic plot using the package:

library(ggcorrplot)
data(mtcars)
corr.mat <- cor(mtcars[, c("mpg", "disp", "hp", "drat", "wt", "carb")])
ggcorrplot(corr.mat)

This plot has a diagonal going up the matrix from left to right. Instead, I want the resulting plot to look exactly like the correlation matrix that I used as an input:

print(corr.mat)

That is, the correlation for the mpg variable should be at position [1,1] in the matrix.


Answer:

You can do it by reversing the rows or columns of corr.mat:

ggcorrplot(corr.mat[,6:1])

gives

Question:

I have a dataset (randomized data, for this example) that I'm working with, and I need to only display a certain part of the matrix. Is there a way to keep the same parameters specified in the code, but to only display the part of the matrix that I need?

The code that I will show below generates the matrix shown here:

I like the size of everything, particularly speaking of the Pearson's correlation value legend on the right. However, I need to only display everything that is in the red box in the following picture:

Here is the code that I'm working with:

Data:

sen <- structure(list(STAGE3 = c(0.876917687075825, 0.412349699244156, 
0.0276263864194565, 0.715878164202211, 0.113324379019157, 0.697977125373629, 
0.495239700082692, 0.433931430983351, 0.969730865766935, 0.325140127448327, 
0.571408271249494, 0.795998354603895, 0.677190885671071, 0.725950370073618, 
0.773033245228681, 0.124492827494149, 0.392125485039593, 0.293970448598711, 
0.574176683397314, 0.439175490268417, 0.187274945963726, 0.114361533064052, 
0.984810340190489, 0.094824786617737, 0.365076362766766, 0.484529293489265, 
0.0248352738577235, 0.370080530701056, 0.916058724203794, 0.698768788065752, 
0.0486805863644058, 0.417384720565167, 0.575510045429799, 0.306442633779882, 
0.352619466947156, 0.0468312121293604, 0.719674300918072, 0.245979619378491, 
0.497903453826379, 0.815929672015352, 0.579266045428163, 0.493374660814721, 
0.981395698013249, 0.501428573306618, 0.232710091203988, 0.295487027432436, 
0.0358089940602796, 0.122221799715662, 0.475075779687791, 0.64716894430911, 
0.411728227914661, 0.189801935164384, 0.995784139092795, 0.338669053292779, 
0.888464605534105, 0.727167494736264, 0.359872858562472, 0.501785825883555, 
0.131682859300131, 0.316615799479412), STAGE2 = c(0.731671739637962, 
0.0349659624268288, 0.250468754401736, 0.692725665227725, 0.63127309683438, 
0.267791520174364, 0.683510378409481, 0.0930250575768417, 0.0469303813447592, 
0.664296422188774, 0.104791391299402, 0.769846767284999, 0.6757286415883, 
0.752123872001356, 0.90542496106528, 0.495894318363979, 0.693023609703169, 
0.268140720274296, 0.715499146536921, 0.28736047756967, 0.0734999857226017, 
0.220321023927838, 0.168970847093894, 0.329637479054441, 0.932333806354766, 
0.920347276344009, 0.0188743828318305, 0.28700830085674, 0.122086809397366, 
0.721930469997516, 0.269049759688981, 0.942247445889545, 0.121226461766639, 
0.269410103956184, 0.543882827865607, 0.355909915494972, 0.805553875119072, 
0.562638932909877, 0.545114635056338, 0.799859513505137, 0.028300052307727, 
0.583254546237197, 0.256765686729071, 0.611805711034515, 0.12397867417326, 
0.904971687781288, 0.383547195151525, 0.798974934455656, 0.808113210022865, 
0.180237564596422, 0.523175753649262, 0.429905001793785, 0.304352173583082, 
0.40672229380892, 0.216918826340633, 0.995255476036526, 0.769882865282478, 
0.315877004728925, 0.473236774091463, 0.977350069130875), STAGE1 = c(0.368965432370926, 
0.700308646686629, 0.37243236843735, 0.118248974397151, 0.924365141290828, 
0.762350776297416, 0.697822321048335, 0.319088598768702, 0.877683213554975, 
0.750204754502701, 0.504154725402079, 0.142208638643596, 0.789965310283345, 
0.23064744899036, 0.356016354550943, 0.62336479144862, 0.213046955322873, 
0.0244722022581561, 0.988358158842575, 0.823084572956891, 0.303622687500738, 
0.96764100797214, 0.230471412999795, 0.817462150200591, 0.65596635231198, 
0.91076738319561, 0.819508996182778, 0.213131722185214, 0.588018916178037, 
0.123125204015868, 0.562792521212946, 0.660044396503998, 0.978415415705398, 
0.0434221393194675, 0.376954390238487, 0.0621651663358314, 0.465209931429691, 
0.384575866404939, 0.242944268682905, 0.0795230807069031, 0.0199700669678636, 
0.32561072886232, 0.93772541505141, 0.585428916722007, 0.318073982629541, 
0.32173669386666, 0.999051218264009, 0.317443552571237, 0.660217053596061, 
0.823899098997627, 0.514457733357758, 0.541111096768051, 0.55508809891723, 
0.984535365206941, 0.471258908268993, 0.993506687275954, 0.437682961713191, 
0.131816605892007, 0.624970126586813, 0.464518729700115), VEG1 = c(0.210969928562774, 
0.898985728644631, 0.22294741757493, 0.648093601120814, 0.575025933877221, 
0.836263821166674, 0.365992846024045, 0.975590314436119, 0.7669346163546, 
0.606281773283188, 0.727857331528672, 0.063336204942156, 0.56024387105928, 
0.572624926379006, 0.928959824695475, 0.0649894364822472, 0.484687521083679, 
0.0408377810735494, 0.0863494174729038, 0.38604744652937, 0.408453836726946, 
0.886479379706545, 0.484111782315899, 0.931864491830317, 0.822804779811041, 
0.42950876407607, 0.870394703931469, 0.643439731712405, 0.9667566201692, 
0.304607549846345, 0.364859919775087, 0.103200556844174, 0.548045455964784, 
0.198120269406476, 0.0343580658743184, 0.802310877023954, 0.993783336617898, 
0.0816308254407008, 0.75765020441394, 0.864780385461517, 0.312678655208107, 
0.74836775788609, 0.534055051243098, 0.288890283554979, 0.953079521748438, 
0.731857197421753, 0.142177987385593, 0.996762443657084, 0.243292464894029, 
0.491579671466928, 0.966627062162325, 0.339857447154563, 0.461728147289064, 
0.634729865229662, 0.3609970911949, 0.258390752729148, 0.506726536795493, 
0.672180274413292, 0.747445719779432, 0.29334486037021), VEG2 = c(0.380862280240474, 
0.627729953103904, 0.878278401019092, 0.5613186824923, 0.636249763005486, 
0.62292681302113, 0.720444249110863, 0.312557900422543, 0.234122422714287, 
0.499639072408177, 0.855974632998296, 0.952555098662679, 0.309983642251446, 
0.882338335501354, 0.361924385825836, 0.0113958292196743, 0.302133129735417, 
0.0208791658903581, 0.117232590999101, 0.925267325435408, 0.0711269914368884, 
0.552243014270943, 0.125495115593106, 0.661915190374669, 0.0293896936238214, 
0.294418615555011, 0.308905573205888, 0.26936911134833, 0.931505940770319, 
0.021435120451226, 0.227312617286182, 0.0195785283931383, 0.099692235112247, 
0.0692925181417872, 0.832555037914636, 0.380793087982671, 0.68110703188308, 
0.0936612801494288, 0.051047767208204, 0.273844437996178, 0.800343046949624, 
0.911380903925364, 0.582479041522435, 0.748111458524328, 0.303383302981895, 
0.906904782788438, 0.328934737098847, 0.69720659917789, 0.166551542287389, 
0.875547286349842, 0.265071728355448, 0.698643970290581, 0.271578241223373, 
0.956035715886986, 0.583251175640849, 0.268613096173177, 0.738778731483354, 
0.22125582045959, 0.289055369557255, 0.697577213911879), VEG3 = c(0.413110318831049, 
0.193370500853792, 0.0443323484313173, 0.312394866761789, 0.381013563679286, 
0.774340211752776, 0.315822280223384, 0.499646073445189, 0.0433440470476795, 
0.63592018453082, 0.375881818437784, 0.758815519342307, 0.517769074796318, 
0.0878879638478763, 0.368268890348083, 0.86661723542387, 0.159257788537873, 
0.302684958923527, 0.947570919817041, 0.157392788910852, 0.125769229458094, 
0.348650174982873, 0.526963940972057, 0.632646354644645, 0.351109914949622, 
0.836552506892888, 0.541982479414601, 0.427697355405521, 0.799356060602721, 
0.205934189654569, 0.511986141383475, 0.807482528548695, 0.232665456121877, 
0.933325748417277, 0.00683041685826713, 0.21579707708936, 0.92805622685536, 
0.754838770281907, 0.39901873988239, 0.991423691630203, 0.73157540991456, 
0.572610353705451, 0.806848128394244, 0.445431445389894, 0.26939539811961, 
0.584527298222492, 0.386004220382543, 0.242375752770125, 0.558781312238412, 
0.026491283834456, 0.343044539065029, 0.802187562912211, 0.885495299962394, 
0.853256462542473, 0.126077883381321, 0.780548336023421, 0.840250789403559, 
0.724812876802032, 0.704098036072535, 0.201989727535322), VEG4 = c(0.640799340986514, 
0.987906611244886, 0.619024206202544, 0.209427026032481, 0.489727139189696, 
0.687333141302772, 0.705507906383631, 0.208889941178721, 0.396874200897516, 
0.712945929935184, 0.973888093276408, 0.763823354263263, 0.377489995555215, 
0.0106930906722855, 0.803664402691951, 0.278756822036515, 0.0550468580770419, 
0.265168521344556, 0.987573377094547, 0.734619443919844, 0.732929467499789, 
0.241731972053084, 0.559823481667716, 0.155701349465554, 0.478406027901528, 
0.961160568832851, 0.0998154836815058, 0.412094044212779, 0.243883425544374, 
0.942308621371675, 0.939264039325847, 0.255191997669476, 0.25098970135836, 
0.882652992136823, 0.287854570522429, 0.332318399664456, 0.412416503681846, 
0.321868386633491, 0.415802982737621, 0.518373582745234, 0.160366074863356, 
0.328725259142546, 0.740582698190321, 0.630219424448588, 0.0963326376312392, 
0.841597785551852, 0.157610936357946, 0.37344035882236, 0.665277345336568, 
0.472350881237637, 0.905421464878138, 0.67001107287601, 0.931361601273204, 
0.478463787477743, 0.457093329803116, 0.901963213218536, 0.0989040910780277, 
0.648496145325191, 0.149756719258531, 0.766506521363941), VEG5 = c(0.180283594647103, 
0.787728519142257, 0.577806490890149, 0.909505719647681, 0.843259853705306, 
0.19170485676349, 0.88285289130116, 0.390956223533144, 0.400453739302465, 
0.138993973168619, 0.701071911445177, 0.0275528476660395, 0.196129789901458, 
0.243754073834767, 0.586994020311301, 0.309287604588564, 0.54116171026274, 
0.804029207727762, 0.633322226579327, 0.576488317293166, 0.657529806993353, 
0.896942372401784, 0.69552906585738, 0.00605796084044197, 0.0333828607443704, 
0.421697067898017, 0.344727073066524, 0.261778629704536, 0.662947164033825, 
0.264729400387399, 0.361422182455181, 0.867093056031147, 0.994741928030988, 
0.1442062746899, 0.757388857488693, 0.0417225005179914, 0.448764060787373, 
0.844033548534798, 0.615968575276885, 0.424045632312236, 0.419379052486891, 
0.47584349576189, 0.209702827083009, 0.765825214487151, 0.834675742228314, 
0.1887294652982, 0.960231969290817, 0.935011727569393, 0.976674243295263, 
0.820848463468705, 0.341388666484707, 0.552464582497579, 0.573838987965132, 
0.698330744449092, 0.383783839725491, 0.713851582153272, 0.964603044208545, 
0.70783623057194, 0.608167299267236, 0.461582775185318)), class = "data.frame", row.names = c(NA, 
-60L))

Code:

library(ggplot2)
library(ggcorrplot)
library(corrplot)

#Generate correlation matrix
res <- cor(sen)

#Generate matrix graphic
ggcorrplot(res, hc.order = FALSE, type = "lower",
           outline.col = "white",
           ggtheme = ggplot2::theme_gray,
           colors = c("#6D9EC1", "white", "#E46726"),
           lab = TRUE, lab_size = 7, show.diag=FALSE,
           legend.title = "r-value", tl.cex=17)+
  theme(
    legend.key.width=unit(1, "cm"),
    legend.key.height=unit(2.5, "cm"),
    legend.title=element_text(size=20),
    legend.text=element_text(size=15)
  )

Answer:

library(reshape2)
library(ggplot2)

res <- cor(sen)
res[upper.tri(res)] = NA
diag(res) = NA
d = melt(res)
d = d[d$Var2 %in% paste0("STAGE", 1:3),]

graphics.off()
ggplot(d, aes(x = Var1, y = Var2, fill = value, label = round(value, 2))) +
    geom_tile(color = "black") +
    geom_text() +
    labs(fill = "r-value") +
    coord_equal()  #OPTIONAL

Question:

Is there a way to add subtitles to a ggcorrplot graph? I've read through the documentation and there is no subtitle feature built into the package.

library(ggcorrplot)
data(mtcars)
ggcorrplot(round(cor(mtcars), 1))

Thanks


Answer:

Since we deal with a ggplot object we can simply add them

library(ggcorrplot)
data(mtcars)
ggcorrplot(round(cor(mtcars), 1)) + 
  labs(title = "My title",
       subtitle = "Here is a subtitle")

Question:

I am working with a connectivity matrix of a network with positive values only and I want to display it before and after a threshold being applied. I´ve already used corrplot library, which allows plenty of changes in legend and colors but I decided to use ggcorrplot because corrplot figures get changed when I try to put them together in a graph trough grid.arrange. The main problem with ggcorrplot is that I haven´t been able to change legend values to be only between 0 and 1. My code is the next

    library(ggcorrplot)
    library(ggplot)

    ggcorrplot(prom_orig_sub, legend.title = "Coherencia")

Could you suggest some solution to change legend values? Thak you so much.


Answer:

Try overwriting the fill with scale_fill_gradient. It would likely alter the default colour scheme for ggcorrplot though.

ggcorrplot(prom_orig_sub, legend.title = "Coherencia") + 
     scale_fill_gradient2(breaks=c(0, 1), limit=c(0, 1))