ggcorrplot中调整y轴标签至右侧对应圆形旁的实现方法咨询
ggcorrplot移动y轴标签到右侧的实现方法
这个需求完全可以实现。你之前调整axis.text.y没有生效的核心原因是代码中指定了ggtheme = ggplot2::theme_void,这个主题会默认移除所有坐标轴文本、轴线、刻度等元素,因此额外追加的轴文本配置不会生效。
核心调整逻辑
- 替换
theme_void为普通基础主题,或手动开启轴文本显示 - 通过
scale_y_discrete(position = "right")将y轴整体移动到绘图区域右侧 - 调整y轴文本对齐参数,保证标签和对应行的圆形元素对齐
完整可运行代码
library(ggcorrplot) library(ggplot2) data(mtcars) corr<-cor(mtcars) cortest<-cor_pmat(mtcars, sig.level = 0.05) ggcorrplot( corr, p.mat = cortest, hc.order = F, method = "circle", type = "lower", outline.color = "black", legend.title = "", lab = TRUE, lab_size = 2.5, # 替换theme_void为基础主题,保留轴元素的渲染能力 ggtheme = ggplot2::theme_bw, colors = c("#6D9EC1", "white", "#E46726") ) + # 将y轴移动到绘图右侧 scale_y_discrete(position = "right") + # 自定义主题,适配corrplot的展示风格 theme( # 调整右侧y轴文本对齐,hjust控制标签和图形的间距,越小间距越近 axis.text.y.right = element_text(hjust = 0, vjust = 0.5, size = 9), axis.text.x = element_blank(), # 不需要显示x轴标签可保留,需要则删除该行 axis.ticks = element_blank(), axis.title = element_blank(), panel.border = element_blank(), panel.grid = element_blank() )
如果需要调整标签和圆形的间距,修改axis.text.y.right中的hjust参数即可,参数取值范围为0~1。
内容的提问来源于stack exchange,提问作者Lee1010
相关产品推荐
相关产品推荐

