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

如何控制ggplot中条形图的Z轴(深度)层级?

问题:制作PowerPoint风格的人时图(调整条形Z轴层级)

我想要制作一张与PowerPoint创建的人时图样式相似的图表,以下是包含前两人数据的示例数据框:

people <- 1:2
months <- -1:12
pt_df <- expand_grid(person = people, month = months)
rows_per_person <- length(months)

p01 <- rep("Diseased", rows_per_person)
p02 <- c(rep("Pre-follow-up", 2), rep("At Risk", 2), rep("Diseased", 3), rep("Deceased", 7))
status <- c(p01, p02)

pt_df$status <- status

pt_df <- pt_df |> 
  mutate(
    person_f = factor(person),
    status_f = factor(status, levels = c("Pre-follow-up", "At Risk", "Diseased", "Deceased"))
  )

我先后尝试了三种方法,但都没解决核心问题:

  • 创建堆叠条形图:使用position = "stack"会让条形在Y轴叠加,不符合需求;
  • 使用position_dodge(0)实现条形叠加:效果接近但需要调整层级,希望最长的"Deceased"条形在最底层,依次是"At Risk"和"Diseased";
  • 反转因子顺序:仅改变了图例顺序,条形的Z轴层级没有变化,还导致条形排列方向反转。因子顺序只影响图例和X轴,无法控制Z轴层级,恳请协助解决。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 15:05:56