r - 更改字体类型并在 corrplot 相关图的标签中添加下标或希腊字母

标签 r label subscript r-corrplot

我想使用 corrplot R 包制作相关图。

使用的数据如下表

head(dat)
    pH      EC     NO3    NH4    Alpha     Beta
1 8.32 12.6174  5.5658 0.6406 641.1503 545.1455
2 8.19 11.9373 10.5631 0.9045 754.5042 514.9512
3 8.07 11.3534  6.8375 0.7977 744.9126 493.5152
4 8.19 12.4137  9.9584 0.9023 879.1580 655.9412
5 7.24  9.4181 10.0677 0.9382 637.0015 489.1772
6 7.75 10.3971 10.6647 1.0393 756.1199 517.5440
....    ....
....    ....
....    ....

但我希望 NO3 和 NH4 的相关图标签中的数字位于下标中,而 Alpha 和 Beta 的相关图标签中的数字使用希腊字母 α 和 β。

我用来制作相关图的代码如下

M <- cor(dat) #create correlation matrix

# colors to be used
col2 <- colorRampPalette(c("#67001F", "#B2182B", "#D6604D", "#F4A582", 
                           "#FDDBC7","#FFFFFF", "#D1E5F0", "#92C5DE", 
                           "#4393C3", "#2166AC", "#053061"))

library(corrplot)

corrplot(M,method="square", order="original", col=col2(20), 
            cl.length=11,type="lower",tl.pos = "lt",tl.cex = 1,
            tl.col = "black", tl.offset = 0.4, tl.srt = 90,cl.pos="r",
            diag = FALSE)

correlogram

数据

dat <- structure(list(pH = c(8.32, 8.19, 8.07, 8.19, 7.24, 7.75, 8.13, 
8.06, 7.91, 6.77, 5.28, 7.89, 7.68, 6.23, 8.31, 8.38, 8.31, 7.12, 
8.2, 8.33, 7.66, 6.24, 6.7, 7.84), EC = c(12.6174, 11.9373, 11.3534, 
12.4137, 9.4181, 10.3971, 14.7648, 13.7368, 15.7797, 7.1344, 
7.2042, 15, 11.7473, 8.087, 13.755, 13.5277, 14.8324, 8.0436, 
14.4983, 13.6894, 16.1245, 7.1204, 7.4364, 13.4685), NO3 = c(5.5658, 
10.5631, 6.8375, 9.9584, 10.0677, 10.6647, 11.6462, 12.5271, 
10.9805, 7.4731, 9.9492, 10.7479, 11.6753, 10.4071, 11.3829, 
12.2611, 11.9056, 10.2238, 9.8128, 11.1856, 4.3521, 4.6849, 7.3659, 
9.8142), NH4 = c(0.6406, 0.9045, 0.7977, 0.9023, 0.9382, 1.0393, 
1.1002, 1.1534, 1.2629, 1.0686, 1.0292, 1.1485, 1.058, 1.2891, 
1.0845, 1.1024, 1.1585, 1.0358, 0.9188, 0.9402, 0.9176, 0.8957, 
1.0508, 0.7891), Alpha = c(641.1503, 754.5042, 744.9126, 879.158, 
637.0015, 756.1199, 888.1357, 928.22, 982.75, 683.9139, 729.0595, 
764.2607, 673.8184, 493.8568, 708.114, 759.094, 1040.6623, 698.2495, 
819.0605, 710.4485, 655.0136, 805.2089, 645.5548, 831.6886), 
    Beta = c(545.1455, 514.9512, 493.5152, 655.9412, 489.1772, 
    517.544, 588.3099, 579.361, 564.1002, 483.3981, 511.9086, 
    440.1535, 497.5929, 328.9879, 506.202, 505.5201, 636.8879, 
    494.4532, 461.9742, 439.8569, 492.6054, 549.7177, 428.9918, 
    540.8364)), .Names = c("pH", "EC", "NO3", "NH4", "Alpha", 
"Beta"), class = "data.frame", row.names = c(NA, -24L))

最佳答案

也许解决这个问题的方法是将 corrplot 函数中的标签颜色设置为 white (tl.col = "white"),然后只需为标签传递新的 text 语句。

corrplot(M,method="square", order="original", col=col2(20), 
         cl.length=11,type="lower",tl.pos = "lt",tl.cex = 1,
         tl.col = "white", tl.offset = 0.4, tl.srt = 90,cl.pos="r",
         diag = FALSE)

text(1:5, 5.75, expression("pH", "EC", "NO"[3], "NH"[4], Alpha))
text(0, 5:1,  expression("EC", "NO"[3], "NH"[4], Alpha, Beta))

enter image description here

您可以将其包装在一个函数中,其中 xy 位置来自矩阵的行/列数

关于r - 更改字体类型并在 corrplot 相关图的标签中添加下标或希腊字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27059543/

相关文章:

python - 如何使用 python 3.2 生成(并标记)一个随机整数?

wpf - FormattedText 类中的下标/上标?

google-chrome - Chrome中的contentEditable上标和下标

mysql - 在 R 中运行 MySQL 查询,其中要搜索的值在 DF 中

r - 计算组内差异 R

html - React Native - 输入顶部的标签

c++ - 无法在 visual studio 2012 中捕获 "ctor subscript out of range"异常

在 R 中添加时删除 NA

r - 使用 tidy 评估选择命名的 [list] 元素

python - 如何更改部分 tkinter 标签的字体样式