简体   繁体   English

R - 如何向 ggplot2 饼图图例添加百分比?

[英]R - How to add percentages to ggplot2 pie chart legend?

I would like to add percentages (rounded to 1 decimal) to my legend.我想在图例中添加百分比(四舍五入到小数点后一位)。 For example, "MSFT 8.3%".例如,“MSFT 8.3%”。 I don't want them in the pie chart since it looks weird when theres too many slices with small % values.我不希望它们出现在饼图中,因为当有太多具有小 % 值的切片时,它看起来很奇怪。

Here is my code:这是我的代码:

library(ggplot2)

tickers <- c("msft","xic.to","fb","aapl","goog","nflx","aal","bmo","xef.to","xec.to","vsp.to","mcd")
Weights <- rep(1/12*100,12)

tickers <- tickers[order(Weights)]; Weights <- sort(Weights)
tickers.factor <- factor(tickers, levels = as.character(tickers))

ypos <- cumsum(Weights) - 0.5*Weights
ypos <- 100 - ypos

ggplot() + theme_bw() +
  geom_bar(aes(x = "", y = Weights, fill = tickers.factor),
           stat = "identity", color = "white") + 
  coord_polar("y", start = 0) +
  ggtitle("Portfolio Weights") +
  theme(plot.title = element_text(hjust = 0.5, size = 20),
        axis.title = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank(),
        panel.grid = element_blank(),
        panel.border = element_blank()) +
  guides(fill = guide_legend(reverse = TRUE)) + 
  #scale_fill_brewer(palette = "PiYG", name = "Tickers") +
  theme(legend.text = element_text(size = 12),
        legend.title = element_text(hjust = 0.5, size = 15),
        legend.key.size = unit(0.8,"cm")) 

Thank you谢谢

I've used scales::percent to format the percentages.我使用 scales::percent 来格式化百分比。



library(ggplot2)

tickers <- c("msft","xic.to","fb","aapl","goog","nflx","aal","bmo","xef.to","xec.to","vsp.to","mcd")
Weights <- rmultinom(n = 1, size = 100, prob = rep(1/length(tickers), length(tickers)))

tickers <- tickers[order(Weights)]; Weights <- sort(Weights)
Percent <- factor(paste(tickers, scales::percent(Weights/100, accuracy = 0.01)), paste(tickers, scales::percent(Weights/100, accuracy = 0.01)))

ypos <- cumsum(Weights) - 0.5*Weights
ypos <- 100 - ypos

ggplot() + theme_bw() +
  geom_bar(aes(x = "", y = Weights, fill = Percent),
           stat = "identity", color = "white") + 
  coord_polar("y", start = 0) +
  ggtitle("Portfolio Weights") +
  theme(plot.title = element_text(hjust = 0.5, size = 20),
        axis.title = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank(),
        panel.grid = element_blank(),
        panel.border = element_blank()) +
  guides(fill = guide_legend(reverse = TRUE)) + 
  #scale_fill_brewer(palette = "PiYG", name = "Tickers") +
  theme(legend.text = element_text(size = 12),
        legend.title = element_text(hjust = 0.5, size = 15),
        legend.key.size = unit(0.8,"cm")) 

I would like to add percentages (rounded to 1 decimal) to my legend.我想在我的图例中添加百分比(四舍五入到小数点后 1 位)。 For example, "MSFT 8.3%".例如,“MSFT 8.3%”。 I don't want them in the pie chart since it looks weird when theres too many slices with small % values.我不希望它们出现在饼图中,因为当切片太多且 % 值较小时,它看起来很奇怪。

Here is my code:这是我的代码:

library(ggplot2)

tickers <- c("msft","xic.to","fb","aapl","goog","nflx","aal","bmo","xef.to","xec.to","vsp.to","mcd")
Weights <- rep(1/12*100,12)

tickers <- tickers[order(Weights)]; Weights <- sort(Weights)
tickers.factor <- factor(tickers, levels = as.character(tickers))

ypos <- cumsum(Weights) - 0.5*Weights
ypos <- 100 - ypos

ggplot() + theme_bw() +
  geom_bar(aes(x = "", y = Weights, fill = tickers.factor),
           stat = "identity", color = "white") + 
  coord_polar("y", start = 0) +
  ggtitle("Portfolio Weights") +
  theme(plot.title = element_text(hjust = 0.5, size = 20),
        axis.title = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank(),
        panel.grid = element_blank(),
        panel.border = element_blank()) +
  guides(fill = guide_legend(reverse = TRUE)) + 
  #scale_fill_brewer(palette = "PiYG", name = "Tickers") +
  theme(legend.text = element_text(size = 12),
        legend.title = element_text(hjust = 0.5, size = 15),
        legend.key.size = unit(0.8,"cm")) 

Thank you谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM