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

R语言ggplot2绘图:如何让Y轴显示所有刻度层级?

解决方法

修改后的代码如下:

library(ggplot2)
data <- read.table(text = "br tr ac met
L FA   73 OLD
L FA   70 NEW
Y FA   50 OLD
Y FA   57 NEW
L DL   17 OLD
L DL   7 NEW
Y DL   29 OLD
Y DL   23 NEW
L GA   57 OLD
L GA   51 NEW
Y GA   75 OLD
Y GA   87 NEW", header = TRUE)

ggplot(data = data, aes(x = br, y = ac, fill = met)) +
  geom_bar(stat = "identity", width = 0.8, position = position_dodge(width = 0.8)) +
  facet_wrap(~tr, strip.position = "bottom", scales = "free_x") +
  scale_y_continuous(breaks = seq(0, 100, 20)) +
  theme(panel.spacing = unit(0, "lines"),
        strip.background = element_blank(),
        strip.placement = "outside",
        axis.title.y = element_text(face = "bold"),
        axis.title.x = element_text(face = "bold"))

关键调整说明:

  • 柱状图样式修正:原代码默认生成堆叠柱状图,通过position = position_dodge(width = 0.8)改为目标图的并列分组样式,同时调整width参数优化柱子间距。
  • Y轴刻度补全:用scale_y_continuous(breaks = seq(0, 100, 20))手动指定Y轴刻度范围与间隔,从0到100、每20个单位显示一个刻度,确保所有层级完整展示。
  • 保留原分面与主题设置,维持分面标签底部无背景的样式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 12:03:21