R/easy_pie.R

Defines functions easy_bar easy_pie

Documented in easy_pie

#' easy_pie
#'
#' Make a nice pie chart
#' @param df Your data frame
#' @param title The chart's title
#' @param lab The label
#' @param fill What variable you will use to fill the pie parts
#' @param value The value column that the pie chart is built from
#' @export

easy_pie <- function(df,title,lab,fill,value,fill_colors =c('#FFCC33','#C7C7C7')) {

  require(tidyverse)
  require(ggrepel)
  require(neugelbdesign)
  require(purrr)

labs <- str_to_title(lab)

  x <- df %>%
    ggplot(aes(x="",y=df[[value]],fill=df[[fill]])) +
    geom_bar(stat='identity',width = 1)+
    coord_polar('y',start=0)+
    labs(fill=lab)+
    ggtitle(title)+
    theme_neugelb()+
    theme(legend.position='right',
          axis.text.y = element_blank(),
          axis.text.x = element_blank())+
    geom_label_repel(aes(label=paste(round(df[[value]]*100,1),"%",sep="")),size=3.5,show.legend = FALSE,nudge_x = 1,family='Gotham Book')

if (is_empty(fill_colors)==FALSE) {

  x <- x +
    scale_fill_manual(values = fill_colors)

}

  return(x)
}

easy_bar <- function(df,x_axis,y_axis,lab,fill,title,facet_wrap=NULL,ylab = NULL,xlab = NULL,yaxis=TRUE,xaxis=TRUE,legend=TRUE) {

  require(tidyverse)
  require(purrr)
  require(scales)

  labs <- str_to_title(lab)

  x <- df %>%
    ggplot(aes(x= df[[x_axis]],y = df[[y_axis]],fill = df[[fill]])) +
    geom_bar(stat='identity',width = 1,position='dodge')+
    labs(fill = lab)+
    ggtitle(title)+
    theme_neugelb()+
    theme(legend.position='bottom')+
    scale_y_continuous(labels = percent)+
    six_colors_fill()+
    geom_text(aes(label=paste(round(df[[y_axis]]*100,digits = 2),'%',sep=''),family='Gotham Book'),col='black',vjust=-.5,size=3)+
    ylab(ylab)+
    xlab(xlab)+
    facet_wrap(. ~ df[[facet_wrap]])

if (xaxis == FALSE) {

x <- x +
  theme(axis.text.x = element_blank())

}

if (yaxis == FALSE) {

  x <- x +
    theme(axis.text.y = element_blank())
}

if (legend == FALSE) {

  x <- x +
    theme(legend.position = 'none')
}

  return(x)

  }
neugelb/neugelbtools documentation built on July 7, 2020, 1:17 a.m.