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

ggplot2饼图标题置底问题:vjust参数无效,求规范解决方案

解决ggplot2饼图标题无法移至底部的规范方法

问题根源:使用coord_polar()配合theme_void()后,绘图的坐标系统和边距逻辑发生改变,vjust的参考基准不再是常规的绘图区域,导致小数值无法将标题推至底部。以下是两种规范的解决方式:

方式一:改用plot.caption放置标题(推荐)

theme_void()默认会将caption置于底部,只需调整样式和边距即可:

library(tidyverse)    
pie_tbl <- tibble(
  surface = factor(c("Dry","Wet","Snow/Ice","Other"),levels=c("Dry","Wet","Snow/Ice","Other")),
  percentage = c(78,14.8,6.9,0.2)
)

pie_tbl %>% ggplot(aes(x="",y=percentage,fill=surface)) + 
  geom_col(color='black',width=1) + 
  coord_polar("y",start=pi/2) +
  labs(caption="Surface Condition during collisions") +  # 用caption替代title
  theme_void() +
  theme(
    plot.caption = element_text(family='serif',face='bold',size=18,hjust=0.5),
    plot.margin = margin(b = 20)  # 给底部预留空间,避免标题被裁切
  ) +
  scale_fill_manual(
    values=c(
      "Dry" = "#27B7CE",
      "Wet" = "#FB6476",
      "Snow/Ice" = "#47B972",
      "Other" = "#FEB20E"
    )
  )

方式二:保留title,用margin参数控制位置

如果必须使用title,可以通过element_text()的margin参数将标题向下推送,同时调整绘图边距:

library(tidyverse)    
pie_tbl <- tibble(
  surface = factor(c("Dry","Wet","Snow/Ice","Other"),levels=c("Dry","Wet","Snow/Ice","Other")),
  percentage = c(78,14.8,6.9,0.2)
)

pie_tbl %>% ggplot(aes(x="",y=percentage,fill=surface)) + 
  geom_col(color='black',width=1) + 
  coord_polar("y",start=pi/2) +
  labs(title="Surface Condition during collisions") +
  theme_void() +
  theme(
    plot.title = element_text(
      family='serif',face='bold',size=18,hjust=0.5,
      margin = margin(b = 100)  # 添加底部外边距,将标题推至下方
    ),
    plot.margin = margin(b = 20)  # 确保底部有足够空间显示标题
  ) +
  scale_fill_manual(
    values=c(
      "Dry" = "#27B7CE",
      "Wet" = "#FB6476",
      "Snow/Ice" = "#47B972",
      "Other" = "#FEB20E"
    )
  )

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 19:15:08