How do I change the shape of the legend key glyph to a hexagon in ggplot2?

LDT

I have been battling for a long time now to find a way to change the legend key of a ggplot2 with a hexagon. Any help or guidance are highly appreciated!

library(ggplot2)
set.seed(123)
ggplot(iris) +
  geom_jitter(aes(x=Species,y=Sepal.Length,color=Species),width=0.25) +
  guides(color= guide_legend(override.aes = list(shape = 21)))

Created on 2021-11-13 by the reprex package (v2.0.1)

user20650

You can create keys using grid commands and then pass to a geom_ using the key_glyph argument.

A quick example:

library(grid)
library(ggplot2)

draw_key_hex <- function (data, params, size) {
    # hexagon vertex coordinates 
    v <- list(x = c(0.95, 0.725, 0.275, 0.05, 0.275, 0.725), 
              y = c(0.5, 0.110288568297003, 0.110288568297003, 0.5, 0.889711431702997, 0.889711431702997))
    # hexagon grob
    polygonGrob(v$x, v$y, 
                gp = gpar(col = data$colour,
                          fill = alpha(data$fill, data$alpha)))
}

set.seed(123)
ggplot(iris, aes(x=Species,y=Sepal.Length,color=Species)) +
  geom_jitter(width=0.25, key_glyph=draw_key_hex) 

# or with fill
set.seed(123)
ggplot(iris, aes(x=Species,y=Sepal.Length,color=Species, fill=Species)) +
  geom_jitter(width=0.25, key_glyph=draw_key_hex) 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Change the shape of legend key for geom_bar in ggplot2

How do I create a legend for both color and shape in ggplot2

How to change legend in shape of ggplot

Change manually the shape of a legend ggplot2

Change shape with line on legend ggplot2

ggplot2 stacked column: How do I sort by date, use custom colors and change the legend?

How do I change line color in ggplot2 without legend disappearing?

How would I change the shape of ggplot legend symbols?

How can I replace legend key in ggplot2?

How can I change the legend linetype in ggplot2?

How can I change the legend labels in R (ggplot2)

How do I change the title of the legend in this ggplot

How do I create a cut-out hexagon shape?

change the key labels in a legend in ggplot2

Change the symbol in a legend key in ggplot2

How to use an image as a legend key glyph?

Change color of glyph in ggplot-legend

How can I completely remove legend.key from ggplot2 legend?

change legend shape of only one level of a variable in ggplot2

Change the shape of the legend in density plots with ggplot2

Change color shape and edit legend in R ggplot2 object

ggplot2: how to change the order of a legend

How to change items in a ggplot2 legend?

How to change legend title and key order and colour to a multi-stacked barplot in R with ggplot2

Change legend geom with override.aes or key_glyph?

How do I make half a hexagon shape using CSS with a border over a rectangle with a border with an image in the middle of the half hexagon

How to adjust legend for a colour-shape combiation in ggplot2?

How do I change the background colour of a legend box in ggplot?

How to change the orientation of the "Key" of a Legend in ggplot?