You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Pearson相关系数图中实现化学名称下标显示?

解决metan包绘制Pearson相关系数图时化学名称下标显示问题

直接使用plotmath表达式定义列名,并在绘图时开启表达式解析即可实现下标显示,完整代码如下:

# 修改列名为plotmath表达式格式,[]内的内容会被解析为下标,~用于添加空格
colnames(JO_Data) <- c(expression(CO[2]),
                       expression(Cumulative~CO[2]),
                       expression(N[2]O),
                       expression(Cumulative~N[2]O),
                       "pH",
                       "pH (CaCl2)",
                       "EC",
                       "MBC",
                       expression(NH[4]),
                       expression(NO[3]))

library(metan)
corrl <- corr_coef(JO_Data)
# 开启parse参数解析表达式
plot(corrl, parse = TRUE)

关键说明

  • expression()是R中用于生成数学表达式的函数,CO[2]会被解析为CO₂的格式
  • ~符号用于在表达式中添加空格,实现类似"Cumulative CO₂"的分隔效果
  • plot()函数中的parse = TRUE参数是核心,让metan包自动识别并解析plotmath表达式

原始问题背景

使用R语言metan包绘制Pearson相关系数图时,无法将CO₂、N₂O等化学名称中的数字设置为下标显示。

原始代码

colnames(JO_Data) = c("CO2","Cumulative CO2", "N2O", "Cumulative N2O", "pH", "pH (CaCl2)", "EC", "MBC", "NH4", "NO3")

library(metan)

corrl <- corr_coef(JO_Data)

plot(corrl)

当前生成的图

Pearson相关系数图

数据结构

> str(JO_Data)
'data.frame':   288 obs. of  10 variables:
 $ CO2           : num  0.517 0.448 0.593 0.627 1.211 ...
 $ Cumulative CO2: num  0.517 0.448 0.593 0.627 1.211 ...
 $ N2O           : num  0.0264 0.0241 0.0298 0.0338 0.0644 ...
 $ Cumulative N2O: num  0.0264 0.0241 0.0298 0.0338 0.0644 ...
 $ pH            : num  5.9 5.94 5.98 5.95 6.1 6.09 6.12 6.11 5.87 6.06 ...
 $ pH (CaCl2)    : num  5.18 5.18 5.18 5.18 5.34 5.34 5.34 5.34 5.33 5.35 ...
 $ EC            : num  90.9 87.4 79.7 88.9 147.4 ...
 $ MBC           : num  681 634 685 619 633 ...
 $ NH4           : num  10 8 7.6 24.7 158.3 ...
 $ NO3           : num  20.5 21.6 19.4 17.7 60.8 55.8 59.7 54 19.8 21.5 ...

示例数据

JO_Data <- structure(list(CO2 = c(0.517359932, 0.447990483, 0.593061985, 
0.627331461, 1.21050112, 1.209451298), `Cumulative CO2` = c(0.517359932, 
0.447990483, 0.593061985, 0.627331461, 1.21050112, 1.209451298
), N2O = c(0.02643275, 0.02407505, 0.02975459, 0.03384935, 0.0643722, 
0.06104556), `Cumulative N2O` = c(0.02643275, 0.02407505, 0.02975459, 
0.03384935, 0.0643722, 0.06104556), pH = c(5.9, 5.94, 5.98, 5.95, 
6.1, 6.09), `pH (CaCl2)` = c(5.18, 5.18, 5.18, 5.18, 5.34, 5.34
), EC = c(90.88, 87.4, 79.74, 88.86, 147.4, 146.2), MBC = c(680.706103, 
634.1018757, 684.5231352, 618.7922904, 632.797167, 649.4327767
), NH4 = c(10, 8, 7.6, 24.7, 158.3, 132.6), NO3 = c(20.5, 21.6, 
19.4, 17.7, 60.8, 55.8)), row.names = c(NA, 6L), class = "data.frame")

内容的提问来源于stack exchange,提问作者James

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 02:26:19