我一直在努力尋找一種方法來更改帶有六邊形的 ggplot2 的圖例鍵。非常感謝任何幫助或指導!
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)))

由reprex 包(v2.0.1)于 2021 年 11 月 13 日創建
uj5u.com熱心網友回復:
您可以使用grid命令創建鍵,然后geom_使用key_glyph引數傳遞給 a 。
一個簡單的例子:
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)

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/358892.html
