如何在ggplot中向饼图添加数字和百分比

用户名

我想对类别进行编号和百分比显示,这是我的代码:

library(dplyr)
library(ggplot2)
library(ggrepel)
library(forcats)
library(scales)

figvad <- read.csv(url("https://raw.githubusercontent.com/learnseq/learning/main/vadev.txt"),sep = '\t',header = TRUE)

library(repr, warn.conflicts = FALSE)
options(repr.plot.width=17, repr.plot.height=10)

ggplot(figvadf, aes(x=Type, fill=Type)) + 
  geom_bar(width = 0.5) +
  coord_polar()+
theme_minimal()+
 theme(text = element_text(size=14, face=4),plot.margin = unit(c(1,1,1,3), "lines"))+
xlab("Types of Vaccines")+
ylab("")

我尝试过,但是没有成功:

geom_text(aes(label = paste(round(factor(Type  / sum(factor(Type) * 100, 1), "%"))),
            position = position_stack(vjust = 0.5))

在此处输入图片说明

艾伦·卡梅伦

您是否正在寻找这样的东西?

library(ggplot2)
library(dplyr)
library(reshape2)

figvad <- read.csv(paste0("https://raw.githubusercontent.com/learnseq",
                           "/learning/main/vadev.txt"), sep = '\t')

ggplot(melt(table(figvad$Type)) %>% mutate(perc = value/sum(value)),
       aes(1, value, fill=Var1)) + 
  geom_col(position = "stack", color = "gray20", alpha = 0.8) +
  geom_text(aes(1.6, label = paste0(Var1, "\n", value, " (", 
                                    scales::percent(perc), ")")),
             hjust = "outward",
            position = position_stack(vjust = 0.5)) +
  scale_fill_brewer(palette = "RdYlGn", name = "Type") +
  coord_polar(theta = "y", clip = "off") +
  theme_void() +
  theme(legend.position = "none")


编辑

ggplot(melt(table(figvad$Type)) %>% 
         mutate(perc = value/sum(value)) %>%
         mutate(label = paste0(Var1, "\n", value, " (", 
                               scales::percent(perc), ")")),
       aes(label, value, fill = label)) + 
  geom_col(position = "stack", color = "gray20", alpha = 0.8, width = 0.5) +
  scale_fill_brewer(palette = "RdYlGn", name = "Type") +
  coord_polar(theta = "x", clip = "off") +
  theme_minimal()+
  theme(text = element_text(size = 14, face = 4),
        plot.margin = unit(c(1, 1, 1, 3), "lines"),
        legend.position = "none",
        axis.text.y = element_blank()) +
  xlab("Types of Vaccines") +
  ylab("")

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Google饼图图例上添加百分比和总计

如何在SSRS饼图图例中显示百分比和值?

如何在R中的ggplot2中向条形图方面添加百分比?

如何在ggplot2中向有序条形图添加频率百分比

向饼图中添加值和百分比

ChartJS 版本 3 如何向饼图工具提示添加百分比

如何在C#中向柱形图顶部添加百分比

ggplot2 中的饼图和百分比

如何将百分比标签移到 ggplot2 中的饼图之外?

如何在饼图中添加百分比符号?

如何在Spotfire中为“是”和“否”答案创建饼图,而不显示“是/否”百分比?

如何在ggplot geom_text标签中添加百分比和分数?

为饼图添加百分比 (%)

Matplotlib 饼图:显示值和百分比

ggplot2中条形图的计数和百分比如何?[R

如何在角度2/8中使用chartjs-plugin-labels(饼图)显示百分比(%)

如何在 Python 中向 matplotlib.annotate 添加百分比符号?

饼图-如何在正确的位置获取百分比文本?

删除饼图图例(amcharts4)中名称和百分比之间的空格

在R中的饼图上添加百分比标签

如何在 Plotly 饼图中以数字而不是百分比显示值?

如何获取nvd3饼图工具提示中的百分比?

如何在ssrs中添加百分比列

在散点图中的每个饼图中添加饼图比例的百分比

如何在css和html中的百分比栏上添加标签?

如何在ggplot2中将字符对象添加到数字标签:在条形图中将%符号添加到百分比标签

带有ggplot2的饼图,带有特定的顺序和百分比注释

具有标签和饼图上的百分比的chartist.js饼图

使用带有计数和百分比的 Plotly 在 R 中打开饼图/甜甜圈图