【发布时间】:2021-01-14 11:56:58
【问题描述】:
我用 R 中的 ggplot 创建了一个饼图,绘制了 7 家公司(A 到 G)的“金额”。
数据和代码如下:
# Data
data=data.frame('Company'=(c("A","B","C","D","E","F","G")),'Amount'=c(30,20,10,5,5,2,1))
data=data %>% mutate(Company= factor(x = Company, levels = Company)) %>%
mutate(prop = Amount / sum(data$Amount) ) %>% mutate(ypos = cumsum(prop)- 0.5*prop )
# Pie chart
library(ggplot2)
ggplot(data, aes(x="", y=Amount, fill= Company) )+
geom_bar(width = 1, stat = "identity") + coord_polar("y", start=0,direction = -1) + theme_void() +
#geom_text(aes(label = percent(prop) ), size=3, position=position_stack(vjust=0.5)) +
labs(x = NULL, y = NULL, fill = NULL) + scale_fill_brewer(palette="Blues", direction=-1) +
geom_text(aes(label = percent(prop) ), size=3, position=position_stack(vjust=0.5))
但是,F 和 G 的标签相互堆叠,因此我们无法正确读取值。
我想将这些标签放在图例中,使图例显示为:A (41.1%) B (27.4%) C (13.7%) 等。
【问题讨论】:
标签: r ggplot2 legend pie-chart