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

如何使用Base R缩小饼图标题与图表之间的间距?

解决Base R饼图标题与图表间距过大的问题

方法1:用title()单独设置标题并调整位置

直接在绘制饼图后,用title()函数重新添加标题,通过line参数精准控制标题与图表的距离:

# 绘制饼图时不指定main参数
pie(group3$count, labels = a, col = c("orange", "pink"))
# 设置标题,line参数为负数时会让标题向图表方向移动,可根据需求调整数值
title(
  main = "The percentage of Fuel type, among Low Emission cluster",
  cex.main = 1,
  line = -1  # 数值越小,标题越靠近饼图,可尝试-0.5、-1.5等
)
legend("bottomright", legend = c("DIESEL/ELECTRIC", "PETROL/ELECTRIC"), fill=c("pink","orange"))

方法2:调整绘图区域的顶部边距

通过par()函数修改mar参数(控制绘图区域上下左右边距,单位为lines),减小顶部边距来缩短标题与饼图的距离:

# 修改顶部边距(mar的第三个值),默认是c(5,4,4,2)+0.1,这里把顶部边距设为2
par(mar = c(4, 4, 2, 2))
# 绘制饼图并设置标题
pie(group3$count, labels = a, col = c("orange", "pink"), main= "The percentage of Fuel type, among Low Emission cluster", cex.main=1 )
legend("bottomright", legend = c("DIESEL/ELECTRIC", "PETROL/ELECTRIC"), fill=c("pink","orange"))
# 恢复默认边距,避免影响后续绘图
par(mar = c(5, 4, 4, 2) + 0.1)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 08:15:52