向饼图中添加值和百分比 [英] Adding values and percentages to pie chart
问题描述
我正在尝试制作一个饼状图,以分解3个不同类别之间的值划分(是的,我知道堆积的条形图会更好,但是我对此没有选择).我想出了如何在堆叠的条形图上使用+ coord_polar制作饼图,并隐藏刻度/轴标签,但是我需要在饼图的每个切片中包括该实际值以及占总数的百分比,以及我不确定该怎么做.这是我正在使用的代码(正在使用的数据是机密的,因此我已将其替换为一些模拟数据).
库(ggplot2)simuldata<-data.frame(Category = LETTERS [1:3],值= c(196,149,127))饼图<-ggplot(simuldata,aes(x =",y = value,fill = Category))+geom_bar(width = 1,stat ="identity")+coord_polar("y",start = 0)+xlab(")+ylab("Value")+主题(axis.text.x = element_blank(),axis.ticks.x = element_blank())饼形图
如何将值和百分比添加到每个切片中?
这与 ggstatsplot
包函数 ggpiestats
((v0.2.1)于2018-10-11创建
I'm trying to make a pie chart breaking down the split in values between 3 different categories (And yes I know a stacked bar chart would be better, but I don't really have a choice in the matter). I figured out how to make the pie using +coord_polar on a stacked bar plot, and hide the ticks/axis labels, but I need to include that actual values, as well as percentage of total, within each slice of the pie chart, and I'm not sure how to do that. Here's the code I'm using (the data I'm working with is confidential, so I've replaced it with some simulated data).
library(ggplot2)
simuldata <- data.frame(Category = LETTERS[1:3],
value = c(196, 149,127))
piechart <- ggplot(simuldata, aes(x="", y=value, fill=Category)) +
geom_bar(width=1, stat="identity") +
coord_polar("y", start=0) +
xlab("") +
ylab("Value") +
theme(axis.text.x=element_blank(),axis.ticks.x=element_blank())
piechart
How would I go about adding the values and percentages to each slice?
This is super-straightforward with the ggstatsplot
package function ggpiestats
(https://cran.r-project.org/web/packages/ggstatsplot/vignettes/ggpiestats.html):
# data
simuldata <- data.frame(Category = LETTERS[1:3],
value = c(196, 149, 127))
# plot
ggstatsplot::ggpiestats(data = simuldata,
main = Category,
counts = value)
Created on 2018-10-11 by the reprex package (v0.2.1)
这篇关于向饼图中添加值和百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!