r - 自定义 ggcorrplot

标签 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)

最佳答案

您可以使用 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) 

enter image description here

这种类型的绘图也不像普通的 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()) 

enter image description here

另一个黑客 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)

enter image description here

关于r - 自定义 ggcorrplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41269593/

相关文章:

r - R中矩阵中可变列数的减法

r - 控制ggplot2图异常(exception)观而不影响绘图

ggplot2 - 牛虻地 block 目前是可组合的吗?

r - 如何向 ggplot2 主题添加额外参数?

r - 将选项设置为 NULL

r - 将列求和为特定值

重新调整一个因素

r - 如何使用场景向 scatter3D Plotly 绘图添加注释?

r - 如何在ggplot2中分别缩放线和点的大小

r - 在图片的特定区域绘制点