You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在ggplot箱线图上叠加六边形原始数据点(基于ggstar)?

解决方案

1. 安装并加载ggstar包

首先确保安装了ggstar包,若未安装则执行安装命令,之后加载包:

install.packages("ggstar")
library(ggstar)

2. 替换geom_point为geom_star实现六边形点

直接将原代码中的geom_point替换为geom_star,通过starshape = 6指定六边形,同时保留抖动位置、颜色等参数即可。

完整可复现代码:

df <- data.frame(variable1 = c(1,3,6,3,5,2,2,4,1))

library(ggplot2)
library(ggstar)

ggplot(df, aes(x = 0, y = variable1)) +
  geom_boxplot(alpha = 0.5, outlier.shape = NA) +
  # 用geom_star生成六边形点,starshape=6对应正六边形
  geom_star(position = position_jitter(width = 0.2), 
            color = "red", 
            starshape = 6,
            size = 3) + # 可根据需求调整六边形大小
  theme(axis.text.x = element_blank(),
        axis.ticks.x = element_blank()) +
  labs(x = NULL)

关键细节说明

  • starshape = 6是ggstar包中对应正六边形的专属编码;
  • position_jitter(width = 0.2)让六边形点横向分散,避免数据重叠;
  • 箱线图设置outlier.shape = NA是为了隐藏默认异常值点,确保所有原始数据都通过自定义的六边形点展示。

内容的提问来源于stack exchange,提问作者Bradley Allf

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 16:37:08