Search code examples
rlatticeglmlevelplot

Manually control axes tick mark labels in glm plot


I am using levelplot() to plot a traitglm() output. The axes labels are automated. I would like to replace each tickmark label with a custom expression, i.e. 'woo' should be 'Wood' and 'XH' should be 'Grassland'.

Also, how can I reorder 'XH','XL''XO' to 'XO','XL','XH' in the plot?

I use this code for plotting:

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)

The output looks as follows:

enter image description here

Many thanks for any advice:

This is the dput() of traitsglm:

> 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")))

Solution

  • One possible approach is to make the changes to the matrix before passing it to levelplot().

    Original:

    > 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
    

    Modified version:

    # 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
    

    Plot using the modified version:

    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