teunbrand / ggh4x

ggplot extension: options for tailored facets, multiple colourscales and miscellaneous
https://teunbrand.github.io/ggh4x/
Other
541 stars 33 forks source link

Colour/fill legend with coloured text #31

Closed teunbrand closed 3 years ago

teunbrand commented 3 years ago

Implement new legend guide that describes colour or fill with coloured text. Should take the scale's labels and apply the mapped colour to the labels. Goal is to have it look like the following, but easier to specify and strings left-aligned by default.

library(ggplot2)

ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
  geom_point(aes(colour = Species),
             key_glyph = draw_key_text) +
  scale_colour_discrete(
    labels = NULL,
    guide = guide_legend(
      override.aes = list(label = levels(iris$Species),
                          size = 4, vjust = 0)
    )
  ) +
  theme(legend.key.width = unit(3, "cm"),
        legend.key = element_blank())

Created on 2021-01-06 by the reprex package (v0.3.0)

teunbrand commented 3 years ago

Might as well make a spin-off package with different guide extensions

teunbrand commented 3 years ago

You can do this now.

library(ggh4x)
#> Loading required package: ggplot2
ggplot(mpg, aes(displ, hwy)) +
  geom_point(aes(colour = manufacturer)) +
  guides(colour = guide_stringlegend(ncol = 2))

Created on 2021-01-09 by the reprex package (v0.3.0)