如何修改ggpairs相关矩阵图内文本的字体样式与大小
解决ggpairs相关矩阵图内文本/数值字号与加粗问题
要调整ggpairs图内(如上三角相关系数、坐标轴文本等)的字号和加粗样式,需要针对不同元素分别设置:
1. 调整上三角相关系数的字号与加粗
上三角的相关系数属于ggally_cor图层,无法通过全局theme控制,需在wrap("cor")函数中直接添加size和face参数:
upper = list(continuous = wrap("cor", method = "pearson", size = 12, face = "bold"))
2. 修正全局主题样式设置
原代码中text = element_text(size = 16, family = "bold")有误,family用于指定字体家族(如Arial、Times),控制加粗需使用face = "bold"。同时可按需补充坐标轴标题、图例文本的样式调整。
修改后的完整代码
library(GGally) # 创建带自定义样式的ggpairs图 Output <- ggpairs(Combined_with_MFV, columns = 2:15, aes(colour=Genotype), lower = list(continuous = "smooth"), # 设置上三角相关系数的字号与加粗 upper = list(continuous = wrap("cor", method = "pearson", size = 12, face = "bold")), # 可选:调整对角线变量名称的样式 diag = list(continuous = wrap("barDiag", text.size = 12, text.face = "bold"))) # 自定义主题元素 Output <- Output + theme( axis.text.x = element_text(face = "bold", size = 12), axis.text.y = element_text(face = "bold", size = 12), axis.title.x = element_text(face = "bold", size = 16), axis.title.y = element_text(face = "bold", size = 16), text = element_text(size = 16, face = "bold"), legend.text = element_text(face = "bold", size = 12) ) # 导出图片 tiff('Morpho.tiff', units="in", width=25, height=16, res=600, compression = 'lzw') Output dev.off() getwd()
关键说明
- 对角线的变量名称需通过
diag参数内的wrap函数,用text.size和text.face调整样式。 - 全局
theme仅能控制坐标轴文本、标题、图例等通用元素的样式。
内容的提问来源于stack exchange,提问作者ORBITTING ARYABHATA
相关产品推荐
相关产品推荐

