首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义ggcorr图

自定义ggcorr图
EN

Stack Overflow用户
提问于 2016-12-21 18:31:26
回答 1查看 11K关注 0票数 7

我希望减少ggcorrplot中标记的大小,并减少文本和绘图之间的空间。

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-21 19:00:59

您可以使用theme元素调整轴文本与绘图之间的距离。使用标准geom_tile中的ggplot,您可以调整瓷砖的heightwidthggcorrplot似乎不接受这种调整。也许有一种方式我不知道,我以前没有使用过这个包。我讨厌的解决方法就是覆盖一个白色的网格,以便在瓷砖之间创建空间:

代码语言:javascript
复制
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) 

这种类型的绘图作为一个常规的ggplot也不是那么困难,然后您将完全控制绘图元素:

代码语言:javascript
复制
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()) 

ggcorrplot的另一个黑客是掩盖并重新绘制使用geom_tile的瓷砖,这样我们就可以访问heightwidth参数:

代码语言:javascript
复制
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)

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41269593

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档