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

使用geom_arc_bar绘制饼图报错:找不到该函数的求助

解决geom_arc_bar()找不到函数的问题

这个报错原因很明确:geom_arc_bar()不是ggplot2自带的函数,它属于ggforce包。之前代码能正常运行是因为当时你的R环境里加载了ggforce,现在重启会话或者knit时启用了新会话,没加载这个包就会触发报错。

解决步骤:

  • 第一步:如果还没安装ggforce包,先执行安装命令:
install.packages("ggforce")
  • 第二步:在绘制饼图的代码开头,加载ggforce包:
library(ggforce)

修正后的完整可运行代码:

library(tidyverse)
library(ggforce) # 必须加载该包才能调用geom_arc_bar()

pie_data %>% 
    ggplot() +
    aes(
        x0 = 0, y0 = 0, r0 = 0.4, r = 1,
        start = start_angle, end = end_angle,
        fill = city
    ) +
    geom_arc_bar() +
    geom_text(
        aes(
            x = 1.05 * sin(mid_angle),
            y = 1.05 * cos(mid_angle),
            label = city,
            hjust = hjust, vjust = vjust
        )
    ) +
    coord_fixed(
        xlim = c(-1.5, 1.65),
        ylim = c(-1.25, 1.25)
    ) +
    labs(
        title = "Houses Sold in Texas in 2015"
    ) +
    xlab(NULL) +
    ylab(NULL) +
    theme(legend.position="none") +
    scale_fill_manual(values = c(
                                 `San Antonio` = "#fbb4ae", Austin = "#b3cde3",
                                 Dallas = "#ccebc5", Houston = "#decbe4",
                                 Other = "#fed9a6"
                               )
                     )

额外提醒:knit文档时,所有用到的第三方包都要在代码开头用library()加载,因为knit会启动全新的R会话,不会继承之前会话中加载的包。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 23:07:40