如何调整caret包featurePlot配对图字体大小并转至ggplot修改?
你用caret的featurePlot生成的是lattice绘图体系的图,没法直接导入ggplot修改,给你两个实用解决方案:
方案1:直接在lattice体系内调整字体大小
featurePlot基于lattice,可通过预设主题或参数直接修改各类文字大小:
- 全局调整字体:用
trellis.par.set()统一设置坐标轴、图例等元素的字体
library(caret) # 预设lattice主题的字体大小 trellis.par.set( axis.text = list(cex = 0.8), # 坐标轴刻度文字 axis.label = list(cex = 1), # 坐标轴标签 legend.text = list(cex = 0.9) # 图例文字 ) # 生成调整后的图 plotFvP <- featurePlot(x = Top_pred_FVP[,1:5], y = Top_pred_FVP$Tissue, plot = "pairs", cex=0.5, auto.key = list(columns = 2)) plotFvP
- 单独调整图例字体:直接在
auto.key里加cex参数
plotFvP <- featurePlot(x = Top_pred_FVP[,1:5], y = Top_pred_FVP$Tissue, plot = "pairs", cex=0.5, auto.key = list(columns = 2, cex = 0.9))
方案2:用ggplot生态的GGally重绘配对图
如果你更习惯ggplot语法,用GGally包的ggpairs复刻图后,就能用theme()自由调整字体:
- 安装加载依赖包
install.packages("GGally") library(GGally) library(ggplot2)
- 绘制配对图并调整字体
ggpairs(Top_pred_FVP, columns = 1:5, # 指定要展示的特征列 aes(color = Tissue), # 按Tissue分组上色 lower = list(continuous = "points", params = list(size = 1)), # 散点大小对应原cex=0.5 upper = list(continuous = "blank"), # 上三角留空,和原图一致 diag = list(continuous = "densityDiag") # 对角线用密度图 ) + theme( axis.text = element_text(size = 10), # 坐标轴刻度文字大小 axis.title = element_text(size = 12), # 坐标轴标签大小 legend.text = element_text(size = 10), # 图例文字大小 legend.title = element_text(size = 11) # 图例标题大小 )
内容的提问来源于stack exchange,提问作者Marion
相关产品推荐
相关产品推荐

