我是R的新手,我正在try 使用geom_arcbar()创建一个饼图,但我遇到了这个间歇性错误.

Error in geom_arc_bar() : could not find function "geom_arc_bar"

我将数据集汇总到一个表中,该表包含帮助我设置饼图的所有信息:

> pie_data
# A tibble: 5 × 7
  city        sales end_angle start_angle mid_angle hjust vjust
  <chr>       <dbl>     <dbl>       <dbl>     <dbl> <dbl> <dbl>
1 San Antonio  1485     0.500       0         0.250     0     0
2 Austin       1656     1.06        0.500     0.778     0     0
3 Dallas       3066     2.09        1.06      1.57      0     1
4 Houston      4494     3.60        2.09      2.84      0     1
5 Other        7971     6.28        3.60      4.94      1     0

这是我的代码导致的错误:

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"
                               )
                     )

一周前所有的线路都工作正常,但今天我重新打开它时,由于那个错误,它不能编织.不知道该怎么办..提前感谢您的帮助!

推荐答案

geom_arc_bar()函数来自ggforce package;一旦加载了所需的库(用于绘图的ggplot2、用于%>%的dplyr和用于geom_arc_bar()的ggforce),示例看起来就像预期的那样工作:

library(ggplot2)
library(dplyr)
library(ggforce)

pie_data <- read.table(text = "  city        sales end_angle start_angle mid_angle hjust vjust
'San Antonio'  1485     0.500       0         0.250     0     0
Austin       1656     1.06        0.500     0.778     0     0
Dallas       3066     2.09        1.06      1.57      0     1
Houston      4494     3.60        2.09      2.84      0     1
Other        7971     6.28        3.60      4.94      1     0", header = TRUE)

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"
  )
  )

创建于2024-02-19年第reprex v2.1.0

这能解决你的问题吗?

R相关问答推荐

r中的stat_difference函数不起作用

如何在modelsummary中重命名统计数据?

如何改变时间图R中的悬停信息?

如何在Chart_Series()中更改轴值的 colored颜色 ?

R中插入符号训练函数的中心因子和尺度因子预测

移除仪表板Quarto中顶盖和车身之间的白色区域

如果可能,将数字列转换为整数,否则保留为数字

传递ggplot2的变量作为函数参数—没有映射级别以正确填充美学

如何删除仅在数据集顶部和底部包含零的行

用两种 colored颜色 填充方框图

跨列查找多个时间报告

`夹心::vcovCL`不等于`AER::tobit`标准错误

正在导出默认的RStudio主题,还是设置括号 colored颜色 ?

查找所有站点的最小值

如何从向量构造一系列双边公式

创建在文本字符串中发现两个不同关键字的实例的数据框

如何为混合模型输出绘制不同的线型?

快速合并R内的值

重写时间间隔模糊连接以减少内存消耗

把代码写成dplyr中的group_by/摘要更简洁吗?