Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 259
catindri Avatar asked Jul 02 '15 06:07

catindri


2 Answers

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"
like image 166
tonytonov Avatar answered Oct 29 '22 01:10

tonytonov


It is a bit ugly, but you could do this (rename the levels in the plot):

library(GGally)
gplt <- ggpairs(iris, columns=c(2:4), 
        title="variable analysis", 
        colour="Species", 
        lower=list(params=list(size=2)), 
        upper=list(params=list(size=6)))

levels(gplt$data$Species)[levels(gplt$data$Species)=="versicolor"] <- "ver"
levels(gplt$data$Species)[levels(gplt$data$Species)=="virginica"] <- "vir"
levels(gplt$data$Species)[levels(gplt$data$Species)=="setosa"] <- "set"

print(gplt)

enter image description here

like image 28
Mike Wise Avatar answered Oct 29 '22 02:10

Mike Wise