Search code examples
rggplot2geom-text

geom_label: change the thickness of the shape outline


With geom_label instead of geom_text, my labels get a background shape with a black outline shape. I know I can control the text size with size argument, but this doesn't affect the border. Is it possible to control the thickness of this black outline border?

Like having a result similar to the plot on the right hand side:

enter image description here

Example:

library(tidyverse)
ggplot(rownames_to_column(mtcars, "name")) + geom_label(aes(label=name, x=mpg, y=hp), size=4)

Thanks,


Solution

  • The label.size parameter can be set per-layer. It is not an aesthetic in the sense that you can map data to this parameter, so splitting the thin and thick outlined text in different layers seems the only sensible option.

    library(tidyverse)
    
    ggplot(rownames_to_column(mtcars, "name")) + 
      geom_label(data = ~ tail(.x, -4), 
                 aes(label=name, x=mpg, y=hp), 
                 size=4, label.size = 0.25) +
      geom_label(data = ~ head(.x, 4), 
                 aes(label=name, x=mpg, y=hp), 
                 size=4, label.size = 2)
    

    Created on 2022-01-31 by the reprex package (v2.0.1)