Change geom_text's default "a" legend to label string itself
Asked Answered
C

2

16

Similarly to this question, I want to change the default "a" in the legend, but rather than removing it completely, I want to replace it with the labels themselves. That is, the first line of the legend should have a colored icon labeled "se" with the full name "setosa" on the right.

iris$abbrev = substr( iris$Species, 1, 2 )
     
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, 
                        shape = Species, colour = Species)) +
    geom_text(aes(label = abbrev))

enter image description here

Cystolith answered 22/4, 2018 at 12:12 Comment(0)
E
17

You can change the legend key generating function. This still requires a bit of manual intervention, but arguably less than using the grobs.

library(ggplot2)
library(grid)

data(iris)
iris$abbrev = substr( iris$Species, 1, 2 )

oldK <- GeomText$draw_key # to save for later

# define new key
# if you manually add colours then add vector of colours 
# instead of `scales::hue_pal()(length(var))`
GeomText$draw_key <- function (data, params, size, 
                               var=unique(iris$abbrev), 
                               cols=scales::hue_pal()(length(var))) {

    # sort as ggplot sorts these alphanumerically / or levels of factor
    txt <- if(is.factor(var)) levels(var) else sort(var)
    txt <- txt[match(data$colour, cols)]

    textGrob(txt, 0.5, 0.5,  
             just="center", 
             gp = gpar(col = alpha(data$colour, data$alpha), 
                       fontfamily = data$family, 
                       fontface = data$fontface, 
                       fontsize = data$size * .pt))
}

ggplot(data=iris, aes(x=Sepal.Length, y=Sepal.Width, 
                      shape=Species, colour=Species)) +  
  geom_text(aes(label = abbrev))


# reset key
GeomText$draw_key <- oldK

enter image description here

Electrolier answered 22/4, 2018 at 14:2 Comment(0)
C
10

You can work on grobs as follows:

p <- ggplot(data = iris, aes(x = Sepal.Length, y=Sepal.Width, shape =
Species, colour = Species)) +  geom_text(aes(label = abbrev))

g <- ggplotGrob(p)
lbls <- unique(iris$abbrev)
g$grobs[[15]][[1]][[1]]$grobs[[4]]$label <- lbls[1]
g$grobs[[15]][[1]][[1]]$grobs[[6]]$label <- lbls[2]
g$grobs[[15]][[1]][[1]]$grobs[[8]]$label <- lbls[3]

library(grid)
grid.draw(g)

enter image description here

Cienfuegos answered 22/4, 2018 at 12:49 Comment(4)
What is [[15]][[1]][[1]] here?Mephistopheles
@JackBrookes g$grobs[[15]] is the guide-box TableGrob (i.e. the legend) and g$grobs[[15]][[1]] is a list with the guides TableGrob as a first element. The text elements of the legend are collected inside g$grobs[[15]][[1]][[1]]Cienfuegos
Good lord. Well, it definitely works! As a note to others generalizing this to other ggplots, you'll need to replace the "15" index by looking at your g$grobs to find the one starting with TableGrob (5 x 5) "guide-box".Cystolith
@Cystolith ; you can find the grobs by looking at the names ie g$grobs[[grep("guide", g$layout$name)]]Electrolier

© 2022 - 2024 — McMap. All rights reserved.