如何调整R语言mixOmics包PLS-DA绘图的外观样式?
解决mixOmics PLS-DA绘图美学调整问题
mixOmics的plotIndiv基于ggplot2,但有自己的参数控制绘图元素,直接用theme_classic()可能会和函数自带的设置冲突。以下是满足你需求的调整方案:
完整代码
library(mixOmics) library(ggplot2) data(srbct) X <- srbct$gene Y <- srbct$class final.plsda.srbct <- plsda(X,Y, ncomp = 3) # 生成基础图并调整核心参数 p <- plotIndiv(final.plsda.srbct, ind.names = FALSE, legend = TRUE, comp = c(1,2), ellipse = TRUE, title = 'PLS-DA on SRBCT comp 1-2', X.label = 'PLS-DA comp 1', Y.label = 'PLS-DA comp 2', grid = FALSE, # 移除默认网格线 title.args = list(bg = "white", box.col = NA) # 移除标题的灰色背景和边框 ) # 添加X=0和Y=0的虚线 p <- p + geom_vline(xintercept = 0, linetype = "dashed", color = "gray50") + geom_hline(yintercept = 0, linetype = "dashed", color = "gray50") + # 确保面板无多余背景元素 theme(panel.background = element_rect(fill = "white")) print(p)
关键调整说明
- 移除网格线:使用
plotIndiv自带的grid = FALSE参数,直接关闭函数默认生成的网格线,比通过ggplot2 theme移除更彻底。 - 移除顶部灰色标题框:通过
title.args参数设置标题背景为白色、边框透明,解决title=NULL仅移除文字但保留灰色框的问题。如果不需要标题文字,可将title设为""空字符串。 - 添加X/Y轴零点虚线:利用ggplot2的
geom_vline和geom_hline手动添加零点虚线,可根据需求调整线条样式(如linetype、color)。
内容的提问来源于stack exchange,提问作者dkcongo
相关产品推荐
相关产品推荐

