我想对类别进行编号和百分比显示,这是我的代码:
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] 删除。
我来说两句