R/distributions.R

Defines functions getFundsByBenchmarkInfoTable plotFundsCountByBenchmarkBar plotFundsAUMByBenchmarkBar plotFundsCountByBenchmarkPie plotFundsAUMByBenchmarkPie plotFundsCountByBenchmarkBar plotFundsAUMByBenchmarkBar plotFundsCountByBenchmarkPie plotFundsAUMByBenchmarkPie plotUniverseByBrandName

#' Add together two numbers.
#'
#' @param x A number.
#' @param y A number.
#' @return The sum of \code{x} and \code{y}.
#' @examples
#' add(1, 1)
#' add(10, 1)
#' @export
getFundsByBenchmarkInfoTable <- function(universe, benchmarkKeywords) {
  MSCI <- universe %>% filter(grepl(msciKeyword,Primary.Prospectus.Benchmark))
  FTSE <- universe %>% filter(grepl(ftseKeyword,Primary.Prospectus.Benchmark))
  STOXX <- universe %>% filter(grepl(stoxxKeyword,Primary.Prospectus.Benchmark))
  SPDJ <- universe %>% filter(grepl(spdjKeyword,Primary.Prospectus.Benchmark))

  fundsCount <- c(length(MSCI$Name),length(FTSE$Name),length(STOXX$Name),length(SPDJ$Name))
  fundsPercent <- fundsCount/sum(fundsCount)
  AUM <- c(sumF(MSCI$Fund.Size.USD),sumF(FTSE$Fund.Size.USD),sumF(STOXX$Fund.Size.USD),sumF(SPDJ$Fund.Size.USD))
  countTable <- data.frame(provider=provider, fundsCount=fundsCount, fundsPercent=fundsPercent, AUM=AUM)
}

plotFundsCountByBenchmarkBar <- function(fundInfoTable) {
  countTable %>%
    ggplot(aes(x=provider, y=fundsCount, fill=provider)) +
    geom_bar(stat="identity") +
    geom_label(fill='white',aes(label=toP(fundsCount,fundsPercent)),fill='white') +
    fillTheme
}

plotFundsAUMByBenchmarkBar <- function(fundInfoTable) {
  ggplot(aes(x=provider, y=AUM, fill=provider)) +
    geom_bar(stat="identity") +
    geom_label(fill='white',aes(label=toP(paste(as.character(round(AUM/1000000000, digits=2)),"B", sep=""), AUM/sum(AUM)))) +
    fillTheme
}

plotFundsCountByBenchmarkPie <- function(fundInfoTable) {
  countTable %>%
    ggplot(aes(x=factor(1), y=fundsPercent, fill=provider)) +
    geom_bar(width=1, stat="identity") +
    coord_polar(theta="y") +
    theme(axis.text=element_blank(), axis.title=element_blank(), panel.background=element_blank()) +
    fillTheme
}


plotFundsAUMByBenchmarkPie <- function(fundInfoTable) {
  countTable %>%
    ggplot(aes(x=factor(1), y=AUM/sum(AUM), fill=provider)) +
    geom_bar(width=1, stat="identity") +
    coord_polar(theta="y") +
    theme(axis.text=element_blank(), axis.title=element_blank(), panel.background=element_blank()) +
    fillTheme
}


plotFundsCountByBenchmarkBar <- function(universe, benchmarkKeywords) {
  countTable %>%
    ggplot(aes(x=provider, y=fundsCount, fill=provider)) +
    geom_bar(stat="identity") +
    geom_label(fill='white',aes(label=toP(fundsCount,fundsPercent)),fill='white') +
    fillTheme
}

plotFundsAUMByBenchmarkBar <- function(universe, benchmarkKeywords) {
  ggplot(aes(x=provider, y=AUM, fill=provider)) +
    geom_bar(stat="identity") +
    geom_label(fill='white',aes(label=toP(paste(as.character(round(AUM/1000000000, digits=2)),"B", sep=""), AUM/sum(AUM)))) +
    fillTheme
}

plotFundsCountByBenchmarkPie <- function(universe, benchmarkKeywords) {
  countTable %>%
    ggplot(aes(x=factor(1), y=fundsPercent, fill=provider)) +
    geom_bar(width=1, stat="identity") +
    coord_polar(theta="y") +
    theme(axis.text=element_blank(), axis.title=element_blank(), panel.background=element_blank()) +
    fillTheme
}


plotFundsAUMByBenchmarkPie <- function(universe, benchmarkKeywords) {
  countTable %>%
    ggplot(aes(x=factor(1), y=AUM/sum(AUM), fill=provider)) +
    geom_bar(width=1, stat="identity") +
    coord_polar(theta="y") +
    theme(axis.text=element_blank(), axis.title=element_blank(), panel.background=element_blank()) +
    fillTheme
}


#' Add together two numbers.
#'
#' @param x A number.
#' @param y A number.
#' @return The sum of \code{x} and \code{y}.
#' @examples
#' add(1, 1)
#' add(10, 1)
plotUniverseByBrandName <- function(universe, floor=5) {
  universe %>%
    group_by(Branding.Name) %>%
    summarize(count=n()) %>%
    filter(count >= floor) %>%
    ggplot(aes(x=Branding.Name, y=count, fill=Branding.Name)) +
    geom_bar(stat="identity") +
    geom_label(fill='white',aes(label=count)) +
    theme(axis.text.x = element_text(angle = 45, hjust = 1))
}
yezhaoqin/morningStarAnalytics documentation built on May 4, 2019, 2:32 p.m.