Skip to contents

# Data correlations evaluation module

Source: `R/data_plots.R`, `R/plot_box.R`, `R/plot_hbar.R`, and 4 more
`data-plots.Rd`

Data correlations evaluation module

Wrapper to create plot based on provided type

Beautiful box plot(s)

Create nice box-plots

Nice horizontal stacked bars (Grotta bars)

Plot nice ridge plot

Readying data for sankey plot

Beautiful sankey plot with option to split by a tertiary group

Beautiful violin plot

Beatiful violin plot

## Usage

    data_visuals_ui(id, tab_title = "Plots", ...)

    data_visuals_server(id, data, ...)

    create_plot(data, type, pri, sec, ter = NULL, ...)

    plot_box(data, pri, sec, ter = NULL)

    plot_box_single(data, pri, sec = NULL, seed = 2103)

    plot_hbars(data, pri, sec, ter = NULL)

    plot_ridge(data, x, y, z = NULL, ...)

    sankey_ready(data, pri, sec, numbers = "count", ...)

    plot_sankey(data, pri, sec, ter = NULL, color.group = "pri", colors = NULL)

    plot_scatter(data, pri, sec, ter = NULL)

    plot_violin(data, pri, sec, ter = NULL)

## Arguments

id

  Module id. (Use 'ns("id")')

...

  ignored for now

data

  data.frame

type

  plot type (derived from possible_plots() and matches custom function)

pri

  primary variable

sec

  secondary variable

ter

  tertiary variable

## Value

Shiny ui module

shiny server module

ggplot2 object

ggplot2 object

ggplot object

ggplot2 object

ggplot2 object

data.frame

ggplot2 object

ggplot2 object

ggplot2 object

## Examples

    create_plot(mtcars, "plot_violin", "mpg", "cyl") |> attributes()
    #> $code
    #> FreesearchR::plot_violin(pri = "mpg", sec = "cyl", ter = NULL)
    #>
    mtcars |> plot_box(pri = "mpg", sec = "cyl", ter = "gear")
    #> Error in purrr::map(p, ~ggplot2::layer_scales(.x)[[axis]]$get_limits()): ℹ In index: 2.
    #> ℹ With name: 4.
    #> Caused by error in `viridis::scale_fill_viridis()`:
    #> ! Continuous values supplied to discrete scale.
    #> ℹ Example values: 4 and 6
    mtcars |>
      default_parsing() |>
      plot_box(pri = "mpg", sec = "cyl", ter = "gear")

    mtcars |> plot_box_single("mpg")

    mtcars |> plot_box_single("mpg","cyl")

    mtcars |> plot_hbars(pri = "carb", sec = "cyl")
    #> Scale for fill is already present.
    #> Adding another scale for fill, which will replace the existing scale.

    mtcars |> plot_hbars(pri = "carb", sec = NULL)
    #> Scale for fill is already present.
    #> Adding another scale for fill, which will replace the existing scale.

    mtcars |>
      default_parsing() |>
      plot_ridge(x = "mpg", y = "cyl")
    #> Picking joint bandwidth of 1.38

    mtcars |> plot_ridge(x = "mpg", y = "cyl", z = "gear")
    #> Picking joint bandwidth of 1.52
    #> Warning: The following aesthetics were dropped during statistical transformation: y and
    #> fill.
    #> ℹ This can happen when ggplot fails to infer the correct grouping structure in
    #>   the data.
    #> ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
    #>   variable into a factor?
    #> Error in ggridges::geom_density_ridges(): Problem while setting up geom.
    #> ℹ Error occurred in the 1st layer.
    #> Caused by error in `compute_geom_1()`:
    #> ! `geom_density_ridges()` requires the following missing aesthetics: y.
    ds <- data.frame(g = sample(LETTERS[1:2], 100, TRUE), first = REDCapCAST::as_factor(sample(letters[1:4], 100, TRUE)), last = sample(c(letters[1:4], NA), 100, TRUE, prob = c(rep(.23, 4), .08)))
    ds |> sankey_ready("first", "last")
    #> # A tibble: 19 × 7
    #>    first last      n gx.sum gy.sum lx          ly
    #>    <fct> <fct> <int>  <int>  <int> <fct>       <fct>
    #>  1 d     d        11     36     18 "d\n(n=36)" "d\n(n=18)"
    #>  2 d     a        11     36     30 "d\n(n=36)" "a\n(n=30)"
    #>  3 d     b         6     36     25 "d\n(n=36)" "b\n(n=25)"
    #>  4 d     c         8     36     22 "d\n(n=36)" "c\n(n=22)"
    #>  5 c     d         3     24     18 "c\n(n=24)" "d\n(n=18)"
    #>  6 c     a         7     24     30 "c\n(n=24)" "a\n(n=30)"
    #>  7 c     b        10     24     25 "c\n(n=24)" "b\n(n=25)"
    #>  8 c     c         1     24     22 "c\n(n=24)" "c\n(n=22)"
    #>  9 c     NA        3     24      5 "c\n(n=24)"  NA
    #> 10 b     d         2     17     18 "b\n(n=17)" "d\n(n=18)"
    #> 11 b     a         4     17     30 "b\n(n=17)" "a\n(n=30)"
    #> 12 b     b         3     17     25 "b\n(n=17)" "b\n(n=25)"
    #> 13 b     c         7     17     22 "b\n(n=17)" "c\n(n=22)"
    #> 14 b     NA        1     17      5 "b\n(n=17)"  NA
    #> 15 a     d         2     23     18 "a\n(n=23)" "d\n(n=18)"
    #> 16 a     a         8     23     30 "a\n(n=23)" "a\n(n=30)"
    #> 17 a     b         6     23     25 "a\n(n=23)" "b\n(n=25)"
    #> 18 a     c         6     23     22 "a\n(n=23)" "c\n(n=22)"
    #> 19 a     NA        1     23      5 "a\n(n=23)"  NA
    ds |> sankey_ready("first", "last", numbers = "percentage")
    #> # A tibble: 19 × 7
    #>    first last      n gx.sum gy.sum lx         ly
    #>    <fct> <fct> <int>  <int>  <int> <fct>      <fct>
    #>  1 d     d        11     36     18 "d\n(36%)" "d\n(18%)"
    #>  2 d     a        11     36     30 "d\n(36%)" "a\n(30%)"
    #>  3 d     b         6     36     25 "d\n(36%)" "b\n(25%)"
    #>  4 d     c         8     36     22 "d\n(36%)" "c\n(22%)"
    #>  5 c     d         3     24     18 "c\n(24%)" "d\n(18%)"
    #>  6 c     a         7     24     30 "c\n(24%)" "a\n(30%)"
    #>  7 c     b        10     24     25 "c\n(24%)" "b\n(25%)"
    #>  8 c     c         1     24     22 "c\n(24%)" "c\n(22%)"
    #>  9 c     NA        3     24      5 "c\n(24%)"  NA
    #> 10 b     d         2     17     18 "b\n(17%)" "d\n(18%)"
    #> 11 b     a         4     17     30 "b\n(17%)" "a\n(30%)"
    #> 12 b     b         3     17     25 "b\n(17%)" "b\n(25%)"
    #> 13 b     c         7     17     22 "b\n(17%)" "c\n(22%)"
    #> 14 b     NA        1     17      5 "b\n(17%)"  NA
    #> 15 a     d         2     23     18 "a\n(23%)" "d\n(18%)"
    #> 16 a     a         8     23     30 "a\n(23%)" "a\n(30%)"
    #> 17 a     b         6     23     25 "a\n(23%)" "b\n(25%)"
    #> 18 a     c         6     23     22 "a\n(23%)" "c\n(22%)"
    #> 19 a     NA        1     23      5 "a\n(23%)"  NA
    data.frame(
      g = sample(LETTERS[1:2], 100, TRUE),
      first = REDCapCAST::as_factor(sample(letters[1:4], 100, TRUE)),
      last = sample(c(TRUE, FALSE, FALSE), 100, TRUE)
    ) |>
      sankey_ready("first", "last")
    #> # A tibble: 8 × 7
    #>   first last      n gx.sum gy.sum lx          ly
    #>   <fct> <fct> <int>  <int>  <int> <fct>       <fct>
    #> 1 b     FALSE    16     29     66 "b\n(n=29)" "FALSE\n(n=66)"
    #> 2 b     TRUE     13     29     34 "b\n(n=29)" "TRUE\n(n=34)"
    #> 3 a     FALSE    18     25     66 "a\n(n=25)" "FALSE\n(n=66)"
    #> 4 a     TRUE      7     25     34 "a\n(n=25)" "TRUE\n(n=34)"
    #> 5 d     FALSE    13     20     66 "d\n(n=20)" "FALSE\n(n=66)"
    #> 6 d     TRUE      7     20     34 "d\n(n=20)" "TRUE\n(n=34)"
    #> 7 c     FALSE    19     26     66 "c\n(n=26)" "FALSE\n(n=66)"
    #> 8 c     TRUE      7     26     34 "c\n(n=26)" "TRUE\n(n=34)"
    ds <- data.frame(g = sample(LETTERS[1:2], 100, TRUE), first = REDCapCAST::as_factor(sample(letters[1:4], 100, TRUE)), last = REDCapCAST::as_factor(sample(letters[1:4], 100, TRUE)))
    ds |> plot_sankey("first", "last")
    #> Loading required package: ggplot2

    ds |> plot_sankey("first", "last", color.group = "sec")

    ds |> plot_sankey("first", "last", ter = "g", color.group = "sec")
    #> Warning: Some strata appear at multiple axes.
    #> Warning: Some strata appear at multiple axes.
    #> Warning: Some strata appear at multiple axes.

    mtcars |>
      default_parsing() |>
      plot_sankey("cyl", "gear", "am", color.group = "pri")
    #> Warning: Some strata appear at multiple axes.
    #> Warning: Some strata appear at multiple axes.
    #> Warning: Some strata appear at multiple axes.

    ## In this case, the last plot as the secondary variable in wrong order
    ## Dont know why...
    mtcars |>
      default_parsing() |>
      plot_sankey("cyl", "gear", "vs", color.group = "pri")
    #> Warning: Some strata appear at multiple axes.
    #> Warning: Some strata appear at multiple axes.
    #> Warning: Some strata appear at multiple axes.

    mtcars |> plot_scatter(pri = "mpg", sec = "wt")

    mtcars |> plot_violin(pri = "mpg", sec = "cyl", ter = "gear")
    #> Warning: There was 1 warning in `summarize()`.
    #> ℹ In argument: `V1 = .fun(as.data.frame(pick(everything())), var)`.
    #> ℹ In group 1: `cyl = 4`.
    #> Caused by warning in `stats::qt()`:
    #> ! NaNs produced
    #> Warning: There was 1 warning in `summarize()`.
    #> ℹ In argument: `V1 = .fun(as.data.frame(pick(everything())), var)`.
    #> ℹ In group 1: `cyl = 4`.
    #> Caused by warning in `stats::qt()`:
    #> ! NaNs produced
    #> Warning: There was 1 warning in `summarize()`.
    #> ℹ In argument: `V1 = .fun(as.data.frame(pick(everything())), var)`.
    #> ℹ In group 2: `cyl = 6`.
    #> Caused by warning in `stats::qt()`:
    #> ! NaNs produced
    #> Warning: There was 1 warning in `summarize()`.
    #> ℹ In argument: `V1 = .fun(as.data.frame(pick(everything())), var)`.
    #> ℹ In group 2: `cyl = 6`.
    #> Caused by warning in `stats::qt()`:
    #> ! NaNs produced
    #> Warning: Groups with fewer than two datapoints have been dropped.
    #> ℹ Set `drop = FALSE` to consider such groups for position adjustment purposes.
    #> Warning: Groups with fewer than two datapoints have been dropped.
    #> ℹ Set `drop = FALSE` to consider such groups for position adjustment purposes.
    #> Warning: Groups with fewer than two datapoints have been dropped.
    #> ℹ Set `drop = FALSE` to consider such groups for position adjustment purposes.
    #> Warning: Groups with fewer than two datapoints have been dropped.
    #> ℹ Set `drop = FALSE` to consider such groups for position adjustment purposes.
    #> Warning: Groups with fewer than two datapoints have been dropped.
    #> ℹ Set `drop = FALSE` to consider such groups for position adjustment purposes.
    #> Warning: Groups with fewer than two datapoints have been dropped.
    #> ℹ Set `drop = FALSE` to consider such groups for position adjustment purposes.