r - 如何在 ggplot2 中将图例键字形的形状更改为六边形?
问题描述
我一直在努力寻找一种方法来改变带有六边形的 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 包于 2021-11-13 创建(v2.0.1)
解决方案
您可以使用命令创建键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)
推荐阅读
- html - 如果满足条件,则从数组中映射字母 REACT
- neo4j - 在不复制现有关系的情况下更新 neo4j 关系属性
- linux - git clone 自带编辑过的文件
- mysql - Sequal Pro Connect 数据库 SSH 密钥对问题(尝试重置)
- vlc - VLC:将流保存到文件
- javascript - 用本地 json 文件填充 Tabulator 表
- c# - 在登录到 Blazor Server .Net Core 5 站点时在哪里设置会话变量?
- mysql - 简单插入到我们的 mysql8 安装中非常慢
- linux - 当我将 esp 移入 ebp 时,我在程序集中遇到分段错误(核心转储),我该如何解决这个问题?
- django - 如何在 django 的 html 页面中添加表单