在饼图上显示百分比值 [英] Display percentage values on a pie chart
本文介绍了在饼图上显示百分比值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图用ggplot2绘制一个饼图。我的代码如下所示。
I am trying to draw a pie chart with ggplot2. My code is shown below.
df <- data.frame(
variable = c("australia","hungary","germany","france","canada"),
value = c(632,20,491,991,20)
)
ggplot(df, aes(x = "", y = value, fill = variable)) +
geom_bar(width = 1, stat = "identity") +
scale_fill_manual(values = c("red", "yellow","blue", "green", "cyan")) +
coord_polar(theta = "y") +
labs(title = "pie chart")
我想显示百分比值。我怎样才能做到这一点?
I would like to display percentage values. How can I do that?
推荐答案
试试
Try
df <- data.frame(
variable = c("australia","hungary","germany","france","canada"),
value = c(632,20,491,991,20)
)
library(ggplot2)
ggplot(transform(transform(df, value=value/sum(value)), labPos=cumsum(value)-value/2),
aes(x="", y = value, fill = variable)) +
geom_bar(width = 1, stat = "identity") +
scale_fill_manual(values = c("red", "yellow","blue", "green", "cyan")) +
coord_polar(theta = "y") +
labs(title = "pie chart") +
geom_text(aes(y=labPos, label=scales::percent(value)))
这篇关于在饼图上显示百分比值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文