# DAL ToolBox
# version 1.01.727
source("https://raw.githubusercontent.com/cefet-rj-dal/daltoolbox/main/jupyter.R")
#loading DAL
load_library("daltoolbox")
Loading required package: daltoolbox Registered S3 method overwritten by 'quantmod': method from as.zoo.data.frame zoo Attaching package: ‘daltoolbox’ The following object is masked from ‘package:base’: transform
load_library("RColorBrewer")
#color palette
colors <- brewer.pal(4, 'Set1')
load_library("ggplot2")
# setting the font size for all charts
font <- theme(text = element_text(size=16))
Loading required package: RColorBrewer Loading required package: ggplot2
The following examples use random variables so that different data distribution can be better viewed.
# example4: dataset to be plotted
example <- data.frame(exponential = rexp(100000, rate = 1),
uniform = runif(100000, min = 2.5, max = 3.5),
normal = rnorm(100000, mean=5))
head(example)
exponential | uniform | normal | |
---|---|---|---|
<dbl> | <dbl> | <dbl> | |
1 | 0.1598983 | 2.705539 | 5.179180 |
2 | 2.3712059 | 3.437170 | 4.297048 |
3 | 0.4120131 | 2.986926 | 5.324625 |
4 | 0.1527977 | 3.082660 | 4.794226 |
5 | 1.1249669 | 2.547858 | 4.577240 |
6 | 2.0349881 | 3.362697 | 4.941374 |
Computes and draws kernel density estimate, which is a smoothed version of the histogram. This is a useful alternative to the histogram for continuous data that comes from an underlying smooth distribution.
More information: ?geom_density (R documentation)
options(repr.plot.width=8, repr.plot.height=5)
grf <- plot_density(example, colors=colors[1:3]) + font
plot(grf)
Using as id variables
Function $grid.arrange$ is used to position previously computed charts
load_library("dplyr")
grfe <- plot_density(example |> select(exponential),
label_x = "exponential", color=colors[1]) + font
grfu <- plot_density(example |> select(uniform),
label_x = "uniform", color=colors[2]) + font
grfn <- plot_density(example |> select(normal),
label_x = "normal", color=colors[3]) + font
Loading required package: dplyr Attaching package: ‘dplyr’ The following objects are masked from ‘package:stats’: filter, lag The following objects are masked from ‘package:base’: intersect, setdiff, setequal, union Using as id variables Using as id variables Using as id variables
load_library("gridExtra")
options(repr.plot.width=15, repr.plot.height=4)
grid.arrange(grfe, grfu, grfn, ncol=3)
Loading required package: gridExtra Attaching package: ‘gridExtra’ The following object is masked from ‘package:dplyr’: combine