Search code examples
rggplot2labelggally

Change label in ggpairs upper panel


Do you know how to change the labels in an upper panel in ggpairs (Ggally package)? I found how to change size, font but not label. Here I want to shorten the label ("set" pour setosa etc...). I tried to put that in labels=c("set", "ver", "vir") or upper=list(params=list(size=8),labels=c("set", "ver", "vir")) but it doesn't work.

ggpairs(iris, columns=c(2:4), title="variable analysis", colour="Species",
        lower=list(params=list(size=2)), upper=list(params=list(size=8))) 

iris DATA GGPAIRS


Solution

  • Conceptually the same as @Mike's solution, but in one line.

    levels(iris$Species) <- c("set", "ver", "vir")
    ggplairs(<...>)
    

    Here's another, more flexible proposal if you have many levels and do not want to abbreviate them by hand: trim levels to a desired length.

    levels(iris$Species) <- strtrim(levels(iris$Species), 3)
    ggplairs(<...>)
    

    And by the way, the width parameter is also vectorized:

    rm(iris)
    strtrim(levels(iris$Species), c(1, 3, 5))
    #[1] "s"     "ver"   "virgi"