在glm plot中手动控制轴刻度标记

时间:2017-10-23 06:28:35

标签: r lattice glm levelplot

我使用levelplot()绘制traitglm()输出。轴标签是自动化的。我想用自定义表达式替换每个tickmark标签,即'woo'应该是'Wood'而'XH'应该是'Grassland'。

另外,如何在剧情中将'XH','XL''XO'重新排序为'XO','XL','XH'?

我使用此代码进行绘图:

aSO        = max( abs(traitsglm) )
colortSO   = colorRampPalette(c("blue", "white", "red")) 
plot.4thSO = levelplot(t(as.matrix(traitsglm)), 
                       xlab = "Env", 
                       ylab = "Traits", 
                       col.regions = colortSO(100), 
                       at = seq(-aSO, aSO, length = 100),
                       scales = list(x = list(rot = 45)))
print(plot.4thSO)

输出如下:

enter image description here

非常感谢任何建议:

这是traitsglm的dput():

> dput(traitsglm)
structure(c(0, 0, 0, 0, -0.0156616179765263, 0.0457475723683713, 
0, 0, 0.065063431709575, 0.0390952440996195, 0, -0.0194671501115163, 
0, -0.0234664518436303, 0, 0.0916078166850258, 0.0109256858453898, 
0, 0.0137149350117998, 0, -0.0227890851186177, 0, 0, 0.0419307281357592, 
0.0143375543423802, 0, 0, 0, -0.0470842266584843, -0.0154350203811271, 
0, 0.0167536127627214, 0, 0, 0, -0.0294763453032897, 0, 0, -0.0456185799682106, 
0.0207276137008492, 0, 0, 0, 0, 0, 0, 0.000971877948579576, 0.0141354127135407, 
0, 0, -0.0142399452642402, 0, 0, 0.0169159154100949, 0, 0, -0.0126555144059337, 
0, 0, -0.00956369894367865), .Dim = c(20L, 3L), .Dimnames = list(
    c("disan", "disba", "diszo", "frube", "fruca", "frudr", "frufo", 
    "frule", "frunu", "frusy", "pgt", "pola", "polb", "polf", 
    "poli", "polw", "polx", "hei", "see", "woo"), c("XH", "XL", 
    "XO")))

1 个答案:

答案 0 :(得分:0)

一种可能的方法是在将矩阵传递给levelplot()之前对矩阵进行更改。

原件:

> traitsglm
               XH          XL            XO
disan  0.00000000 -0.02278909  0.0000000000
disba  0.00000000  0.00000000  0.0000000000
diszo  0.00000000  0.00000000  0.0000000000
frube  0.00000000  0.04193073  0.0000000000
fruca -0.01566162  0.01433755  0.0000000000
frudr  0.04574757  0.00000000  0.0000000000
frufo  0.00000000  0.00000000  0.0009718779
frule  0.00000000  0.00000000  0.0141354127
frunu  0.06506343 -0.04708423  0.0000000000
frusy  0.03909524 -0.01543502  0.0000000000
pgt    0.00000000  0.00000000 -0.0142399453
pola  -0.01946715  0.01675361  0.0000000000
polb   0.00000000  0.00000000  0.0000000000
polf  -0.02346645  0.00000000  0.0169159154
poli   0.00000000  0.00000000  0.0000000000
polw   0.09160782 -0.02947635  0.0000000000
polx   0.01092569  0.00000000 -0.0126555144
hei    0.00000000  0.00000000  0.0000000000
see    0.01371494 -0.04561858  0.0000000000
woo    0.00000000  0.02072761 -0.0095636989

修改版本:

# reverse x-axis order
tg <- traitsglm[,3:1] 

# change axis labels
attr(tg, "dimnames") <- list(
  c("Y1", "Y2", "Y3", "Y4", "Y5", "Y6", "Y7", "Y8", "Y9", "Y10",
    "Y11", "Y12", "Y13", "Y14", "Y15", "Y16", "Y17", "Y18", "Y19", "Wood"),
  c("X1", "X2", "Grassland") 
)

> tg
                X1          X2   Grassland
Y1    0.0000000000 -0.02278909  0.00000000
Y2    0.0000000000  0.00000000  0.00000000
Y3    0.0000000000  0.00000000  0.00000000
Y4    0.0000000000  0.04193073  0.00000000
Y5    0.0000000000  0.01433755 -0.01566162
Y6    0.0000000000  0.00000000  0.04574757
Y7    0.0009718779  0.00000000  0.00000000
Y8    0.0141354127  0.00000000  0.00000000
Y9    0.0000000000 -0.04708423  0.06506343
Y10   0.0000000000 -0.01543502  0.03909524
Y11  -0.0142399453  0.00000000  0.00000000
Y12   0.0000000000  0.01675361 -0.01946715
Y13   0.0000000000  0.00000000  0.00000000
Y14   0.0169159154  0.00000000 -0.02346645
Y15   0.0000000000  0.00000000  0.00000000
Y16   0.0000000000 -0.02947635  0.09160782
Y17  -0.0126555144  0.00000000  0.01092569
Y18   0.0000000000  0.00000000  0.00000000
Y19   0.0000000000 -0.04561858  0.01371494
Wood -0.0095636989  0.02072761  0.00000000

使用修改后的版本进行绘图:

library(lattice)

levelplot(t(as.matrix(tg)), 
          xlab = "Env", 
          ylab = "Traits", 
          col.regions = colortSO(100), 
          at = seq(-aSO, aSO, length = 100),
          scales = list(x = list(rot = 45)))

plot