[英]Putting percentage labels of a piechart in the legend using ggplot2
我使用 ggplot2 制作了以下餅圖。 我希望找到一個解決方案,我可以將百分比標簽放在圖例中每個項目旁邊的括號中,而不是在餅圖本身上?
有誰知道如何做到這一點?
這是我下面的代碼
country<-c('Botswana','Botswana','Botswana','Botswana','Botswana','Botswana','Botswana','Botswana')
key<-c('Retail Margin','Wholesale Margin','BFP Component and Related Costs','Customs and Excise Tax','MVA Levy',"Fuel Tax","Petroleum Fund Levy","Road User Charge")
value<-c(0.0687,0.0577,0.7393,0.0031,0.01,0.0126,0.0141,0.0945)
data <-data.frame(country,key,value)
botswana <- ggplot(data, aes("", value, fill = key)) +
geom_bar(stat = "identity", color = "white", size = 1) +
geom_text_repel(aes(label = paste0(value * 100, "%")),
position = position_stack(vjust = 0.5),
color = "black", size = 4) +
coord_polar(theta = "y") +
labs(colour = NULL) +
theme_void()
botswana <- botswana + theme(legend.title = element_blank())
print(botswana)
TIA
你可以試試這個:
country<-c('Botswana','Botswana','Botswana','Botswana','Botswana','Botswana','Botswana','Botswana')
key<-c('Retail Margin','Wholesale Margin','BFP Component and Related Costs','Customs and Excise Tax','MVA Levy',"Fuel Tax","Petroleum Fund Levy","Road User Charge")
value<-c(0.0687,0.0577,0.7393,0.0031,0.01,0.0126,0.0141,0.0945)
data <-data.frame(country,key,value)
#Create variable
data$key2 <- paste0(data$key,' [',100*round(data$value,2),'%',']')
botswana <- ggplot(data, aes("", value, fill = key2)) +
geom_bar(stat = "identity", color = "white", size = 1) +
coord_polar(theta = "y") +
labs(colour = NULL) +
theme_void()
botswana <- botswana + theme(legend.title = element_blank())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.