r - 如何用因子制作饼图
问题描述
我想使用 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")
但它没有用。
解决方案
你要fill=factor(Mechanism)
推荐阅读
- user-interface - 在 Vaadin 应用程序中为 Link 创建的新页面添加按钮
- javascript - javascript中只有一个反斜杠
- java - OAuth2RestTemplate.postForEntity RestClientException:[org.x.xx] 和内容类型 [application/json] 没有 HttpMessageConverter
- javascript - 切换关闭除单击之外的所有内容
- c# - IDX10501:签名验证失败。孩子:'[PII 已隐藏]',令牌:'[PII 已隐藏]' - Azure B2C
- salesforce - Window.close 在页面中不起作用参考 Apex 方法
- java - 消费者组重复读取主题中的相同消息
- c# - 我可以将 Microsoft Office PartnerCenter SDK 用于客户管理员级别而不是 CSP 级别吗?
- sql - 使用来自其他表的属性以及新属性创建表
- python - 在 Tkinter 中绑定到一个键的“移动对象”功能一次只能使一个对象移动,如何使多个对象同时移动?