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

为何使用ggplot绘制的条形图y轴没有显示刻度?

问题原因

你为y轴调用了适配离散分类变量的scale_y_discrete()函数,但geom_bar()默认生成的y轴数值是计数统计值,属于连续数值类型,标度类型和变量类型不匹配,因此y轴刻度无法正常渲染显示。

修正方案

将原代码中的scale_y_discrete("Number of Individuals")替换为适配连续变量的scale_y_continuous("Number of Individuals")即可,修改后完整可运行代码如下:

ggplot(data = PTC, mapping = aes(x = Genotype, fill = Smoker)) +
  geom_bar(position = position_dodge()) +
  ggtitle("Barplot of Genotype and Smoking Status") +
  scale_fill_discrete(name = "Smoking Status",
                      breaks = c("N", "S"),
                      labels = c("Non-smoker", "Smoker")) +
  scale_y_continuous("Number of Individuals") +
  geom_text(aes(label =..count..), stat = "count", position = position_dodge(width = 0.9), vjust = 1.25)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 13:36:05