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

如何修改ggdist::stat_dots函数中点的颜色与大小?

解决ggdist::stat_dots点的颜色与大小修改问题

要修改ggdist::stat_dots生成的散点颜色和大小,直接在stat_dots()函数内添加对应参数即可——这些样式属于该统计层的专属设置,而非全局映射项。

修改后的完整可运行代码

# 补充原代码中缺失的give.n函数
give.n <- function(x){
  return(c(y = median(x) + 0.3, label = length(x)))
}

data <- data.frame(type = c('fruit', 'root', 'fruit', 'root'),
                   food = c('apple', 'taro', 'banana', 'potato'),
                   weight = c(2, 4, 3, 2))

ggplot(data, aes(x = type, y = weight)) +
  # 在这里设置stat_dots的点颜色和大小
  ggdist::stat_dots(side = "both", color = "blue", size = 2) +
  theme_classic() +
  theme(axis.title.x = element_text(size = 20 ), axis.text.x = element_text(size = 15),
        axis.title.y = element_text(size = 20 ), axis.text.y = element_text(size = 15),
        plot.title = element_text(size = 30),
        plot.subtitle = element_text(size = 20)) +
  stat_summary(fun = mean, geom = "point", shape = 18, size = 6, color="black")  + 
  stat_summary(fun.data = give.n, geom = "text", fun = median, size = 15, position = position_dodge(width = 0.75)) +
  labs(title = "title", subtitle = "sub title"  ,x = "x title", y = "y title",
       caption = "caption") +
  scale_y_continuous(expand = c(0, 0), limits = c(0, 4.5))

关键说明

  • 修改stat_dots样式:在ggdist::stat_dots()中添加color = "blue"(可替换为"red"等任意颜色值)和size = 2(数值越大点越大,按需调整),即可统一设置所有散点的颜色与大小。
  • 优化映射写法:原代码中aes(x = data[,1], y = data[,3])的写法不够直观,改为直接引用列名aes(x = type, y = weight)更易维护。
  • 补全依赖函数:原代码中调用了give.n但未定义,补充该函数实现后代码才能正常运行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 23:52:43