1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
GetStrategyPiePlot <- function(data, interval, exchina){
if(interval){
aggr_orders = data
} else {
aggr_orders = data %>% filter(Month == current_month & Year == lastYearPiechart)
}
if(exchina){
aggr_orders = aggr_orders %>% filter(Country != "China")
}
aggr_orders = aggr_orders %>%
select(StrategyName, ExecPrice, ExecQty, Forex) %>%
mutate(Turnover = ExecPrice * ExecQty * Forex,
Total = sum(Turnover, na.rm=T)) %>%
group_by(StrategyName) %>%
summarise(StrategyTurnover = sum(Turnover, na.rm=T),
Total = first(Total),
PctStrategy = (100*StrategyTurnover/Total)) %>%
select(Strategy, PctStrategy) %>%
filter(PctStrategy >= 1*1e-2)
aggr_orders$Label = paste(aggr_orders$StrategyName, round(aggr_orders$PctStrategy, 2), sep=" ")
aggr_orders$Label = paste(aggr_order$Label, "%", sep="")
aggr_orders = with(aggr_orders, aggr_orders[order(PctStrategy, decreasing = T),])
aggr_orders$Label <- factor(aggr_orders$Label, levels=unique(as.character(aggr_orders$Label)))
r <- ggplot(aggr_orders)
r <- r + geom_bar(aes(x = "", y=PctStrategy, fill=factor(Label)), width = 1, stat="identity", color="white")
r <- r + coord_polar(theta = "y")
r <- r + theme_bw()
r <- r + theme(axis.text = element.blank(),
axis.title = element.blank(),
panel.grid = element.blank(),
panel.border = element.blank())
r <- r + guides(fill=guide_legend(title="Strategies"))
if(interval){
r <- r + labs(title="Strategy Breakdown")
} else {
r <- r + labs(title=paste(month.name[current_month], lastYearPieChart, "Strategy Breakdown", sep=" "))
}
gr = arrangeGrob(r)
return(gr)
} |
Partager