首页 > 解决方案 > 如何用因子制作饼图

问题描述

我想使用 ggplot 制作带有因子列的饼图,我想为每个显示百分比、频率和试用阶段:这是我的代码:

library(ggplot2)
library(ggrepel)
library(dplyr)
library(cowplot)


#Retrieve data 
figvac <- read.csv(url("https://raw.githubusercontent.com/learnseq/learning/main/vaccinedev.txt"),sep = '\t',header = TRUE)

library(repr, warn.conflicts = FALSE)
options(repr.plot.width=17, repr.plot.height=10)
ggplot(figvac, aes(x="", fill=factor(figvac[, 2] ))) + geom_bar(width = 1) + coord_polar("y", start=0)

我试过了

geom_text(aes(label = paste(round(factor(figvac[, 2]  / sum(factor(figvac[, 2]) * 100, 1), "%"))),
            position = position_stack(vjust = 0.5)) +
  theme_classic() +
  theme(plot.title = element_text(hjust=0.5),
        axis.line = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank()) +
  labs(fill = "Category",
       x = NULL,
       y = NULL,
       title = "Pie Chart of Vaccines") + 
  coord_polar("y")

但它没有用。

标签: rggplot2

解决方案


你要fill=factor(Mechanism)


推荐阅读