自定义ggcorrplot

Geo*_*-sp 5 r ggplot2

我想减少标记的大小ggcorrplot,并减少文本和图之间的空间.

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)
Run Code Online (Sandbox Code Playgroud)

eip*_*i10 9

您可以使用theme元素调整轴文本和绘图之间的距离.随着geom_tile在一个标准的ggplot,可以调整heightwidth瓷砖的.ggcorrplot似乎不接受这种调整.可能有一种我不知道的方式; 我以前没用过这个包.我的hacky解决方法是只覆盖一个白色网格以在瓷砖之间创建空间:

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) 
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

这种类型的情节也不像常规ggplot那样难以制作,然后你就可以完全控制情节元素:

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()) 
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

另一个黑客ggcorrplot是掩盖,然后重新绘制瓷砖geom_tile使我们可以访问heightwidth参数:

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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述