├── .Rbuildignore ├── .gitignore ├── DESCRIPTION ├── LICENSE ├── LICENSE.md ├── NAMESPACE ├── R ├── calculate.R ├── data.R ├── helpers.R ├── plot_bars.R ├── plot_boxplot.R ├── plot_correlation.R ├── plot_deciles.R ├── plot_density.R ├── plot_lines.R ├── tweak.R ├── utils.R └── zzz.R ├── README.Rmd ├── README.md ├── data └── diamonds.rda ├── ggrapid.Rproj ├── inst └── extdata │ ├── ggtesting │ └── plot_bars_examples.R ├── man ├── apply_theme.Rd ├── calculate_decile_table.Rd ├── diamonds.Rd ├── figures │ ├── unnamed-chunk-10-1.png │ ├── unnamed-chunk-11-1.png │ ├── unnamed-chunk-12-1.png │ ├── unnamed-chunk-16-1.png │ ├── unnamed-chunk-17-1.png │ ├── unnamed-chunk-18-1.png │ ├── unnamed-chunk-20-1.png │ ├── unnamed-chunk-21-1.png │ ├── unnamed-chunk-22-1.png │ ├── unnamed-chunk-3-1.png │ ├── unnamed-chunk-4-1.png │ ├── unnamed-chunk-5-1.png │ ├── unnamed-chunk-6-1.png │ ├── unnamed-chunk-7-1.png │ ├── unnamed-chunk-8-1.png │ └── unnamed-chunk-9-1.png ├── first_to_lower.Rd ├── first_to_upper.Rd ├── number_ticks.Rd ├── pipe.Rd ├── plot_bars.Rd ├── plot_boxplot.Rd ├── plot_correlation.Rd ├── plot_deciles.Rd ├── plot_density.Rd ├── plot_line.Rd └── select_palette.Rd └── tests ├── testthat.R └── testthat ├── test_calculate.R ├── test_helpers.R ├── test_plot_density.R └── test_tweak.R /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^LICENSE\.md$ 2 | ^.*\.Rproj$ 3 | ^\.Rproj\.user$ 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | .Ruserdata 5 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: ggrapid 2 | Title: Create neat & complete ggplot visualizations with as little code as possible 3 | Version: 0.0.1.0 4 | Authors@R: person("Konrad", "Semsch", email = "konrad.semsch@gmail.com", role = c("aut", "cre")) 5 | Description: The ggrapid package enables creation of the most common ggplot-based visualizations fast and with just a few lines of code. 6 | License: MIT + file LICENSE 7 | Encoding: UTF-8 8 | LazyData: true 9 | RoxygenNote: 6.1.1 10 | Depends: 11 | R (>= 3.6.0) 12 | Imports: 13 | corrplot, 14 | dplyr, 15 | extrafont, 16 | forcats, 17 | formattable, 18 | ggplot2, 19 | glue, 20 | grDevices, 21 | hrbrthemes, 22 | magrittr, 23 | paletteer, 24 | rlang, 25 | scales, 26 | stats, 27 | tidyr 28 | Suggests: 29 | testthat, 30 | tidyverse, 31 | vdiffr 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | YEAR: 2019 2 | COPYRIGHT HOLDER: Konrad Semsch 3 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # MIT License 2 | 3 | Copyright (c) 2019 Konrad Semsch 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | export("%>%") 4 | export(apply_theme) 5 | export(calculate_decile_table) 6 | export(first_to_lower) 7 | export(first_to_upper) 8 | export(plot_bars) 9 | export(plot_boxplot) 10 | export(plot_correlation) 11 | export(plot_deciles) 12 | export(plot_density) 13 | export(plot_line) 14 | export(select_palette) 15 | import(dplyr) 16 | importFrom(ggplot2,"%+replace%") 17 | importFrom(ggplot2,aes) 18 | importFrom(ggplot2,coord_cartesian) 19 | importFrom(ggplot2,element_blank) 20 | importFrom(ggplot2,element_text) 21 | importFrom(ggplot2,facet_wrap) 22 | importFrom(ggplot2,geom_bar) 23 | importFrom(ggplot2,geom_boxplot) 24 | importFrom(ggplot2,geom_density) 25 | importFrom(ggplot2,geom_histogram) 26 | importFrom(ggplot2,geom_hline) 27 | importFrom(ggplot2,geom_line) 28 | importFrom(ggplot2,geom_point) 29 | importFrom(ggplot2,geom_text) 30 | importFrom(ggplot2,geom_vline) 31 | importFrom(ggplot2,ggplot) 32 | importFrom(ggplot2,ggtitle) 33 | importFrom(ggplot2,labs) 34 | importFrom(ggplot2,position_dodge) 35 | importFrom(ggplot2,rel) 36 | importFrom(ggplot2,scale_fill_gradientn) 37 | importFrom(ggplot2,scale_fill_manual) 38 | importFrom(ggplot2,scale_x_continuous) 39 | importFrom(ggplot2,scale_y_continuous) 40 | importFrom(ggplot2,theme) 41 | importFrom(ggplot2,xlim) 42 | importFrom(magrittr,"%<>%") 43 | importFrom(magrittr,"%>%") 44 | importFrom(rlang,":=") 45 | importFrom(rlang,.data) 46 | importFrom(stats,median) 47 | -------------------------------------------------------------------------------- /R/calculate.R: -------------------------------------------------------------------------------- 1 | 2 | # Calculate decile table -------------------------------------------------- 3 | 4 | #' Calculate a decile breakdown 5 | #' 6 | #' This function calculates a decile table for any combination of numerical and categorical variables. 7 | #' 8 | #' @param df A data frame. 9 | #' @param binning Variable for which binning should be applied. 10 | #' @param grouping A two-level (binary) variable to calculate the ratio in each bin. 11 | #' @param top_level Top level of the grouping variable. Defaults to "1". 12 | #' @param n_bins Provide a number of bins. Defaults to 10. 13 | #' @param format Should table printing be formatted with kable? Defaults to FALSE 14 | #' @param ... Additional grouping columns to calculate deciles 15 | #' @examples 16 | #' library(tidyverse) 17 | #' 18 | #' diamonds_filter <- diamonds %>% filter(cut %in% c("Ideal", "Premium")) 19 | #' 20 | #' diamonds_filter %>% 21 | #' calculate_decile_table(price, cut, "Ideal") 22 | #' 23 | #' diamonds_filter %>% 24 | #' calculate_decile_table(binning = price, 25 | #' grouping = cut, 26 | #' top_level = "Ideal", 27 | #' n_bins = 10, 28 | #' format = FALSE, 29 | #' color) 30 | #' @import dplyr 31 | #' @importFrom magrittr %>% 32 | #' @importFrom magrittr %<>% 33 | #' @importFrom rlang .data 34 | #' @importFrom stats median 35 | #' @export 36 | calculate_decile_table <- function(df, 37 | binning, 38 | grouping, 39 | top_level = "1", 40 | n_bins = 10, 41 | format = FALSE, 42 | ...) { 43 | 44 | if (!is.data.frame(df)) 45 | stop("object must be a data frame") 46 | 47 | if (!is.character(top_level)) 48 | stop("argument must be character") 49 | 50 | if (!is.numeric(n_bins)) 51 | stop("argument must be numeric") 52 | 53 | var_binning <- rlang::enquo(binning) 54 | var_grouping <- rlang::enquo(grouping) 55 | 56 | var_extra_grouping <- rlang::enquos(...) 57 | 58 | params <- list(na.rm = TRUE) 59 | 60 | df_out <- df %>% 61 | tidyr::drop_na(!!var_binning) %>% 62 | mutate( 63 | decile = as.factor(ntile(!!var_binning, n_bins)), 64 | grouping_chr = as.character(!!var_grouping) 65 | ) %>% 66 | group_by(!!!var_extra_grouping, .data$decile) %>% 67 | summarize( 68 | min = round(min(!!var_binning, !!!params), 3), 69 | median = round(median(!!var_binning, !!!params), 3), 70 | max = round(max(!!var_binning, !!!params), 3), 71 | top_level = sum(.data$grouping_chr == top_level), 72 | total = n(), 73 | bottom_level = .data$total - .data$top_level, 74 | ratio = formattable::percent(.data$top_level / .data$total, 2) 75 | ) %>% 76 | ungroup() %>% 77 | mutate_at(vars(one_of(c("min", "median", "max"))), round, 2) 78 | 79 | if (format == TRUE) { 80 | df_out %<>% 81 | mutate_at(vars(.data$ratio), ~formattable::color_tile("white", "red")(.x)) 82 | } 83 | 84 | return(df_out) 85 | 86 | } 87 | -------------------------------------------------------------------------------- /R/data.R: -------------------------------------------------------------------------------- 1 | #' Diamonds dataset used for examples 2 | #' 3 | #' See \code{\link[ggplot2]{diamonds}} for details. 4 | #' 5 | "diamonds" 6 | -------------------------------------------------------------------------------- /R/helpers.R: -------------------------------------------------------------------------------- 1 | 2 | # Number ticks ------------------------------------------------------------ 3 | 4 | #' Specify equal sized axis ticks 5 | #' 6 | #' This function creates equally spaced axis ticks for ggplot graphs. Should be used as input 7 | #' for the "break" argument of scale_continuous function in a ggplot function. 8 | #' 9 | #' @param n Number of desired ticks on an axis. Defaults to 10. 10 | number_ticks <- function(n = 10) { 11 | 12 | if (!is.numeric(n)) 13 | stop("n must be a numeric input") 14 | 15 | function(limits) { 16 | pretty(limits, n) 17 | } 18 | 19 | } 20 | 21 | # Apply theme ------------------------------------------------------------- 22 | 23 | #' Apply a neat, customized ggplot2 theme 24 | #' 25 | #' This function applies a customized ggplot2 theme (based on the hrbrthemes package) 26 | #' to any ggplot graph in order to create neat and good looking visualizations. 27 | #' 28 | #' @param theme_type Type of a base hrbrthemes theme to use. Currently only "ipsum" is supported. 29 | #' @importFrom ggplot2 theme element_text rel %+replace% 30 | #' @export 31 | apply_theme <- function(theme_type = "ipsum"){ 32 | 33 | if (!is.character(theme_type)) 34 | stop("theme_type must be character") 35 | 36 | selected_theme <- if(theme_type == "ipsum"){ 37 | hrbrthemes::theme_ipsum( 38 | # Adjusting the basic grid color 39 | grid_col = "#e6e6e6" 40 | ) 41 | } else { 42 | stop("no other theme than 'ipsum' is currently supported") 43 | } 44 | 45 | selected_theme %+replace% 46 | theme( 47 | # Sizing adjustments of different plot elements 48 | plot.title = element_text(size = rel(1.25)), 49 | plot.subtitle = element_text(size = rel(1.00)), 50 | strip.text = element_text(size = rel(0.95)), 51 | legend.title = element_text(size = rel(0.95)) 52 | ) 53 | 54 | } 55 | 56 | # Select palette ---------------------------------------------------------- 57 | 58 | #' Apply a neat, appealing pallete scheme 59 | #' 60 | #' Palettes are based on the list of available color schemes: \code{\link{https://github.com/EmilHvitfeldt/r-color-palettes}}. 61 | #' 62 | #' @param palette Select a palette. Available options for discrete palettes are: "awtools" (8 discrete colors) 63 | #' and for continuous paletter: "berlin" or "lajolla" (60 continuous colors each). Defaults to "awtools". 64 | #' 65 | #' @export 66 | select_palette <- function(palette = "awtools"){ 67 | 68 | if (!is.character(palette)) 69 | stop("argument must be character") 70 | 71 | if (!(palette %in% c("awtools", "binary", "inv_binary", "berlin", "lajolla"))) 72 | stop("palette type not supported") 73 | 74 | ### Discrete palettes 75 | 76 | if (palette == "awtools") { 77 | 78 | paletteer::paletteer_d("awtools", "a_palette") 79 | 80 | } else if (palette == "binary") { 81 | 82 | c("#4db69e", "#e84a5f") 83 | 84 | } else if (palette == "inv_binary") { 85 | 86 | c("#e84a5f", "#4db69e") 87 | 88 | ### Continuous palettes 89 | 90 | } else if (palette == "berlin") { 91 | 92 | paletteer::paletteer_c("scico", "berlin", 60) 93 | 94 | } else if (palette == "lajolla") { 95 | 96 | paletteer::paletteer_c("scico", "lajolla", 60) 97 | 98 | 99 | } else { 100 | 101 | NULL 102 | 103 | } 104 | 105 | } 106 | -------------------------------------------------------------------------------- /R/plot_bars.R: -------------------------------------------------------------------------------- 1 | # Create a barplot --------------------------------------------------- 2 | 3 | #' Plot bars of numerical variables 4 | #' 5 | #' This function creates nicely formatted, standardized bar-plots. 6 | #' 7 | #' @param df A data frame. 8 | #' @param x A numeric/ categorical variable to plot the bar plot. 9 | #' @param y A numeric variable which contains summarised y values (before plotting). Use only with stat = "identity". 10 | #' @param y_prop A logical variable to choose between counts/ proportion on the y axis. Defaults to FALSE (proportion). 11 | #' @param x_type Character identifier for type of the variable x defined above: "num" for numeric (plots histogram) and "char" for character (plots bar chart). Defauls to "num". 12 | #' @param fill Select an additional grouping variable to be used for plotting. Defaults to NULL. 13 | #' @param facet Select an additional faceting variable to create facets. Defaults to NULL. 14 | #' @param binwidth Select binwidth, defaults to NULL and lets ggplot select the optimal binwidth. 15 | #' @param position Select the position of the barplot from: "stack" (default), "dodge" or "fill". 16 | #' @param stat Character identifier for whether the data is already grouped ("identity") or if the function needs to aggregate data at the level of x ("count"). 17 | #' @param fct_order Should the factors be reordered by their frequency? Defaults to FALSE. 18 | #' @param angle Select the rotation angle for the x axis labels. Defaults to 0. 19 | #' @param title Should the plot title appear automatically. Defaults to TRUE. 20 | #' @param subtitle Text that is displayed on the subtitle. Defaults to NULL. 21 | #' @param caption Text that is displayed on the caption. Defaults to NULL. 22 | #' @param lab_x Text that is displayed on the x axis. Defaults to "Level". 23 | #' @param lab_y Text that is displayed on the y axis. Defaults to "Value range". 24 | #' @param legend Should the plot legend appear automatically. Defaults to TRUE. 25 | #' @param vline Should any horizontal lines be added to the plot. Defaults to c(NaN). 26 | #' @param alpha Select plot fill transparency. Defaults to 0.7. 27 | #' @param quantile_low Select lower percentile for outliers exclusion. Defaults to 0.0\%. 28 | #' @param quantile_high Select upper percentile for outliers exclusion. Defaults to 1.0\%. 29 | #' @param theme_type Select a theme type from themes available in the \code{\link{apply_theme}} function. Defaults to "ipsum". 30 | #' @param palette Select a palette type with the \code{\link{select_palette}} function. Defaults to "awtools". 31 | #' @examples 32 | #' library(tidyverse) 33 | #' 34 | #' diamonds_sum <- diamonds %>% 35 | #' group_by(cut) %>% 36 | #' summarise(mean_carat = mean(carat, na.rm = TRUE)) 37 | #' 38 | #' @import dplyr 39 | #' @importFrom ggplot2 geom_histogram xlim 40 | #' @importFrom rlang .data := 41 | #' @export 42 | plot_bars <- function(df, 43 | x, 44 | y = NULL, 45 | y_prop = FALSE, 46 | x_type = "num", 47 | fill = NULL, 48 | facet = NULL, 49 | binwidth = NULL, 50 | position = "stack", 51 | stat = "count", 52 | fct_order = FALSE, 53 | angle = 0, 54 | title = TRUE, 55 | subtitle = NULL, 56 | caption = NULL, 57 | lab_x = "Value range", 58 | lab_y = "Proportion", 59 | legend = TRUE, 60 | vline = c(NaN), 61 | alpha = 0.7, 62 | quantile_low = 0, 63 | quantile_high = 1, 64 | theme_type = "ipsum", 65 | palette = "awtools" 66 | ) { 67 | 68 | if (!is.data.frame(df)) 69 | stop("object must be a data frame") 70 | 71 | if (!is.character(palette)) 72 | stop("argument must be character") 73 | 74 | var_x <- rlang::enquo(x) 75 | var_fill <- rlang::enquo(fill) 76 | var_facet <- rlang::enquo(facet) 77 | var_y <- rlang::enquo(y) 78 | 79 | if (x_type == "num") { 80 | 81 | plot <- df %>% 82 | ggplot() + 83 | geom_vline(xintercept = vline, linetype = 2, size = 1, color = "#6E7B8B", alpha = .8) 84 | 85 | limits <- df %>% 86 | select(value = !!var_x) %>% 87 | summarise( 88 | min = stats::quantile(.data$value, quantile_low[[1]], na.rm = TRUE), 89 | max = stats::quantile(.data$value, quantile_high[[1]], na.rm = TRUE) 90 | ) 91 | 92 | if (rlang::quo_is_null(var_fill)) { 93 | 94 | plot <- plot + 95 | geom_histogram( 96 | aes( 97 | x = {{ x }} 98 | ), 99 | alpha = alpha, 100 | position = position, 101 | fill = "#1d8fd2", 102 | binwidth = binwidth 103 | ) + 104 | xlim(limits$min, limits$max) 105 | 106 | } else { 107 | 108 | plot <- plot + 109 | geom_histogram( 110 | aes( 111 | x = {{ x }}, 112 | fill = {{ fill }} 113 | ), 114 | alpha = alpha, 115 | position = position, 116 | binwidth = binwidth 117 | ) + 118 | xlim(limits$min, limits$max) + 119 | scale_fill_manual(values = select_palette(palette)) 120 | 121 | } 122 | } 123 | 124 | else if (x_type == "char") { 125 | 126 | var_name <- rlang::quo_name(var_x) 127 | 128 | if (fct_order == TRUE){ 129 | 130 | df <- df %>% 131 | mutate(!!var_name := as.factor(!!var_x) %>% 132 | forcats::fct_infreq() %>% 133 | forcats::fct_rev()) 134 | } else { 135 | 136 | df <- df %>% 137 | mutate(!!var_name := as.factor(!!var_x)) 138 | } 139 | 140 | if (rlang::quo_is_null(var_y)) { 141 | 142 | if (y_prop){ 143 | 144 | plot <- df %>% 145 | ggplot(aes(y = stat(count / sum(count)))) 146 | 147 | } else { 148 | 149 | plot <- df %>% 150 | ggplot(aes(y = stat(count))) 151 | 152 | } 153 | 154 | } else { 155 | 156 | if (y_prop) { 157 | 158 | df_tmp <- df %>% 159 | mutate(prop = (!!var_y)/sum(!!var_y)) 160 | 161 | plot <- df_tmp %>% 162 | ggplot(aes(y = .data$prop)) 163 | 164 | } else { 165 | 166 | plot <- df %>% 167 | ggplot(aes(y = {{ y }})) 168 | 169 | } 170 | } 171 | 172 | if (rlang::quo_is_null(var_fill)) { 173 | 174 | plot <- plot + 175 | geom_bar( 176 | aes({{ x }}), 177 | alpha = alpha, 178 | stat = stat, 179 | fill = "#019875", 180 | position = position) 181 | 182 | } else { 183 | 184 | plot <- plot + 185 | geom_bar( 186 | aes( 187 | x = {{ x }}, 188 | fill = {{ fill }} 189 | ), 190 | alpha = alpha, 191 | stat = stat, 192 | position = position 193 | ) + 194 | scale_fill_manual(values = select_palette(palette)) 195 | } 196 | 197 | if (y_prop) { 198 | plot <- plot + 199 | scale_y_continuous(labels = scales::percent_format()) 200 | } 201 | } 202 | 203 | if (!rlang::quo_is_null(var_facet)) { 204 | plot <- plot + 205 | facet_wrap(rlang::quo_text(var_facet), scales = "free_x") 206 | } 207 | 208 | if (!y_prop & lab_y == "Proportion") lab_y = "Count" 209 | 210 | plot + 211 | ggtitle( 212 | label = if (title == TRUE) { 213 | glue::glue("Bar plot of {rlang::quo_text(var_x)}") 214 | } else if (is.character(title)) { 215 | title 216 | } else { 217 | element_blank() 218 | } 219 | ) + 220 | labs( 221 | fill = glue::glue("{aider::first_to_upper(rlang::quo_text(var_fill))}:"), 222 | x = lab_x, 223 | y = lab_y 224 | ) + 225 | labs( 226 | subtitle = if (is.null(subtitle)) {element_blank()} else {subtitle} 227 | ) + 228 | labs( 229 | caption = if (is.null(caption)) {element_blank()} else {caption} 230 | ) + 231 | apply_theme(theme_type) + 232 | theme( 233 | legend.position = ifelse(legend == TRUE, "bottom", "none"), 234 | axis.text.x = element_text(angle = angle, hjust = ifelse(angle != 0, 1, .5)) 235 | ) 236 | } 237 | -------------------------------------------------------------------------------- /R/plot_boxplot.R: -------------------------------------------------------------------------------- 1 | 2 | # Create a boxplot --------------------------------------------------- 3 | 4 | #' Plot box-plots of numerical variables 5 | #' 6 | #' This function creates nicely formatted, standardized box-plots. 7 | #' 8 | #' @param df A data frame. 9 | #' @param x A categorical variable for the x axis groupings. 10 | #' @param y A numerical variable for the y axis levels. 11 | #' @param fill Select an additional grouping variable to be used for plotting. Defaults to NULL. 12 | #' @param facet Select an additional faceting variable to create facets. Defaults to NULL. 13 | #' @param ticks Select the number of ticks on the x and y axis with the \code{\link{number_ticks}} function. Defaults to 10. 14 | #' @param angle Select the rotation angle for the x axis labels. Defaults to 0. 15 | #' @param title Should the plot title appear automatically. Defaults to TRUE. 16 | #' @param subtitle Text that is displayed on the subtitle. Defaults to NULL. 17 | #' @param caption Text that is displayed on the caption. Defaults to NULL. 18 | #' @param lab_x Text that is displayed on the x axis. Defaults to "Level". 19 | #' @param lab_y Text that is displayed on the y axis. Defaults to "Value range". 20 | #' @param legend Should the plot legend appear automatically. Defaults to TRUE. 21 | #' @param vline Should any horizontal lines be added to the plot. Defaults to c(NaN). 22 | #' @param alpha Select plot fill transparency. Defaults to 0.7. 23 | #' @param quantile_low Select lower percentile for outliers exclusion. Defaults to 0.0\%. 24 | #' @param quantile_high Select upper percentile for outliers exclusion. Defaults to 1.0\%. 25 | #' @param theme_type Select a theme type from themes available in the \code{\link{apply_theme}} function. Defaults to "ipsum". 26 | #' @param palette Select a palette type with the \code{\link{select_palette}} function. Defaults to "awtools". 27 | #' @examples 28 | #' diamonds %>% 29 | #' plot_boxplot(x = cut, 30 | #' y = carat) 31 | #' 32 | #' diamonds %>% 33 | #' plot_boxplot(x = cut, 34 | #' y = carat, 35 | #' fill = color) 36 | #' 37 | #' diamonds %>% 38 | #' plot_boxplot(x = cut, 39 | #' y = carat, 40 | #' fill = cut, 41 | #' facet = color) 42 | #' 43 | #' diamonds %>% 44 | #' plot_boxplot(x = cut, 45 | #' y = carat, 46 | #' fill = cut, 47 | #' title = "Write your title here", 48 | #' subtitle = "Write your subtitle here", 49 | #' caption = "Write your caption here", 50 | #' lab_x = "Carat", 51 | #' alpha = .5, 52 | #' vline = 1) 53 | #' @importFrom ggplot2 geom_boxplot geom_hline 54 | #' @export 55 | plot_boxplot <- function(df, 56 | x, 57 | y, 58 | fill = NULL, 59 | facet = NULL, 60 | ticks = 10, 61 | angle = 0, 62 | title = TRUE, 63 | subtitle = NULL, 64 | caption = NULL, 65 | lab_x = "Level", 66 | lab_y = "Value range", 67 | legend = TRUE, 68 | vline = c(NaN), 69 | alpha = .7, 70 | quantile_low = 0, 71 | quantile_high = 1, 72 | theme_type = "ipsum", 73 | palette = "awtools" 74 | ) { 75 | 76 | if (!is.data.frame(df)) 77 | stop("object must be a data frame") 78 | 79 | var_x <- rlang::enquo(x) 80 | var_y <- rlang::enquo(y) 81 | var_fill <- rlang::enquo(fill) 82 | var_facet <- rlang::enquo(facet) 83 | 84 | limits <- df %>% 85 | dplyr::select(value = !!var_y) %>% 86 | dplyr::summarise( 87 | min = stats::quantile(.data$value, quantile_low[[1]], na.rm = TRUE), 88 | max = stats::quantile(.data$value, quantile_high[[1]], na.rm = TRUE) 89 | ) 90 | 91 | plot <- df %>% 92 | ggplot() + 93 | geom_hline(yintercept = vline, linetype = 2, size = 1, color = "#6E7B8B", alpha = .8) + 94 | ggtitle( 95 | label = if (title == TRUE) { 96 | glue::glue("Boxplot plot of {rlang::quo_text(var_y)} by {rlang::quo_text(var_x)}") 97 | } else if (is.character(title)) { 98 | title 99 | } else { 100 | element_blank() 101 | } 102 | ) + 103 | labs( 104 | fill = glue::glue("{first_to_upper(rlang::quo_text(var_fill))}:"), 105 | x = lab_x, 106 | y = lab_y) + 107 | labs( 108 | subtitle = if (is.null(subtitle)) {element_blank()} else {subtitle} 109 | ) + 110 | labs( 111 | caption = if (is.null(caption)) {element_blank()} else {caption} 112 | ) + 113 | scale_y_continuous( 114 | limits = c( 115 | limits$min, 116 | limits$max 117 | ), 118 | breaks = number_ticks(ticks) 119 | ) + 120 | apply_theme(theme_type) + 121 | theme( 122 | legend.position = ifelse(legend == TRUE, "bottom", "none"), 123 | axis.text.x = element_text(angle = angle, hjust = ifelse(angle != 0, 1, .5)) 124 | ) 125 | 126 | if (!rlang::quo_is_null(var_facet)) { 127 | 128 | plot <- plot + 129 | facet_wrap(rlang::quo_text(var_facet), scales = "free_x") 130 | 131 | } 132 | 133 | if (rlang::quo_is_null(var_fill)) { 134 | 135 | plot + 136 | geom_boxplot( 137 | aes( 138 | x = {{ x }}, 139 | y = {{ y }} 140 | ), 141 | alpha = alpha, 142 | fill = "#019875" 143 | ) 144 | 145 | } else { 146 | 147 | plot + 148 | geom_boxplot( 149 | aes( 150 | x = {{ x }}, 151 | y = {{ y }}, 152 | fill = {{ fill }} 153 | ), 154 | alpha = alpha 155 | ) + 156 | scale_fill_manual(values = select_palette(palette)) 157 | } 158 | } 159 | -------------------------------------------------------------------------------- /R/plot_correlation.R: -------------------------------------------------------------------------------- 1 | 2 | # Create a correlation matrix --------------------------------------------- 3 | 4 | #' Plot a correlation matrix of numerical variables 5 | #' 6 | #' This function creates a nicely formatted, standardised correlation matrix of numerical variables. 7 | #' Make sure that long variables names are are shortened before using the function for easier interpretation. 8 | #' 9 | #' @param df A data frame. 10 | #' @param method A character string indicating which correlation coefficient (or covariance) should be computed. Options are: "spearman" (default), "pearson" or "kendall". 11 | #' @param order Ordering method of the correlation matrix. Options are: "alphabet" (default) and "hclust". 12 | #' @param label_size Size of the text label. Defaults to 0.7. 13 | #' @param number_size Size of the correlation number. Defaults to 0.9. 14 | #' @examples 15 | #' diamonds %>% 16 | #' plot_correlation() 17 | #' 18 | #' @import dplyr 19 | #' @importFrom magrittr %>% 20 | #' @importFrom rlang .data 21 | #' @export 22 | plot_correlation <- function(df, 23 | method = "spearman", 24 | order = "alphabet", 25 | label_size = 0.7, 26 | number_size = 0.7 27 | ){ 28 | 29 | if (!is.data.frame(df)) 30 | stop("object must be a data frame") 31 | 32 | if (any(!is.character(method), !is.character(order))) 33 | stop("arguments must be character") 34 | 35 | if (!is.numeric(label_size)) 36 | stop("argument must be numeric") 37 | 38 | cor_mtx <- df %>% 39 | select_if(is.numeric) %>% 40 | stats::cor(use = "pairwise.complete.obs", method = method) 41 | 42 | cor_sig <- corrplot::cor.mtest(cor_mtx, conf.level = .95) 43 | 44 | corrplot::corrplot( 45 | cor_mtx, 46 | col = grDevices::colorRampPalette(c("#e84a5f", "white", "#4db69e"))(50), 47 | order = order, 48 | tl.cex = label_size, 49 | addCoef.col = "black", 50 | number.cex = number_size, 51 | method = "circle", 52 | type = "lower", 53 | tl.pos = "dt", 54 | addrect = 3, 55 | tl.col = "black", 56 | tl.srt = 45, 57 | p.mat = if (order == "alphabet") {NULL} else {cor_sig$p}, 58 | insig = "blank", 59 | diag = FALSE) 60 | 61 | } 62 | -------------------------------------------------------------------------------- /R/plot_deciles.R: -------------------------------------------------------------------------------- 1 | 2 | # Create a decile plot --------------------------------------------------- 3 | 4 | #' Plot decile plots of numerical variables 5 | #' 6 | #' This function creates nicely formatted, standardised decile plots. Prior to calling the function 7 | #' the data should only be in a form of a decile table \code{\link{calculate_decile_table}}. Above 8 | #' each bar the median value of the bin is displayed. 9 | #' 10 | #' @param df A data frame. 11 | #' @param x A categorical variable for the x axis groupings. Defaults to 'decile'. 12 | #' @param y A numerical variable for the y axis levels. Defaults to 'ratio'. 13 | #' @param facet Select an additional faceting variable to create facets. Defaults to NULL. 14 | #' @param ticks Select the number of ticks on the x and y axis with the \code{\link{number_ticks}} function. Defaults to 10. 15 | #' @param angle Select the rotation angle for the x axis labels. Defaults to 0. 16 | #' @param title Should the plot title appear automatically. Defaults to TRUE. 17 | #' @param subtitle Text that is displayed on the subtitle. Defaults to NULL. 18 | #' @param caption Text that is displayed on the caption. Defaults to NULL. 19 | #' @param lab_x Text that is displayed on the x axis. Defaults to "Decile". 20 | #' @param lab_y Text that is displayed on the y axis. Defaults to "Percentage". 21 | #' @param legend Should the plot legend appear automatically. Defaults to TRUE. 22 | #' @param alpha Select plot fill transparency. Defaults to 0.7. 23 | #' @param quantile_low Select lower percentile for outliers exclusion. Defaults to 0.0\%. 24 | #' @param quantile_high Select upper percentile for outliers exclusion. Defaults to 1.0\%. 25 | #' @param theme_type Select a theme type from themes available in the \code{\link{apply_theme}} function. Defaults to "ipsum". 26 | #' @param palette Select a palette type with the \code{\link{select_palette}} function. Defaults to "awtools". 27 | #' @examples 28 | #' library(tidyverse) 29 | #' 30 | #' diamonds_filter <- diamonds %>% filter(cut %in% c("Ideal", "Premium")) 31 | #' 32 | #' diamonds_filter %>% 33 | #' calculate_decile_table(price, cut, "Ideal") %>% 34 | #' plot_deciles() 35 | #' 36 | #' diamonds_filter %>% 37 | #' calculate_decile_table(binning = price, 38 | #' grouping = cut, 39 | #' top_level = "Ideal", 40 | #' n_bins = 10, 41 | #' format = FALSE, 42 | #' color) %>% 43 | #' plot_deciles(facet = color) 44 | #' @importFrom ggplot2 geom_bar geom_text position_dodge scale_fill_gradientn 45 | #' @importFrom rlang .data 46 | #' @export 47 | plot_deciles <- function(df, 48 | x = decile, 49 | y = ratio, 50 | facet = NULL, 51 | ticks = 10, 52 | angle = 0, 53 | title = TRUE, 54 | subtitle = NULL, 55 | caption = NULL, 56 | lab_x = "Decile", 57 | lab_y = "Ratio", 58 | legend = TRUE, 59 | alpha = .7, 60 | quantile_low = 0, 61 | quantile_high = 1, 62 | theme_type = "ipsum", 63 | palette = "awtools" 64 | ) { 65 | 66 | if (!is.data.frame(df)) 67 | stop("object must be a data frame") 68 | 69 | var_x <- rlang::enquo(x) 70 | var_y <- rlang::enquo(y) 71 | var_facet <- rlang::enquo(facet) 72 | 73 | limits_min <- 0 74 | limits_max <- dplyr::select(df, !!var_y)[[1]] %>% max() + .05 75 | 76 | plot <- df %>% 77 | ggplot() + 78 | geom_bar( 79 | aes( 80 | x = decile, 81 | y = ratio, 82 | fill = ratio 83 | ), 84 | stat = "identity", 85 | width = .8, 86 | alpha = alpha 87 | ) + 88 | geom_text( 89 | aes( 90 | x = decile, 91 | y = ratio + 0.03, 92 | label = round(median, 2) 93 | ), 94 | position = position_dodge(.9), 95 | size = 3.2, 96 | check_overlap = TRUE 97 | ) + 98 | ggtitle( 99 | label = if (title == TRUE) { 100 | glue::glue("Decile plot of {rlang::quo_text(var_y)} by {rlang::quo_text(var_x)}") 101 | } else if (is.character(title)) { 102 | title 103 | } else { 104 | element_blank() 105 | } 106 | ) + 107 | labs( 108 | fill = "Ratio", 109 | x = lab_x, 110 | y = lab_y) + 111 | labs( 112 | subtitle = if (is.null(subtitle)) {element_blank()} else {subtitle} 113 | ) + 114 | labs( 115 | caption = if (is.null(caption)) {element_blank()} else {caption} 116 | ) + 117 | scale_y_continuous( 118 | limits = c( 119 | limits_min, 120 | limits_max 121 | ), 122 | labels = scales::percent, 123 | breaks = number_ticks(ticks) 124 | ) + 125 | apply_theme(theme_type) + 126 | theme( 127 | legend.position = ifelse(legend == TRUE, "bottom", "none"), 128 | axis.text.x = element_text(angle = angle, hjust = ifelse(angle != 0, 1, .5)) 129 | ) 130 | 131 | if (!rlang::quo_is_null(var_facet)) { 132 | plot <- plot + 133 | facet_wrap(rlang::quo_text(var_facet), scales = "free_x") 134 | } 135 | 136 | plot + 137 | scale_fill_gradientn(colours = select_palette(palette)) 138 | 139 | } 140 | -------------------------------------------------------------------------------- /R/plot_density.R: -------------------------------------------------------------------------------- 1 | 2 | # Create a density plot --------------------------------------------------- 3 | 4 | #' Plot density of numerical variables 5 | #' 6 | #' This function creates nicely formatted, standardized density plots. 7 | #' 8 | #' @param df A data frame. 9 | #' @param x A numerical variable to plot its density. 10 | #' @param fill Select an additional grouping variable to be used for density plotting. Defaults to NULL. 11 | #' @param facet Select an additional faceting variable to create facets. Defaults to NULL. 12 | #' @param position Select the positional adjustment of the plot. Possible values are: "identity" (default), "stack" or "fill" 13 | #' @param ticks Select the number of ticks on the x and y axis with the \code{\link{number_ticks}} function. Defaults to 10. 14 | #' @param angle Select the rotation angle for the x axis labels. Defaults to 0. 15 | #' @param title Should the plot title appear automatically. Defaults to TRUE. 16 | #' @param subtitle Text that is displayed on the subtitle. Defaults to NULL. 17 | #' @param caption Text that is displayed on the caption. Defaults to NULL. 18 | #' @param lab_x Text that is displayed on the x axis. Defaults to "Value range". 19 | #' @param lab_y Text that is displayed on the y axis. Defaults to "Density". 20 | #' @param legend Should the plot legend appear automatically. Defaults to TRUE. 21 | #' @param vline Should any vertical lines be added to the plot. Defaults to c(NaN). 22 | #' @param alpha Select plot fill transparency. Defaults to 0.7. 23 | #' @param quantile_low Select lower percentile for outliers exclusion. Defaults to 0.0\%. 24 | #' @param quantile_high Select upper percentile for outliers exclusion. Defaults to 1.0\%. 25 | #' @param theme_type Select a theme type from themes available in the \code{\link{apply_theme}} function. Defaults to "ipsum". 26 | #' @param palette Select a palette type with the \code{\link{select_palette}} function. Defaults to "awtools". 27 | #' @examples 28 | #' diamonds %>% 29 | #' plot_density(x = carat) 30 | #' 31 | #' diamonds %>% 32 | #' plot_density(x = carat, 33 | #' fill = cut) 34 | #' 35 | #' diamonds %>% 36 | #' plot_density(x = carat, 37 | #' fill = cut, 38 | #' position = "stack") 39 | #' 40 | #' diamonds %>% 41 | #' plot_density(x = carat, 42 | #' fill = cut, 43 | #' position = "fill") 44 | #' 45 | #' diamonds %>% 46 | #' plot_density(x = carat, 47 | #' facet = cut) 48 | #' 49 | #' diamonds %>% 50 | #' plot_density(x = carat, 51 | #' fill = cut, 52 | #' facet = cut, 53 | #' alpha = .5) 54 | #' 55 | #' diamonds %>% 56 | #' plot_density(x = carat, 57 | #' fill = cut, 58 | #' facet = cut, 59 | #' title = "Write your title here", 60 | #' subtitle = "Write your subtitle here", 61 | #' caption = "Write your caption here", 62 | #' lab_x = "Carat", 63 | #' alpha = .5, 64 | #' vline = 1) 65 | #' @importFrom ggplot2 ggplot geom_vline geom_density ggtitle labs theme 66 | #' @importFrom ggplot2 ggtitle labs theme aes 67 | #' @importFrom ggplot2 facet_wrap 68 | #' @importFrom ggplot2 element_blank element_text 69 | #' @importFrom ggplot2 ggplot scale_x_continuous scale_y_continuous scale_fill_manual 70 | #' @importFrom rlang .data 71 | #' @export 72 | plot_density <- function(df, 73 | x, 74 | fill = NULL, 75 | facet = NULL, 76 | position = "identity", 77 | ticks = 5, 78 | angle = 0, 79 | title = TRUE, 80 | subtitle = NULL, 81 | caption = NULL, 82 | lab_x = "Value range", 83 | lab_y = "Density", 84 | legend = TRUE, 85 | vline = c(NaN), 86 | alpha = .7, 87 | quantile_low = 0, 88 | quantile_high = 1, 89 | theme_type = "ipsum", 90 | palette = "awtools" 91 | ) { 92 | 93 | if (!is.data.frame(df)) 94 | stop("object must be a data frame") 95 | 96 | var_x <- rlang::enquo(x) 97 | var_fill <- rlang::enquo(fill) 98 | var_facet <- rlang::enquo(facet) 99 | 100 | limits <- df %>% 101 | dplyr::select(value = !!var_x) %>% 102 | dplyr::summarise( 103 | min = stats::quantile(.data$value, quantile_low[[1]], na.rm = TRUE), 104 | max = stats::quantile(.data$value, quantile_high[[1]], na.rm = TRUE) 105 | ) 106 | 107 | plot <- df %>% 108 | ggplot() + 109 | geom_vline(xintercept = vline, linetype = 2, size = 1, color = "#6E7B8B", alpha = .8) + 110 | ggtitle( 111 | label = if (title == TRUE) { 112 | glue::glue("Density plot of {rlang::quo_text(var_x)}") 113 | } else if (is.character(title)) { 114 | title 115 | } else { 116 | element_blank() 117 | } 118 | ) + 119 | labs( 120 | fill = glue::glue("{first_to_upper(rlang::quo_text(var_fill))}:"), 121 | x = lab_x, 122 | y = lab_y) + 123 | labs( 124 | subtitle = if (is.null(subtitle)) {element_blank()} else {subtitle} 125 | ) + 126 | labs( 127 | caption = if (is.null(caption)) {element_blank()} else {caption} 128 | ) + 129 | scale_x_continuous( 130 | limits = c( 131 | limits$min, 132 | limits$max 133 | ), 134 | breaks = number_ticks(ticks) 135 | ) + 136 | scale_y_continuous( 137 | breaks = number_ticks(ticks) 138 | ) + 139 | apply_theme(theme_type) + 140 | theme( 141 | legend.position = ifelse(legend == TRUE, "bottom", "none"), 142 | axis.text.x = element_text(angle = angle, hjust = ifelse(angle != 0, 1, .5)) 143 | ) 144 | 145 | if (!rlang::quo_is_null(var_facet)) { 146 | plot <- plot + 147 | facet_wrap(rlang::quo_text(var_facet), scales = "free_x") 148 | } 149 | 150 | if (rlang::quo_is_null(var_fill)) { 151 | 152 | plot + 153 | geom_density( 154 | aes( 155 | x = {{ x }} 156 | ), 157 | position = position, 158 | alpha = alpha, 159 | fill = "#019875" 160 | ) 161 | 162 | } else { 163 | 164 | plot + 165 | geom_density( 166 | aes( 167 | x = {{ x }}, 168 | fill = {{ fill }} 169 | ), 170 | position = position, 171 | alpha = alpha 172 | ) + 173 | scale_fill_manual(values = select_palette(palette)) 174 | } 175 | } 176 | 177 | -------------------------------------------------------------------------------- /R/plot_lines.R: -------------------------------------------------------------------------------- 1 | 2 | # Create a line plot ------------------------------------------------------ 3 | 4 | #' Plot lines of numerical variables. Usefull especially for time-series data 5 | #' 6 | #' This function creates nicely formatted, standardised line plots. Color and group parameters for geom_line and 7 | #' geom_point are automatically inherited from the fill parameter. 8 | #' 9 | #' @param df A data frame. 10 | #' @param x A categorical variable for the x axis groupings. 11 | #' @param y A numerical variable for the y axis levels. 12 | #' @param fill Select an additional grouping variable to be used for plotting. Defaults to NULL. 13 | #' @param facet Select an additional faceting variable to create facets. Defaults to NULL. 14 | #' @param ticks Select the number of ticks on the x and y axis with the \code{\link{number_ticks}} function. Defaults to 10. 15 | #' @param angle Select the rotation angle for the x axis labels. Defaults to 0. 16 | #' @param title Should the plot title appear automatically. Defaults to TRUE. 17 | #' @param subtitle Text that is displayed on the subtitle. Defaults to NULL. 18 | #' @param caption Text that is displayed on the caption. Defaults to NULL. 19 | #' @param lab_x Text that is displayed on the x axis. Defaults to "Value range". 20 | #' @param lab_y Text that is displayed on the y axis. Defaults to "Value range". 21 | #' @param legend Should the plot legend appear automatically. Defaults to TRUE. 22 | #' @param hline Should any horizontal lines be added to the plot. Defaults to c(NaN). 23 | #' @param alpha Select plot fill transparency. Defaults to 0.7. 24 | #' @param limit_min Select lower limit for the y scale. Defaults to NA. 25 | #' @param limit_max Select upper limit for the y scale. Defaults to NA. 26 | #' @param theme_type Select a theme type from themes available in the \code{\link{apply_theme}} function. Defaults to "ipsum". 27 | #' @param palette Select a palette type with the \code{\link{select_palette}} function. Defaults to "awtools". 28 | #' @examples 29 | #' library(tidyverse) 30 | #' 31 | #' tibble( 32 | #' time = 1:20, 33 | #' value = rnorm(20, 0.5, 2) 34 | #' ) %>% 35 | #' plot_line( 36 | #' x = time, 37 | #' y = value, 38 | #' ticks = 10, 39 | #' hline = 0.05 40 | #' ) 41 | #' 42 | #' tibble( 43 | #' time = 1:20, 44 | #' value = rnorm(20, 0.5, 2) 45 | #' ) %>% 46 | #' plot_line( 47 | #' x = time, 48 | #' y = value, 49 | #' ticks = 10, 50 | #' hline = 0.05, 51 | #' limit_min = -2, 52 | #' limit_max = 2 53 | #' ) 54 | #' @import dplyr 55 | #' @importFrom ggplot2 geom_line geom_point coord_cartesian 56 | #' @importFrom rlang .data 57 | #' @export 58 | plot_line <- function(df, 59 | x, 60 | y, 61 | fill = NULL, 62 | facet = NULL, 63 | ticks = 10, 64 | angle = 0, 65 | title = TRUE, 66 | subtitle = NULL, 67 | caption = NULL, 68 | lab_x = "Value range", 69 | lab_y = "Value range", 70 | legend = TRUE, 71 | hline = c(NaN), 72 | alpha = 0.7, 73 | limit_min = NA, 74 | limit_max = NA, 75 | theme_type = "ipsum", 76 | palette = "awtools" 77 | ) { 78 | 79 | if (!is.data.frame(df)) 80 | stop("object must be a data frame") 81 | 82 | var_x <- rlang::enquo(x) 83 | var_y <- rlang::enquo(y) 84 | var_fill <- rlang::enquo(fill) 85 | var_facet <- rlang::enquo(facet) 86 | 87 | true_min <- min(select(df, !!var_y), na.rm = TRUE) 88 | true_max <- max(select(df, !!var_y), na.rm = TRUE) 89 | 90 | plot <- df %>% 91 | ggplot() + 92 | geom_hline(yintercept = hline, linetype = 2, size = 1, color = "#6E7B8B", alpha = .8) + 93 | ggtitle( 94 | label = if (title == TRUE) { 95 | glue::glue("{first_to_upper(rlang::quo_text(var_y))} by {rlang::quo_text(var_x)}") 96 | } else if (is.character(title)) { 97 | title 98 | } else { 99 | element_blank() 100 | } 101 | ) + 102 | labs( 103 | color = glue::glue("{first_to_upper(rlang::quo_text(var_fill))}:"), 104 | x = lab_x, 105 | y = lab_y) + 106 | labs( 107 | subtitle = if (is.null(subtitle)) {element_blank()} else {subtitle} 108 | ) + 109 | labs( 110 | caption = if (is.null(caption)) {element_blank()} else {caption} 111 | ) + 112 | coord_cartesian( 113 | ylim = c( 114 | ifelse(is.na(limit_min), true_min, limit_min), 115 | ifelse(is.na(limit_max), true_max, limit_max) 116 | ) 117 | ) + 118 | scale_y_continuous( 119 | breaks = number_ticks(ticks) 120 | ) + 121 | apply_theme(theme_type) + 122 | theme( 123 | legend.position = ifelse(legend == TRUE, "bottom", "none"), 124 | axis.text.x = element_text(angle = angle, hjust = ifelse(angle != 0, 1, .5)) 125 | ) 126 | 127 | if (!rlang::quo_is_null(var_facet)) { 128 | plot <- plot + 129 | facet_wrap(rlang::quo_text(var_facet), scales = "free_x") 130 | } 131 | 132 | if (rlang::quo_is_null(var_fill)) { 133 | 134 | plot + 135 | geom_line( 136 | aes( 137 | x = {{ x }}, 138 | y = {{ y }}, 139 | group = 1 140 | ), 141 | alpha = alpha, 142 | color = "#019875" 143 | ) + 144 | geom_point( 145 | aes( 146 | x = {{ x }}, 147 | y = {{ y }}, 148 | ), 149 | alpha = alpha, 150 | ) 151 | 152 | } else { 153 | 154 | plot + 155 | geom_line( 156 | aes( 157 | x = {{ x }}, 158 | y = {{ y }}, 159 | group = {{ fill }}, 160 | color = {{ fill }} 161 | ), 162 | alpha = alpha 163 | ) + 164 | geom_point( 165 | aes( 166 | x = {{ x }}, 167 | y = {{ y }}, 168 | ), 169 | alpha = alpha 170 | ) + 171 | scale_fill_manual(values = select_palette(palette)) 172 | } 173 | } 174 | -------------------------------------------------------------------------------- /R/tweak.R: -------------------------------------------------------------------------------- 1 | 2 | # First to lower ---------------------------------------------------------- 3 | 4 | #' Convert strings first letter to lowercase 5 | #' 6 | #' This function converts your data frame column names or character vector elements 7 | #' first letter to lowercase. It can be used in a pipe. 8 | #' 9 | #' @param x A data frame or vector. 10 | #' @examples 11 | #' first_to_lower(c("first", "second")) 12 | #' @export 13 | first_to_lower <- function(x) { 14 | 15 | if (is.data.frame(x)) { 16 | df_names <- names(x) 17 | substr(df_names, 1, 1) <- tolower(substr(df_names, 1, 1)) 18 | names(x) <- df_names 19 | x 20 | } else if (all(is.vector(x), is.character(x))) { 21 | substr(x, 1, 1) <- tolower(substr(x, 1, 1)) 22 | x 23 | } else { 24 | stop("argument must be a data frame or character vector") 25 | } 26 | 27 | } 28 | 29 | # First to upper ---------------------------------------------------------- 30 | 31 | #' Convert strings first letter to uppercase 32 | #' 33 | #' This function converts your data frame column names or character vector elements 34 | #' first letter to uppercase. It can be used in a pipe. 35 | #' 36 | #' @param x A data frame or vector. 37 | #' @examples 38 | #' first_to_upper(c("first", "second")) 39 | #' 40 | #' diamonds %>% 41 | #' first_to_upper() 42 | #' @export 43 | first_to_upper <- function(x) { 44 | 45 | if (is.data.frame(x)) { 46 | df_names <- names(x) 47 | substr(df_names, 1, 1) <- toupper(substr(df_names, 1, 1)) 48 | names(x) <- df_names 49 | x 50 | } else if (all(is.vector(x), is.character(x))) { 51 | substr(x, 1, 1) <- toupper(substr(x, 1, 1)) 52 | x 53 | } else { 54 | stop("argument must be a data frame or character vector") 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /R/utils.R: -------------------------------------------------------------------------------- 1 | 2 | utils::globalVariables(c("decile", "ratio")) 3 | 4 | # Pipe operator ----------------------------------------------------------- 5 | 6 | #' Pipe operator 7 | #' 8 | #' See \code{magrittr::\link[magrittr]{\%>\%}} for details. 9 | #' 10 | #' @name %>% 11 | #' @rdname pipe 12 | #' @keywords internal 13 | #' @export 14 | #' @importFrom magrittr %>% 15 | #' @usage lhs \%>\% rhs 16 | NULL 17 | 18 | -------------------------------------------------------------------------------- /R/zzz.R: -------------------------------------------------------------------------------- 1 | 2 | .onAttach <- function(libname, pkgname) { 3 | packageStartupMessage("Enjoy plotting!") 4 | 5 | pdfFonts <- grDevices::pdfFonts 6 | extrafont::loadfonts("pdf" ,quiet = TRUE) 7 | } 8 | -------------------------------------------------------------------------------- /README.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | output: github_document 3 | --- 4 | 5 | 6 | 7 | ```{r, echo = FALSE, message=FALSE, warning=FALSE} 8 | library(tidyverse) 9 | library(ggrapid) 10 | 11 | knitr::opts_chunk$set( 12 | collapse = TRUE, 13 | comment = "#>", 14 | fig.path = "man/figures/", 15 | warning = FALSE, 16 | message = FALSE 17 | ) 18 | ``` 19 | 20 | # ggrapid: Create neat & complete ggplot visualizations with as little code as possible 21 | 22 | ## Overview 23 | 24 | ggrapid enables creation of the most common ggplot-based visualizations fast and with just a few lines of code. In practice the package offers wrappers of some of the most common ggplot geoms such as: geom_density, geom_boxplot, geom_bar etc. ggrapid comes handy when you'd like to do an initial and quick EDA (Exploratory Data Analysis) over various columns of your dataset programatically, without the need of writing a lot of custom ggplot code. 25 | 26 | ## Installation 27 | 28 | ```{r, eval = FALSE} 29 | # Install development version from GitHub 30 | devtools::install_github("konradsemsch/ggrapid") 31 | ``` 32 | 33 | ## Main functions 34 | 35 | ggrapid offers a couple wrappers around the most commonly used functions in the course of doing an EDA: 36 | 37 | * ```plot_density``` 38 | * ```plot_boxplot``` 39 | * ```plot_deciles``` (with ```calculate_decile_table```) 40 | * ```plot_correlation``` 41 | * ```plot_bars``` 42 | * ```plot_line``` 43 | 44 | #### Density plot 45 | 46 | ```{r} 47 | diamonds %>% 48 | plot_density(x = carat) 49 | ``` 50 | 51 | #### Box-plot 52 | 53 | ```{r} 54 | diamonds %>% 55 | plot_boxplot(x = cut, 56 | y = carat, 57 | fill = cut) 58 | ``` 59 | 60 | #### Decile plot 61 | 62 | ```{r} 63 | diamonds %>% 64 | filter(cut %in% c("Ideal", "Premium")) %>% 65 | calculate_decile_table(price, cut, "Ideal") %>% 66 | plot_deciles() 67 | ``` 68 | 69 | #### Correlation 70 | 71 | ```{r} 72 | diamonds %>% 73 | plot_correlation() 74 | ``` 75 | 76 | #### Barplot 77 | 78 | ```{r} 79 | diamonds %>% 80 | plot_bars(x = carat, 81 | x_type = "num", 82 | fill = cut) 83 | ``` 84 | 85 | #### Lineplot 86 | 87 | ```{r} 88 | tibble( 89 | time = 1:20, 90 | value = rnorm(20, 0.5, 2) 91 | ) %>% 92 | plot_line( 93 | x = time, 94 | y = value 95 | ) 96 | ``` 97 | 98 | ## Main arguments 99 | 100 | The most commonly implemented ggplot2 arguments across all main ggrapid functions ensure that you can build your basic EDA file without making additional changes or custom functions. Those arguments are mainly (might slightly differ across functions): 101 | 102 | * fill 103 | * facet 104 | * position 105 | * ticks 106 | * angle 107 | * title 108 | * subtitle 109 | * caption 110 | * lab_x 111 | * lab_y 112 | * legend 113 | * vline/ hline 114 | * alpha 115 | * quantile_low 116 | * quantile_high 117 | * theme_type 118 | * palette 119 | 120 | 121 | ```{r} 122 | diamonds %>% 123 | plot_density(x = carat) 124 | ``` 125 | 126 | ```{r} 127 | diamonds %>% 128 | plot_density(x = carat, 129 | fill = cut, 130 | position = "stack") 131 | ``` 132 | 133 | ```{r} 134 | diamonds %>% 135 | plot_density(x = carat, 136 | fill = cut, 137 | position = "fill") 138 | ``` 139 | 140 | ```{r} 141 | diamonds %>% 142 | plot_density(x = carat, 143 | fill = cut, 144 | facet = cut, 145 | title = "Write your title here", 146 | subtitle = "Write your subtitle here", 147 | caption = "Write your caption here", 148 | lab_x = "Carat", 149 | alpha = .5, 150 | vline = 1) 151 | ``` 152 | 153 | ## Complete usage 154 | 155 | You can easily iterate across selected columns and create a set of plots for your EDA file: 156 | 157 | ```{r} 158 | 159 | library(recipes) 160 | 161 | credit_data_nested <- credit_data %>% 162 | select(-one_of("Home", "Marital", "Records", "Job")) %>% # removing categorical variables 163 | gather(variable, variable_value, 164 | one_of("Seniority", "Time", "Age", "Expenses", # selecting variables to gather 165 | "Income", "Assets", "Debt", "Amount", "Price")) %>% 166 | nest(-variable) %>% 167 | mutate( 168 | decile_table = map(data, 169 | ~calculate_decile_table( 170 | .x, 171 | binning = variable_value, 172 | grouping = Status, 173 | top_level = "bad", 174 | format = FALSE 175 | ) 176 | ), 177 | plot_deciles = pmap(list(x = decile_table, y = variable), 178 | ~plot_deciles( 179 | .x, 180 | title = glue::glue("Decile plot of {.y}"), 181 | quantile_low = 0, 182 | quantile_high = 1, 183 | lab_x = "Decile", 184 | lab_y = "Bad rate, %" 185 | ) 186 | ), 187 | plot_boxplot = pmap(list(x = data, y = variable), 188 | ~plot_boxplot( 189 | .x, 190 | x = Status, 191 | y = variable_value, 192 | fill = Status, 193 | title = glue::glue("Box plot of {.y} by Status"), 194 | quantile_low = 0.01, 195 | quantile_high = 0.99, 196 | lab_x = "Performance", 197 | caption = "Removed 1% of observations from each side", 198 | palette = "inv_binary" 199 | ) 200 | ), 201 | plot_density = pmap(list(x = data, y = variable), 202 | ~plot_density( 203 | .x, 204 | x = variable_value, 205 | fill = Status, 206 | title = glue::glue("Box plot of {.y} by Status"), 207 | quantile_low = 0.01, 208 | quantile_high = 0.99, 209 | lab_x = "Performance", 210 | caption = "Removed 1% of observations from each side", 211 | palette = "inv_binary" 212 | ) 213 | ) 214 | ) 215 | 216 | ``` 217 | 218 | This will give you the following structure. Each row represents an individual variable and columns are the different plots you would like to inspect: 219 | 220 | ```{r} 221 | credit_data_nested[1:3, ] 222 | ``` 223 | 224 | ## Exemplary EDA format 225 | 226 | Creating a standardised EDA file is just as easy as doing something like this: 227 | 228 | ### `r glue::glue("Variable: {credit_data_nested$variable[[1]]}")` {.tabset .tabset-fade .tabset-pills} 229 | 230 | #### Decile analysis 231 | 232 | ```{r} 233 | credit_data_nested$decile_table[[1]] 234 | ``` 235 | 236 | ```{r} 237 | credit_data_nested$plot_deciles[[1]] 238 | ``` 239 | 240 | #### Aditional plots 241 | 242 | ```{r} 243 | credit_data_nested$plot_boxplot[[1]] 244 | ``` 245 | 246 | ```{r} 247 | credit_data_nested$plot_density[[1]] 248 | ``` 249 | 250 | ### `r glue::glue("Variable: {credit_data_nested$variable[[2]]}")` {.tabset .tabset-fade .tabset-pills} 251 | 252 | #### Decile analysis 253 | 254 | ```{r} 255 | credit_data_nested$decile_table[[2]] 256 | ``` 257 | 258 | ```{r} 259 | credit_data_nested$plot_deciles[[2]] 260 | ``` 261 | 262 | #### Aditional plots 263 | 264 | ```{r} 265 | credit_data_nested$plot_boxplot[[2]] 266 | ``` 267 | 268 | ```{r} 269 | credit_data_nested$plot_density[[2]] 270 | ``` 271 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | # ggrapid: Create neat & complete ggplot visualizations with as little code as possible 5 | 6 | ## Overview 7 | 8 | ggrapid enables creation of the most common ggplot-based visualizations 9 | fast and with just a few lines of code. In practice the package offers 10 | wrappers of some of the most common ggplot geoms such as: geom\_density, 11 | geom\_boxplot, geom\_bar etc. ggrapid comes handy when you’d like to do 12 | an initial and quick EDA (Exploratory Data Analysis) over various 13 | columns of your dataset programatically, without the need of writing a 14 | lot of custom ggplot code. 15 | 16 | ## Installation 17 | 18 | ``` r 19 | # Install development version from GitHub 20 | devtools::install_github("konradsemsch/ggrapid") 21 | ``` 22 | 23 | ## Main functions 24 | 25 | ggrapid offers a couple wrappers around the most commonly used functions 26 | in the course of doing an EDA: 27 | 28 | - `plot_density` 29 | - `plot_boxplot` 30 | - `plot_deciles` (with `calculate_decile_table`) 31 | - `plot_correlation` 32 | - `plot_bars` 33 | - `plot_line` 34 | 35 | #### Density plot 36 | 37 | ``` r 38 | diamonds %>% 39 | plot_density(x = carat) 40 | ``` 41 | 42 | ![](man/figures/unnamed-chunk-3-1.png) 43 | 44 | #### Box-plot 45 | 46 | ``` r 47 | diamonds %>% 48 | plot_boxplot(x = cut, 49 | y = carat, 50 | fill = cut) 51 | ``` 52 | 53 | ![](man/figures/unnamed-chunk-4-1.png) 54 | 55 | #### Decile plot 56 | 57 | ``` r 58 | diamonds %>% 59 | filter(cut %in% c("Ideal", "Premium")) %>% 60 | calculate_decile_table(price, cut, "Ideal") %>% 61 | plot_deciles() 62 | ``` 63 | 64 | ![](man/figures/unnamed-chunk-5-1.png) 65 | 66 | #### Correlation 67 | 68 | ``` r 69 | diamonds %>% 70 | plot_correlation() 71 | ``` 72 | 73 | ![](man/figures/unnamed-chunk-6-1.png) 74 | 75 | #### Barplot 76 | 77 | ``` r 78 | diamonds %>% 79 | plot_bars(x = carat, 80 | x_type = "num", 81 | fill = cut) 82 | ``` 83 | 84 | ![](man/figures/unnamed-chunk-7-1.png) 85 | 86 | #### Lineplot 87 | 88 | ``` r 89 | tibble( 90 | time = 1:20, 91 | value = rnorm(20, 0.5, 2) 92 | ) %>% 93 | plot_line( 94 | x = time, 95 | y = value 96 | ) 97 | ``` 98 | 99 | ![](man/figures/unnamed-chunk-8-1.png) 100 | 101 | ## Main arguments 102 | 103 | The most commonly implemented ggplot2 arguments across all main ggrapid 104 | functions ensure that you can build your basic EDA file without making 105 | additional changes or custom functions. Those arguments are mainly 106 | (might slightly differ across functions): 107 | 108 | - fill 109 | - facet 110 | - position 111 | - ticks 112 | - angle 113 | - title 114 | - subtitle 115 | - caption 116 | - lab\_x 117 | - lab\_y 118 | - legend 119 | - vline/ hline 120 | - alpha 121 | - quantile\_low 122 | - quantile\_high 123 | - theme\_type 124 | - palette 125 | 126 | 127 | 128 | ``` r 129 | diamonds %>% 130 | plot_density(x = carat) 131 | ``` 132 | 133 | ![](man/figures/unnamed-chunk-9-1.png) 134 | 135 | ``` r 136 | diamonds %>% 137 | plot_density(x = carat, 138 | fill = cut, 139 | position = "stack") 140 | ``` 141 | 142 | ![](man/figures/unnamed-chunk-10-1.png) 143 | 144 | ``` r 145 | diamonds %>% 146 | plot_density(x = carat, 147 | fill = cut, 148 | position = "fill") 149 | ``` 150 | 151 | ![](man/figures/unnamed-chunk-11-1.png) 152 | 153 | ``` r 154 | diamonds %>% 155 | plot_density(x = carat, 156 | fill = cut, 157 | facet = cut, 158 | title = "Write your title here", 159 | subtitle = "Write your subtitle here", 160 | caption = "Write your caption here", 161 | lab_x = "Carat", 162 | alpha = .5, 163 | vline = 1) 164 | ``` 165 | 166 | ![](man/figures/unnamed-chunk-12-1.png) 167 | 168 | ## Complete usage 169 | 170 | You can easily iterate across selected columns and create a set of plots 171 | for your EDA file: 172 | 173 | ``` r 174 | 175 | library(recipes) 176 | 177 | credit_data_nested <- credit_data %>% 178 | select(-one_of("Home", "Marital", "Records", "Job")) %>% # removing categorical variables 179 | gather(variable, variable_value, 180 | one_of("Seniority", "Time", "Age", "Expenses", # selecting variables to gather 181 | "Income", "Assets", "Debt", "Amount", "Price")) %>% 182 | nest(-variable) %>% 183 | mutate( 184 | decile_table = map(data, 185 | ~calculate_decile_table( 186 | .x, 187 | binning = variable_value, 188 | grouping = Status, 189 | top_level = "bad", 190 | format = FALSE 191 | ) 192 | ), 193 | plot_deciles = pmap(list(x = decile_table, y = variable), 194 | ~plot_deciles( 195 | .x, 196 | title = glue::glue("Decile plot of {.y}"), 197 | quantile_low = 0, 198 | quantile_high = 1, 199 | lab_x = "Decile", 200 | lab_y = "Bad rate, %" 201 | ) 202 | ), 203 | plot_boxplot = pmap(list(x = data, y = variable), 204 | ~plot_boxplot( 205 | .x, 206 | x = Status, 207 | y = variable_value, 208 | fill = Status, 209 | title = glue::glue("Box plot of {.y} by Status"), 210 | quantile_low = 0.01, 211 | quantile_high = 0.99, 212 | lab_x = "Performance", 213 | caption = "Removed 1% of observations from each side", 214 | palette = "inv_binary" 215 | ) 216 | ), 217 | plot_density = pmap(list(x = data, y = variable), 218 | ~plot_density( 219 | .x, 220 | x = variable_value, 221 | fill = Status, 222 | title = glue::glue("Box plot of {.y} by Status"), 223 | quantile_low = 0.01, 224 | quantile_high = 0.99, 225 | lab_x = "Performance", 226 | caption = "Removed 1% of observations from each side", 227 | palette = "inv_binary" 228 | ) 229 | ) 230 | ) 231 | ``` 232 | 233 | This will give you the following structure. Each row represents an 234 | individual variable and columns are the different plots you would like 235 | to inspect: 236 | 237 | ``` r 238 | credit_data_nested[1:3, ] 239 | #> # A tibble: 3 x 6 240 | #> variable data decile_table plot_deciles plot_boxplot plot_density 241 | #> 242 | #> 1 Seniority 243 | #> 2 Time 244 | #> 3 Age 245 | ``` 246 | 247 | ## Exemplary EDA format 248 | 249 | Creating a standardised EDA file is just as easy as doing something like 250 | this: 251 | 252 | ### Variable: Seniority 253 | 254 | #### Decile analysis 255 | 256 | ``` r 257 | credit_data_nested$decile_table[[1]] 258 | #> # A tibble: 10 x 8 259 | #> decile min median max top_level total bottom_level ratio 260 | #> 261 | #> 1 1 0 0 0 235 446 211 52.69% 262 | #> 2 2 0 1 1 209 445 236 46.97% 263 | #> 3 3 1 2 2 174 446 272 39.01% 264 | #> 4 4 2 3 3 146 445 299 32.81% 265 | #> 5 5 3 4 5 122 445 323 27.42% 266 | #> 6 6 5 6 8 105 446 341 23.54% 267 | #> 7 7 8 10 10 88 445 357 19.78% 268 | #> 8 8 10 12 14 76 446 370 17.04% 269 | #> 9 9 14 16 20 54 445 391 12.13% 270 | #> 10 10 20 25 48 45 445 400 10.11% 271 | ``` 272 | 273 | ``` r 274 | credit_data_nested$plot_deciles[[1]] 275 | ``` 276 | 277 | ![](man/figures/unnamed-chunk-16-1.png) 278 | 279 | #### Aditional plots 280 | 281 | ``` r 282 | credit_data_nested$plot_boxplot[[1]] 283 | ``` 284 | 285 | ![](man/figures/unnamed-chunk-17-1.png) 286 | 287 | ``` r 288 | credit_data_nested$plot_density[[1]] 289 | ``` 290 | 291 | ![](man/figures/unnamed-chunk-18-1.png) 292 | 293 | ### Variable: Time 294 | 295 | #### Decile analysis 296 | 297 | ``` r 298 | credit_data_nested$decile_table[[2]] 299 | #> # A tibble: 10 x 8 300 | #> decile min median max top_level total bottom_level ratio 301 | #> 302 | #> 1 1 6 18 24 64 446 382 14.35% 303 | #> 2 2 24 30 36 109 445 336 24.49% 304 | #> 3 3 36 36 36 124 446 322 27.80% 305 | #> 4 4 36 36 48 136 445 309 30.56% 306 | #> 5 5 48 48 48 135 445 310 30.34% 307 | #> 6 6 48 48 60 125 446 321 28.03% 308 | #> 7 7 60 60 60 133 445 312 29.89% 309 | #> 8 8 60 60 60 155 446 291 34.75% 310 | #> 9 9 60 60 60 129 445 316 28.99% 311 | #> 10 10 60 60 72 144 445 301 32.36% 312 | ``` 313 | 314 | ``` r 315 | credit_data_nested$plot_deciles[[2]] 316 | ``` 317 | 318 | ![](man/figures/unnamed-chunk-20-1.png) 319 | 320 | #### Aditional plots 321 | 322 | ``` r 323 | credit_data_nested$plot_boxplot[[2]] 324 | ``` 325 | 326 | ![](man/figures/unnamed-chunk-21-1.png) 327 | 328 | ``` r 329 | credit_data_nested$plot_density[[2]] 330 | ``` 331 | 332 | ![](man/figures/unnamed-chunk-22-1.png) 333 | -------------------------------------------------------------------------------- /data/diamonds.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konradsemsch/ggrapid/f74b67e6402c4f9b4442701e5c6a136106ff7d06/data/diamonds.rda -------------------------------------------------------------------------------- /ggrapid.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: No 4 | SaveWorkspace: No 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: knitr 13 | LaTeX: pdfLaTeX 14 | 15 | AutoAppendNewline: Yes 16 | StripTrailingWhitespace: Yes 17 | 18 | BuildType: Package 19 | PackageUseDevtools: Yes 20 | PackageInstallArgs: --no-multiarch --with-keep.source 21 | PackageRoxygenize: rd,collate,namespace 22 | -------------------------------------------------------------------------------- /inst/extdata/ggtesting: -------------------------------------------------------------------------------- 1 | test_that("output of ggplot() is stable", { 2 | vdiffr::expect_doppelganger("A blank plot", ggplot()) 3 | }) 4 | 5 | change tidy-eval syntax in calculate_decile and other functions if there's still the old one -------------------------------------------------------------------------------- /inst/extdata/plot_bars_examples.R: -------------------------------------------------------------------------------- 1 | #' library(dplyr) 2 | #' 3 | #' diamonds_sum <- diamonds %>% 4 | #' group_by(cut) %>% 5 | #' summarise(mean_carat = mean(carat, na.rm = TRUE)) 6 | #' 7 | #' diamonds %>% 8 | #' plot_bars(x = carat, 9 | #' x_type = "num", 10 | #' fill = cut, 11 | #' facet = color) 12 | #' 13 | #' # Not fully working 14 | #' diamonds %>% 15 | #' plot_bars(x = carat, 16 | #' x_type = "num", 17 | #' fill = cut, 18 | #' position = "stack", 19 | #' facet = color, 20 | #' # binwidth = 50, 21 | #' vline = 45, 22 | #' angle = 45, 23 | #' alpha = .7) 24 | #' 25 | #' # for generating counts 26 | #' diamonds %>% 27 | #' plot_bars(x = cut, 28 | #' x_type = "char", 29 | #' y_prop = FALSE) 30 | #' 31 | #' diamonds %>% 32 | #' plot_bars(x = cut, 33 | #' x_type = "char", 34 | #' position = "dodge", 35 | #' fill = cut, 36 | #' facet = color) 37 | #' 38 | #' # for generating proportions 39 | #' diamonds %>% 40 | #' plot_bars(x = cut, 41 | #' x_type = "char", 42 | #' y_prop = TRUE, 43 | #' position = "fill", 44 | #' fill = color) 45 | #' 46 | #' diamonds_sum %>% 47 | #' plot_bars(x = cut, 48 | #' y = mean_carat, 49 | #' x_type = "char", 50 | #' stat = "identity") -------------------------------------------------------------------------------- /man/apply_theme.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/helpers.R 3 | \name{apply_theme} 4 | \alias{apply_theme} 5 | \title{Apply a neat, customized ggplot2 theme} 6 | \usage{ 7 | apply_theme(theme_type = "ipsum") 8 | } 9 | \arguments{ 10 | \item{theme_type}{Type of a base hrbrthemes theme to use. Currently only "ipsum" is supported.} 11 | } 12 | \description{ 13 | This function applies a customized ggplot2 theme (based on the hrbrthemes package) 14 | to any ggplot graph in order to create neat and good looking visualizations. 15 | } 16 | -------------------------------------------------------------------------------- /man/calculate_decile_table.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/calculate.R 3 | \name{calculate_decile_table} 4 | \alias{calculate_decile_table} 5 | \title{Calculate a decile breakdown} 6 | \usage{ 7 | calculate_decile_table(df, binning, grouping, top_level = "1", 8 | n_bins = 10, format = FALSE, ...) 9 | } 10 | \arguments{ 11 | \item{df}{A data frame.} 12 | 13 | \item{binning}{Variable for which binning should be applied.} 14 | 15 | \item{grouping}{A two-level (binary) variable to calculate the ratio in each bin.} 16 | 17 | \item{top_level}{Top level of the grouping variable. Defaults to "1".} 18 | 19 | \item{n_bins}{Provide a number of bins. Defaults to 10.} 20 | 21 | \item{format}{Should table printing be formatted with kable? Defaults to FALSE} 22 | 23 | \item{...}{Additional grouping columns to calculate deciles} 24 | } 25 | \description{ 26 | This function calculates a decile table for any combination of numerical and categorical variables. 27 | } 28 | \examples{ 29 | library(tidyverse) 30 | 31 | diamonds_filter <- diamonds \%>\% filter(cut \%in\% c("Ideal", "Premium")) 32 | 33 | diamonds_filter \%>\% 34 | calculate_decile_table(price, cut, "Ideal") 35 | 36 | diamonds_filter \%>\% 37 | calculate_decile_table(binning = price, 38 | grouping = cut, 39 | top_level = "Ideal", 40 | n_bins = 10, 41 | format = FALSE, 42 | color) 43 | } 44 | -------------------------------------------------------------------------------- /man/diamonds.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/data.R 3 | \docType{data} 4 | \name{diamonds} 5 | \alias{diamonds} 6 | \title{Diamonds dataset used for examples} 7 | \format{An object of class \code{tbl_df} (inherits from \code{tbl}, \code{data.frame}) with 53940 rows and 10 columns.} 8 | \usage{ 9 | diamonds 10 | } 11 | \description{ 12 | See \code{\link[ggplot2]{diamonds}} for details. 13 | } 14 | \keyword{datasets} 15 | -------------------------------------------------------------------------------- /man/figures/unnamed-chunk-10-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konradsemsch/ggrapid/f74b67e6402c4f9b4442701e5c6a136106ff7d06/man/figures/unnamed-chunk-10-1.png -------------------------------------------------------------------------------- /man/figures/unnamed-chunk-11-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konradsemsch/ggrapid/f74b67e6402c4f9b4442701e5c6a136106ff7d06/man/figures/unnamed-chunk-11-1.png -------------------------------------------------------------------------------- /man/figures/unnamed-chunk-12-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konradsemsch/ggrapid/f74b67e6402c4f9b4442701e5c6a136106ff7d06/man/figures/unnamed-chunk-12-1.png -------------------------------------------------------------------------------- /man/figures/unnamed-chunk-16-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konradsemsch/ggrapid/f74b67e6402c4f9b4442701e5c6a136106ff7d06/man/figures/unnamed-chunk-16-1.png -------------------------------------------------------------------------------- /man/figures/unnamed-chunk-17-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konradsemsch/ggrapid/f74b67e6402c4f9b4442701e5c6a136106ff7d06/man/figures/unnamed-chunk-17-1.png -------------------------------------------------------------------------------- /man/figures/unnamed-chunk-18-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konradsemsch/ggrapid/f74b67e6402c4f9b4442701e5c6a136106ff7d06/man/figures/unnamed-chunk-18-1.png -------------------------------------------------------------------------------- /man/figures/unnamed-chunk-20-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konradsemsch/ggrapid/f74b67e6402c4f9b4442701e5c6a136106ff7d06/man/figures/unnamed-chunk-20-1.png -------------------------------------------------------------------------------- /man/figures/unnamed-chunk-21-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konradsemsch/ggrapid/f74b67e6402c4f9b4442701e5c6a136106ff7d06/man/figures/unnamed-chunk-21-1.png -------------------------------------------------------------------------------- /man/figures/unnamed-chunk-22-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konradsemsch/ggrapid/f74b67e6402c4f9b4442701e5c6a136106ff7d06/man/figures/unnamed-chunk-22-1.png -------------------------------------------------------------------------------- /man/figures/unnamed-chunk-3-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konradsemsch/ggrapid/f74b67e6402c4f9b4442701e5c6a136106ff7d06/man/figures/unnamed-chunk-3-1.png -------------------------------------------------------------------------------- /man/figures/unnamed-chunk-4-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konradsemsch/ggrapid/f74b67e6402c4f9b4442701e5c6a136106ff7d06/man/figures/unnamed-chunk-4-1.png -------------------------------------------------------------------------------- /man/figures/unnamed-chunk-5-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konradsemsch/ggrapid/f74b67e6402c4f9b4442701e5c6a136106ff7d06/man/figures/unnamed-chunk-5-1.png -------------------------------------------------------------------------------- /man/figures/unnamed-chunk-6-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konradsemsch/ggrapid/f74b67e6402c4f9b4442701e5c6a136106ff7d06/man/figures/unnamed-chunk-6-1.png -------------------------------------------------------------------------------- /man/figures/unnamed-chunk-7-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konradsemsch/ggrapid/f74b67e6402c4f9b4442701e5c6a136106ff7d06/man/figures/unnamed-chunk-7-1.png -------------------------------------------------------------------------------- /man/figures/unnamed-chunk-8-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konradsemsch/ggrapid/f74b67e6402c4f9b4442701e5c6a136106ff7d06/man/figures/unnamed-chunk-8-1.png -------------------------------------------------------------------------------- /man/figures/unnamed-chunk-9-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/konradsemsch/ggrapid/f74b67e6402c4f9b4442701e5c6a136106ff7d06/man/figures/unnamed-chunk-9-1.png -------------------------------------------------------------------------------- /man/first_to_lower.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/tweak.R 3 | \name{first_to_lower} 4 | \alias{first_to_lower} 5 | \title{Convert strings first letter to lowercase} 6 | \usage{ 7 | first_to_lower(x) 8 | } 9 | \arguments{ 10 | \item{x}{A data frame or vector.} 11 | } 12 | \description{ 13 | This function converts your data frame column names or character vector elements 14 | first letter to lowercase. It can be used in a pipe. 15 | } 16 | \examples{ 17 | first_to_lower(c("first", "second")) 18 | } 19 | -------------------------------------------------------------------------------- /man/first_to_upper.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/tweak.R 3 | \name{first_to_upper} 4 | \alias{first_to_upper} 5 | \title{Convert strings first letter to uppercase} 6 | \usage{ 7 | first_to_upper(x) 8 | } 9 | \arguments{ 10 | \item{x}{A data frame or vector.} 11 | } 12 | \description{ 13 | This function converts your data frame column names or character vector elements 14 | first letter to uppercase. It can be used in a pipe. 15 | } 16 | \examples{ 17 | first_to_upper(c("first", "second")) 18 | 19 | diamonds \%>\% 20 | first_to_upper() 21 | } 22 | -------------------------------------------------------------------------------- /man/number_ticks.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/helpers.R 3 | \name{number_ticks} 4 | \alias{number_ticks} 5 | \title{Specify equal sized axis ticks} 6 | \usage{ 7 | number_ticks(n = 10) 8 | } 9 | \arguments{ 10 | \item{n}{Number of desired ticks on an axis. Defaults to 10.} 11 | } 12 | \description{ 13 | This function creates equally spaced axis ticks for ggplot graphs. Should be used as input 14 | for the "break" argument of scale_continuous function in a ggplot function. 15 | } 16 | -------------------------------------------------------------------------------- /man/pipe.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/utils.R 3 | \name{\%>\%} 4 | \alias{\%>\%} 5 | \title{Pipe operator} 6 | \usage{ 7 | lhs \%>\% rhs 8 | } 9 | \description{ 10 | See \code{magrittr::\link[magrittr]{\%>\%}} for details. 11 | } 12 | \keyword{internal} 13 | -------------------------------------------------------------------------------- /man/plot_bars.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/plot_bars.R 3 | \name{plot_bars} 4 | \alias{plot_bars} 5 | \title{Plot bars of numerical variables} 6 | \usage{ 7 | plot_bars(df, x, y = NULL, y_prop = FALSE, x_type = "num", 8 | fill = NULL, facet = NULL, binwidth = NULL, position = "stack", 9 | stat = "count", fct_order = FALSE, angle = 0, title = TRUE, 10 | subtitle = NULL, caption = NULL, lab_x = "Value range", 11 | lab_y = "Proportion", legend = TRUE, vline = c(NaN), alpha = 0.7, 12 | quantile_low = 0, quantile_high = 1, theme_type = "ipsum", 13 | palette = "awtools") 14 | } 15 | \arguments{ 16 | \item{df}{A data frame.} 17 | 18 | \item{x}{A numeric/ categorical variable to plot the bar plot.} 19 | 20 | \item{y}{A numeric variable which contains summarised y values (before plotting). Use only with stat = "identity".} 21 | 22 | \item{y_prop}{A logical variable to choose between counts/ proportion on the y axis. Defaults to FALSE (proportion).} 23 | 24 | \item{x_type}{Character identifier for type of the variable x defined above: "num" for numeric (plots histogram) and "char" for character (plots bar chart). Defauls to "num".} 25 | 26 | \item{fill}{Select an additional grouping variable to be used for plotting. Defaults to NULL.} 27 | 28 | \item{facet}{Select an additional faceting variable to create facets. Defaults to NULL.} 29 | 30 | \item{binwidth}{Select binwidth, defaults to NULL and lets ggplot select the optimal binwidth.} 31 | 32 | \item{position}{Select the position of the barplot from: "stack" (default), "dodge" or "fill".} 33 | 34 | \item{stat}{Character identifier for whether the data is already grouped ("identity") or if the function needs to aggregate data at the level of x ("count").} 35 | 36 | \item{fct_order}{Should the factors be reordered by their frequency? Defaults to FALSE.} 37 | 38 | \item{angle}{Select the rotation angle for the x axis labels. Defaults to 0.} 39 | 40 | \item{title}{Should the plot title appear automatically. Defaults to TRUE.} 41 | 42 | \item{subtitle}{Text that is displayed on the subtitle. Defaults to NULL.} 43 | 44 | \item{caption}{Text that is displayed on the caption. Defaults to NULL.} 45 | 46 | \item{lab_x}{Text that is displayed on the x axis. Defaults to "Level".} 47 | 48 | \item{lab_y}{Text that is displayed on the y axis. Defaults to "Value range".} 49 | 50 | \item{legend}{Should the plot legend appear automatically. Defaults to TRUE.} 51 | 52 | \item{vline}{Should any horizontal lines be added to the plot. Defaults to c(NaN).} 53 | 54 | \item{alpha}{Select plot fill transparency. Defaults to 0.7.} 55 | 56 | \item{quantile_low}{Select lower percentile for outliers exclusion. Defaults to 0.0\%.} 57 | 58 | \item{quantile_high}{Select upper percentile for outliers exclusion. Defaults to 1.0\%.} 59 | 60 | \item{theme_type}{Select a theme type from themes available in the \code{\link{apply_theme}} function. Defaults to "ipsum".} 61 | 62 | \item{palette}{Select a palette type with the \code{\link{select_palette}} function. Defaults to "awtools".} 63 | } 64 | \description{ 65 | This function creates nicely formatted, standardized bar-plots. 66 | } 67 | \examples{ 68 | library(tidyverse) 69 | 70 | diamonds_sum <- diamonds \%>\% 71 | group_by(cut) \%>\% 72 | summarise(mean_carat = mean(carat, na.rm = TRUE)) 73 | 74 | } 75 | -------------------------------------------------------------------------------- /man/plot_boxplot.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/plot_boxplot.R 3 | \name{plot_boxplot} 4 | \alias{plot_boxplot} 5 | \title{Plot box-plots of numerical variables} 6 | \usage{ 7 | plot_boxplot(df, x, y, fill = NULL, facet = NULL, ticks = 10, 8 | angle = 0, title = TRUE, subtitle = NULL, caption = NULL, 9 | lab_x = "Level", lab_y = "Value range", legend = TRUE, 10 | vline = c(NaN), alpha = 0.7, quantile_low = 0, quantile_high = 1, 11 | theme_type = "ipsum", palette = "awtools") 12 | } 13 | \arguments{ 14 | \item{df}{A data frame.} 15 | 16 | \item{x}{A categorical variable for the x axis groupings.} 17 | 18 | \item{y}{A numerical variable for the y axis levels.} 19 | 20 | \item{fill}{Select an additional grouping variable to be used for plotting. Defaults to NULL.} 21 | 22 | \item{facet}{Select an additional faceting variable to create facets. Defaults to NULL.} 23 | 24 | \item{ticks}{Select the number of ticks on the x and y axis with the \code{\link{number_ticks}} function. Defaults to 10.} 25 | 26 | \item{angle}{Select the rotation angle for the x axis labels. Defaults to 0.} 27 | 28 | \item{title}{Should the plot title appear automatically. Defaults to TRUE.} 29 | 30 | \item{subtitle}{Text that is displayed on the subtitle. Defaults to NULL.} 31 | 32 | \item{caption}{Text that is displayed on the caption. Defaults to NULL.} 33 | 34 | \item{lab_x}{Text that is displayed on the x axis. Defaults to "Level".} 35 | 36 | \item{lab_y}{Text that is displayed on the y axis. Defaults to "Value range".} 37 | 38 | \item{legend}{Should the plot legend appear automatically. Defaults to TRUE.} 39 | 40 | \item{vline}{Should any horizontal lines be added to the plot. Defaults to c(NaN).} 41 | 42 | \item{alpha}{Select plot fill transparency. Defaults to 0.7.} 43 | 44 | \item{quantile_low}{Select lower percentile for outliers exclusion. Defaults to 0.0\%.} 45 | 46 | \item{quantile_high}{Select upper percentile for outliers exclusion. Defaults to 1.0\%.} 47 | 48 | \item{theme_type}{Select a theme type from themes available in the \code{\link{apply_theme}} function. Defaults to "ipsum".} 49 | 50 | \item{palette}{Select a palette type with the \code{\link{select_palette}} function. Defaults to "awtools".} 51 | } 52 | \description{ 53 | This function creates nicely formatted, standardized box-plots. 54 | } 55 | \examples{ 56 | diamonds \%>\% 57 | plot_boxplot(x = cut, 58 | y = carat) 59 | 60 | diamonds \%>\% 61 | plot_boxplot(x = cut, 62 | y = carat, 63 | fill = color) 64 | 65 | diamonds \%>\% 66 | plot_boxplot(x = cut, 67 | y = carat, 68 | fill = cut, 69 | facet = color) 70 | 71 | diamonds \%>\% 72 | plot_boxplot(x = cut, 73 | y = carat, 74 | fill = cut, 75 | title = "Write your title here", 76 | subtitle = "Write your subtitle here", 77 | caption = "Write your caption here", 78 | lab_x = "Carat", 79 | alpha = .5, 80 | vline = 1) 81 | } 82 | -------------------------------------------------------------------------------- /man/plot_correlation.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/plot_correlation.R 3 | \name{plot_correlation} 4 | \alias{plot_correlation} 5 | \title{Plot a correlation matrix of numerical variables} 6 | \usage{ 7 | plot_correlation(df, method = "spearman", order = "alphabet", 8 | label_size = 0.7, number_size = 0.7) 9 | } 10 | \arguments{ 11 | \item{df}{A data frame.} 12 | 13 | \item{method}{A character string indicating which correlation coefficient (or covariance) should be computed. Options are: "spearman" (default), "pearson" or "kendall".} 14 | 15 | \item{order}{Ordering method of the correlation matrix. Options are: "alphabet" (default) and "hclust".} 16 | 17 | \item{label_size}{Size of the text label. Defaults to 0.7.} 18 | 19 | \item{number_size}{Size of the correlation number. Defaults to 0.9.} 20 | } 21 | \description{ 22 | This function creates a nicely formatted, standardised correlation matrix of numerical variables. 23 | Make sure that long variables names are are shortened before using the function for easier interpretation. 24 | } 25 | \examples{ 26 | diamonds \%>\% 27 | plot_correlation() 28 | 29 | } 30 | -------------------------------------------------------------------------------- /man/plot_deciles.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/plot_deciles.R 3 | \name{plot_deciles} 4 | \alias{plot_deciles} 5 | \title{Plot decile plots of numerical variables} 6 | \usage{ 7 | plot_deciles(df, x = decile, y = ratio, facet = NULL, ticks = 10, 8 | angle = 0, title = TRUE, subtitle = NULL, caption = NULL, 9 | lab_x = "Decile", lab_y = "Ratio", legend = TRUE, alpha = 0.7, 10 | quantile_low = 0, quantile_high = 1, theme_type = "ipsum", 11 | palette = "awtools") 12 | } 13 | \arguments{ 14 | \item{df}{A data frame.} 15 | 16 | \item{x}{A categorical variable for the x axis groupings. Defaults to 'decile'.} 17 | 18 | \item{y}{A numerical variable for the y axis levels. Defaults to 'ratio'.} 19 | 20 | \item{facet}{Select an additional faceting variable to create facets. Defaults to NULL.} 21 | 22 | \item{ticks}{Select the number of ticks on the x and y axis with the \code{\link{number_ticks}} function. Defaults to 10.} 23 | 24 | \item{angle}{Select the rotation angle for the x axis labels. Defaults to 0.} 25 | 26 | \item{title}{Should the plot title appear automatically. Defaults to TRUE.} 27 | 28 | \item{subtitle}{Text that is displayed on the subtitle. Defaults to NULL.} 29 | 30 | \item{caption}{Text that is displayed on the caption. Defaults to NULL.} 31 | 32 | \item{lab_x}{Text that is displayed on the x axis. Defaults to "Decile".} 33 | 34 | \item{lab_y}{Text that is displayed on the y axis. Defaults to "Percentage".} 35 | 36 | \item{legend}{Should the plot legend appear automatically. Defaults to TRUE.} 37 | 38 | \item{alpha}{Select plot fill transparency. Defaults to 0.7.} 39 | 40 | \item{quantile_low}{Select lower percentile for outliers exclusion. Defaults to 0.0\%.} 41 | 42 | \item{quantile_high}{Select upper percentile for outliers exclusion. Defaults to 1.0\%.} 43 | 44 | \item{theme_type}{Select a theme type from themes available in the \code{\link{apply_theme}} function. Defaults to "ipsum".} 45 | 46 | \item{palette}{Select a palette type with the \code{\link{select_palette}} function. Defaults to "awtools".} 47 | } 48 | \description{ 49 | This function creates nicely formatted, standardised decile plots. Prior to calling the function 50 | the data should only be in a form of a decile table \code{\link{calculate_decile_table}}. Above 51 | each bar the median value of the bin is displayed. 52 | } 53 | \examples{ 54 | library(tidyverse) 55 | 56 | diamonds_filter <- diamonds \%>\% filter(cut \%in\% c("Ideal", "Premium")) 57 | 58 | diamonds_filter \%>\% 59 | calculate_decile_table(price, cut, "Ideal") \%>\% 60 | plot_deciles() 61 | 62 | diamonds_filter \%>\% 63 | calculate_decile_table(binning = price, 64 | grouping = cut, 65 | top_level = "Ideal", 66 | n_bins = 10, 67 | format = FALSE, 68 | color) \%>\% 69 | plot_deciles(facet = color) 70 | } 71 | -------------------------------------------------------------------------------- /man/plot_density.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/plot_density.R 3 | \name{plot_density} 4 | \alias{plot_density} 5 | \title{Plot density of numerical variables} 6 | \usage{ 7 | plot_density(df, x, fill = NULL, facet = NULL, position = "identity", 8 | ticks = 5, angle = 0, title = TRUE, subtitle = NULL, 9 | caption = NULL, lab_x = "Value range", lab_y = "Density", 10 | legend = TRUE, vline = c(NaN), alpha = 0.7, quantile_low = 0, 11 | quantile_high = 1, theme_type = "ipsum", palette = "awtools") 12 | } 13 | \arguments{ 14 | \item{df}{A data frame.} 15 | 16 | \item{x}{A numerical variable to plot its density.} 17 | 18 | \item{fill}{Select an additional grouping variable to be used for density plotting. Defaults to NULL.} 19 | 20 | \item{facet}{Select an additional faceting variable to create facets. Defaults to NULL.} 21 | 22 | \item{position}{Select the positional adjustment of the plot. Possible values are: "identity" (default), "stack" or "fill"} 23 | 24 | \item{ticks}{Select the number of ticks on the x and y axis with the \code{\link{number_ticks}} function. Defaults to 10.} 25 | 26 | \item{angle}{Select the rotation angle for the x axis labels. Defaults to 0.} 27 | 28 | \item{title}{Should the plot title appear automatically. Defaults to TRUE.} 29 | 30 | \item{subtitle}{Text that is displayed on the subtitle. Defaults to NULL.} 31 | 32 | \item{caption}{Text that is displayed on the caption. Defaults to NULL.} 33 | 34 | \item{lab_x}{Text that is displayed on the x axis. Defaults to "Value range".} 35 | 36 | \item{lab_y}{Text that is displayed on the y axis. Defaults to "Density".} 37 | 38 | \item{legend}{Should the plot legend appear automatically. Defaults to TRUE.} 39 | 40 | \item{vline}{Should any vertical lines be added to the plot. Defaults to c(NaN).} 41 | 42 | \item{alpha}{Select plot fill transparency. Defaults to 0.7.} 43 | 44 | \item{quantile_low}{Select lower percentile for outliers exclusion. Defaults to 0.0\%.} 45 | 46 | \item{quantile_high}{Select upper percentile for outliers exclusion. Defaults to 1.0\%.} 47 | 48 | \item{theme_type}{Select a theme type from themes available in the \code{\link{apply_theme}} function. Defaults to "ipsum".} 49 | 50 | \item{palette}{Select a palette type with the \code{\link{select_palette}} function. Defaults to "awtools".} 51 | } 52 | \description{ 53 | This function creates nicely formatted, standardized density plots. 54 | } 55 | \examples{ 56 | diamonds \%>\% 57 | plot_density(x = carat) 58 | 59 | diamonds \%>\% 60 | plot_density(x = carat, 61 | fill = cut) 62 | 63 | diamonds \%>\% 64 | plot_density(x = carat, 65 | fill = cut, 66 | position = "stack") 67 | 68 | diamonds \%>\% 69 | plot_density(x = carat, 70 | fill = cut, 71 | position = "fill") 72 | 73 | diamonds \%>\% 74 | plot_density(x = carat, 75 | facet = cut) 76 | 77 | diamonds \%>\% 78 | plot_density(x = carat, 79 | fill = cut, 80 | facet = cut, 81 | alpha = .5) 82 | 83 | diamonds \%>\% 84 | plot_density(x = carat, 85 | fill = cut, 86 | facet = cut, 87 | title = "Write your title here", 88 | subtitle = "Write your subtitle here", 89 | caption = "Write your caption here", 90 | lab_x = "Carat", 91 | alpha = .5, 92 | vline = 1) 93 | } 94 | -------------------------------------------------------------------------------- /man/plot_line.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/plot_lines.R 3 | \name{plot_line} 4 | \alias{plot_line} 5 | \title{Plot lines of numerical variables. Usefull especially for time-series data} 6 | \usage{ 7 | plot_line(df, x, y, fill = NULL, facet = NULL, ticks = 10, 8 | angle = 0, title = TRUE, subtitle = NULL, caption = NULL, 9 | lab_x = "Value range", lab_y = "Value range", legend = TRUE, 10 | hline = c(NaN), alpha = 0.7, limit_min = NA, limit_max = NA, 11 | theme_type = "ipsum", palette = "awtools") 12 | } 13 | \arguments{ 14 | \item{df}{A data frame.} 15 | 16 | \item{x}{A categorical variable for the x axis groupings.} 17 | 18 | \item{y}{A numerical variable for the y axis levels.} 19 | 20 | \item{fill}{Select an additional grouping variable to be used for plotting. Defaults to NULL.} 21 | 22 | \item{facet}{Select an additional faceting variable to create facets. Defaults to NULL.} 23 | 24 | \item{ticks}{Select the number of ticks on the x and y axis with the \code{\link{number_ticks}} function. Defaults to 10.} 25 | 26 | \item{angle}{Select the rotation angle for the x axis labels. Defaults to 0.} 27 | 28 | \item{title}{Should the plot title appear automatically. Defaults to TRUE.} 29 | 30 | \item{subtitle}{Text that is displayed on the subtitle. Defaults to NULL.} 31 | 32 | \item{caption}{Text that is displayed on the caption. Defaults to NULL.} 33 | 34 | \item{lab_x}{Text that is displayed on the x axis. Defaults to "Value range".} 35 | 36 | \item{lab_y}{Text that is displayed on the y axis. Defaults to "Value range".} 37 | 38 | \item{legend}{Should the plot legend appear automatically. Defaults to TRUE.} 39 | 40 | \item{hline}{Should any horizontal lines be added to the plot. Defaults to c(NaN).} 41 | 42 | \item{alpha}{Select plot fill transparency. Defaults to 0.7.} 43 | 44 | \item{limit_min}{Select lower limit for the y scale. Defaults to NA.} 45 | 46 | \item{limit_max}{Select upper limit for the y scale. Defaults to NA.} 47 | 48 | \item{theme_type}{Select a theme type from themes available in the \code{\link{apply_theme}} function. Defaults to "ipsum".} 49 | 50 | \item{palette}{Select a palette type with the \code{\link{select_palette}} function. Defaults to "awtools".} 51 | } 52 | \description{ 53 | This function creates nicely formatted, standardised line plots. Color and group parameters for geom_line and 54 | geom_point are automatically inherited from the fill parameter. 55 | } 56 | \examples{ 57 | library(tidyverse) 58 | 59 | tibble( 60 | time = 1:20, 61 | value = rnorm(20, 0.5, 2) 62 | ) \%>\% 63 | plot_line( 64 | x = time, 65 | y = value, 66 | ticks = 10, 67 | hline = 0.05 68 | ) 69 | 70 | tibble( 71 | time = 1:20, 72 | value = rnorm(20, 0.5, 2) 73 | ) \%>\% 74 | plot_line( 75 | x = time, 76 | y = value, 77 | ticks = 10, 78 | hline = 0.05, 79 | limit_min = -2, 80 | limit_max = 2 81 | ) 82 | } 83 | -------------------------------------------------------------------------------- /man/select_palette.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/helpers.R 3 | \name{select_palette} 4 | \alias{select_palette} 5 | \title{Apply a neat, appealing pallete scheme} 6 | \usage{ 7 | select_palette(palette = "awtools") 8 | } 9 | \arguments{ 10 | \item{palette}{Select a palette. Available options for discrete palettes are: "awtools" (8 discrete colors) 11 | and for continuous paletter: "berlin" or "lajolla" (60 continuous colors each). Defaults to "awtools".} 12 | } 13 | \description{ 14 | Palettes are based on the list of available color schemes: \code{\link{https://github.com/EmilHvitfeldt/r-color-palettes}}. 15 | } 16 | -------------------------------------------------------------------------------- /tests/testthat.R: -------------------------------------------------------------------------------- 1 | library(testthat) 2 | library(ggrapid) 3 | library(tidyverse) 4 | 5 | test_check("ggrapid") 6 | -------------------------------------------------------------------------------- /tests/testthat/test_calculate.R: -------------------------------------------------------------------------------- 1 | 2 | context("Testing functions from calculate file...") 3 | 4 | test_that("calculate_decile_table function works...",{ 5 | 6 | diamonds_filter <- diamonds %>% filter(cut %in% c("Ideal", "Premium")) 7 | 8 | t_1 <- diamonds_filter %>% 9 | calculate_decile_table(price, cut, "Ideal") 10 | 11 | t_2 <- diamonds_filter %>% 12 | calculate_decile_table(binning = price, 13 | grouping = cut, 14 | top_level = "Ideal", 15 | n_bins = 5) 16 | 17 | t_3 <- diamonds_filter %>% 18 | calculate_decile_table(binning = price, 19 | grouping = cut, 20 | top_level = "Ideal", 21 | n_bins = 5, 22 | format = FALSE, 23 | color) 24 | 25 | expect_equal(nrow(t_1), 10) 26 | expect_equal(ncol(t_1), 8) 27 | expect_named( 28 | t_1, 29 | c("decile", "min", "median", "max", "top_level", "total", "bottom_level", "ratio") 30 | ) 31 | 32 | expect_equal(nrow(t_2), 5) 33 | expect_equal(ncol(t_2), 8) 34 | expect_named( 35 | t_2, 36 | c("decile", "min", "median", "max", "top_level", "total", "bottom_level", "ratio") 37 | ) 38 | 39 | expect_equal(nrow(t_3), 35) 40 | expect_equal(ncol(t_3), 9) 41 | expect_named( 42 | t_3, 43 | c("color", "decile", "min", "median", "max", "top_level", "total", "bottom_level", "ratio") 44 | ) 45 | 46 | }) 47 | -------------------------------------------------------------------------------- /tests/testthat/test_helpers.R: -------------------------------------------------------------------------------- 1 | 2 | context("Testing functions from helpers file...") 3 | 4 | test_that("number_tikcs function works...",{ 5 | expect_true(is.function(number_ticks())) 6 | }) 7 | 8 | test_that("apply_theme function works...",{ 9 | expect_true(is.list(apply_theme())) 10 | expect_error(apply_theme("test"), "no other theme than 'ipsum' is currently supported") 11 | }) 12 | 13 | test_that("select_palette function works...",{ 14 | expect_true(is.vector(select_palette())) 15 | expect_error(select_palette("test"), "palette type not supported") 16 | expect_length(select_palette("awtools"), 8) 17 | expect_length(select_palette("berlin"), 60) 18 | expect_length(select_palette("lajolla"), 60) 19 | }) 20 | -------------------------------------------------------------------------------- /tests/testthat/test_plot_density.R: -------------------------------------------------------------------------------- 1 | 2 | context("Testing plot_density file...") 3 | 4 | test_that(" works...",{ 5 | 6 | }) 7 | -------------------------------------------------------------------------------- /tests/testthat/test_tweak.R: -------------------------------------------------------------------------------- 1 | 2 | context("Testing functions from tweak file...") 3 | 4 | test_that("first_to_lower function works...",{ 5 | expect_identical(first_to_lower(c("Test")), "test") 6 | expect_identical(first_to_lower(c("Test1", "Test2")), c("test1", "test2")) 7 | t_in <- tibble(Test1 = c(1, 2), Test2 = c(3, 4)) 8 | t_out <- tibble(test1 = c(1, 2), test2 = c(3, 4)) 9 | expect_identical(first_to_lower(t_in), t_out) 10 | }) 11 | 12 | test_that("first_to_upper function works...",{ 13 | expect_identical(first_to_upper(c("test")), "Test") 14 | expect_identical(first_to_upper(c("test1", "test2")), c("Test1", "Test2")) 15 | t_in <- tibble(test1 = c(1, 2), test2 = c(3, 4)) 16 | t_out <- tibble(Test1 = c(1, 2), Test2 = c(3, 4)) 17 | expect_identical(first_to_upper(t_in), t_out) 18 | }) 19 | --------------------------------------------------------------------------------