GithubHelp home page GithubHelp logo

emitanaka / deggust Goto Github PK

View Code? Open in Web Editor NEW
11.0 4.0 2.0 11.29 MB

An R-package to visualise edibble designs as ggplot graphics

Home Page: https://deggust.emitanaka.org

R 100.00%
rstats-package ggplot-extension

deggust's Introduction

deggust

Lifecycle: experimental

The aim for deggust R-package is to visualise designs constructed from the edibble R-package. The visualisation are ggplot graphics.

(WIP)

Installation

The development version from GitHub with:

# install.packages("devtools")
devtools::install_github("emitanaka/deggust")

Recipe experimental designs

You can see deggust::autoplot() used in various edibble designs at https://emitanaka.org/edibble-book/cookbook.html. Some samples are below.

Examples

library(edibble)
library(deggust)
#> Loading required package: ggplot2
fac <- takeout(menu_factorial(trt = c(3, 2)))
fac
#> start_design("Factorial Design") %>%
#>   set_units(unit = 42) %>%
#>   set_trts(trt1 = 3,
#>            trt2 = 2) %>%
#>   allot_trts(~unit) %>%
#>   assign_trts("random", seed = 440) %>%
#>   serve_table() 
#> 
#> # Factorial Design 
#> # An edibble: 42 x 3
#>          unit     trt1     trt2
#>  * <unit(42)> <trt(3)> <trt(2)>
#>  1     unit1     trt12    trt21
#>  2     unit2     trt11    trt22
#>  3     unit3     trt13    trt22
#>  4     unit4     trt11    trt21
#>  5     unit5     trt12    trt21
#>  6     unit6     trt11    trt22
#>  7     unit7     trt12    trt21
#>  8     unit8     trt13    trt22
#>  9     unit9     trt13    trt21
#> 10     unit10    trt13    trt21
#> # … with 32 more rows
autoplot(fac)

hyper_graeco <- takeout(menu_hyper_graeco(t = 6))
hyper_graeco
#> start_design("Hyper-Graeco-Latin Square Design") %>%
#>   set_units(block1 = 6,
#>             block2 = 6,
#>             block3 = 6,
#>             block4 = 6,
#>             unit = ~block1:block2:block3:block4) %>%
#>   set_trts(trt = 6) %>%
#>   allot_trts(trt ~ unit) %>%
#>   assign_trts("random", seed = 253) %>%
#>   serve_table() 
#> 
#> # Hyper-Graeco-Latin Square Design 
#> # An edibble: 1,296 x 6
#>       block1    block2    block3    block4       unit      trt
#>  * <unit(6)> <unit(6)> <unit(6)> <unit(6)> <unit(1k)> <trt(6)>
#>  1   block11   block21   block31   block41     unit1      trt3
#>  2   block12   block21   block31   block41     unit2      trt6
#>  3   block13   block21   block31   block41     unit3      trt1
#>  4   block14   block21   block31   block41     unit4      trt5
#>  5   block15   block21   block31   block41     unit5      trt2
#>  6   block16   block21   block31   block41     unit6      trt4
#>  7   block11   block22   block31   block41     unit7      trt1
#>  8   block12   block22   block31   block41     unit8      trt3
#>  9   block13   block22   block31   block41     unit9      trt4
#> 10   block14   block22   block31   block41     unit10     trt2
#> # … with 1,286 more rows
autoplot(hyper_graeco)

Automatic look into the design layout:

unit1 <- start_design(name = "One unit") %>%
    set_trts(trt = 4) %>%
    set_units(unit = 16) %>%
    allocate_trts(trt ~ unit) %>%
    randomise_trts() %>%
    serve_table()
#> Warning: `randomise_trts` is deprecated. Please use `assign_trts("random")`
#> instead.
#> Warning: `allocate_trts` is deprecated. Please use `allot_trts` instead.

autoplot(unit1)

Easy to replace the unit shape:

autoplot(unit1, shape = "box")

autoplot(unit1, shape = "triangle")

autoplot(unit1, shape = "hexagon",
         text = element_text(size = 6, 
                             color = "gray",
                             face = "bold")) 

Related work

  • desplot for visualising designs
  • ExploreModelMatrix for exploring design matrix
  • ez for easy analysis and visualization of factorial experiments

deggust's People

Contributors

emitanaka avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

deggust's Issues

unit positions

currently the edges appear in a zig-zag as the order of the nodes are always from left-to-right. I should allow an option such that edge length are minimised

README-unnamed-chunk-3-1

it should be `scale_unit`

not geom_unit. Principally, I'm changing the representation of an unit, so change of graphical element -> edibble unit is scale and not replacing geom

design overhaul for geom objects

Basic

ggplot(ed, aes(unit, block, x, y, fill, shape, pattern)) + 
  geom_unit_node(width, height, size, shape, angle) + 
  geom_unit_text(label, size, color, fontface, angle, vjust, hjust, lineheight, alpha) + 
  geom_block_node() + 
  geom_block_text()
  • Alternative option for treatment mapping include: hue = fill, chroma, luminance, alpha, color (for line), lwd, lty. Some of these have continuous range so works better if there is order in the treatment.

Facets

plot + facet_group(~group)
plot + facet_group_grid(y ~ x)

Perception

patchwork_perception(plot, layout)

Autoplot

Hierarchical structure

  • 1 unit -> unit
  • 2 unit -> unit, block
  • 3 unit -> unit, block, facet

If no hierarchical structure or unclear and the number of units are greater than 1, then output warning and select smallest unit as the unit.

Spatial structure

set_units(x = 1:4, y = 1:4, plot = ~x:y) %>% 
mutate(block = case_when(x %in% 1:2 & y %in% 1:2 ~ "block1",
                                                 x %in% 1:2 & y %in% 3:4 ~ "block2",
                                                 x %in% 3:4 & y %in% 1:2 ~ "block3",
                                                 x %in% 3:4 & y %in% 3:4 ~ "block4"))                                 

Then geom_unit(aes(x = x, y = y, block = block)). The x and y defines location on plot and block makes a convex hull around it (assumes that block are spatially close).

Producing experiment materials from edibble designs

Hi @emitanaka, there was a question at your Ihaka lecture at around 56:00 along the lines of "Can the software produce experiment specific layouts and instructions from the experimental design"? Examples given included field plots for planting experiments, or diagrams of "which part of a rat's lobe to randomly cut off."

Aside from replacing the geoms with images, another idea would be to use a background image (of the lab plate, or field layout) and attach coordinates to each experimental unit for plotting the treatment to apply to that experimental unit. You could think of it as a variation on georeferencing map images below (taken from Master GDAL Tools)--

I don't have enough ggplot skills to try this out, but it seems in theory possible and seems more flexible than the row/col solution you mentioned at the lecture.

Other type of visualisations

  • upset plots to check the frequency of treatments and combinations of it
  • when the comparison is made on rcrd, the most appropriate comparison plot for the given structure
  • the factor and level network graphs that show the relationship between the variable
  • flow diagrams that document the overall process
  • cube plots? (are they useful?)
    screening

Installation issue for macOS Monterey 12.0.1 (M1 macs)

Seems the issue is with dependencies, igraph installation, couldn't find a workaround

installing source package ‘deggust’ ...
** using staged installation
** R
** byte-compile and prepare package for lazy loading
Error: object ‘get_edibble_design’ is not exported by 'namespace:edibble'
Execution halted
ERROR: lazy loading failed for package ‘deggust’

  • removing ‘/Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/library/deggust’
    Warning messages:
    1: In i.p(...) : installation of package ‘igraph’ had non-zero exit status
    2: In i.p(...) :
    installation of package ‘/var/folders/vp/9f936cf54cb8fgrb3mhd4p5h0000gn/T//Rtmp2KjQXD/file5d5919f17b94/deggust_0.0.0.9000.tar.gz’ had non-zero exit status

Remove `ggraph` dependancy

Since I'm using manual positioning anyway, it seems easier to stick with just using ggplot alone since ggraph doesn't do inheritance of aes in typical fashion.

autoplot throws error

The autoplot function not rendering the plot and throws the following error:

library(edibble)
library(deggust)
#> Loading required package: ggplot2

CRD1 <- 
  takeout(
    recipe = menu_crd(
                        t    = 5
                      , n    = 30
                      , r    = 
                      , seed = 12345
                      )
  , show   = TRUE
    )
CRD1
#> design("Completely Randomised Design") %>%
#>   set_units(unit = 30) %>%
#>   set_trts(trt = 5) %>%
#>   allot_trts(trt ~ unit) %>%
#>   assign_trts("random", seed = 12345) %>%
#>   serve_table() 
#> 
#> # Completely Randomised Design 
#> # An edibble: 30 x 2
#>          unit      trt
#>  * <unit(30)> <trt(5)>
#>  1     unit1      trt4
#>  2     unit2      trt2
#>  3     unit3      trt2
#>  4     unit4      trt5
#>  5     unit5      trt4
#>  6     unit6      trt4
#>  7     unit7      trt5
#>  8     unit8      trt5
#>  9     unit9      trt3
#> 10     unit10     trt2
#> # ℹ 20 more rows

deggust::autoplot(CRD1)
#> Error in `geom_unit_node()`:
#> ! Problem while converting geom to grob.
#> ℹ Error occurred in the 2nd layer.
#> Caused by error in `check.length()`:
#> ! 'gpar' element 'lwd' must not be length 0
#> Backtrace:
#>      ▆
#>   1. ├─base::tryCatch(...)
#>   2. │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>   3. │   ├─base (local) tryCatchOne(...)
#>   4. │   │ └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>   5. │   └─base (local) tryCatchList(expr, names[-nh], parentenv, handlers[-nh])
#>   6. │     └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>   7. │       └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>   8. ├─base::withCallingHandlers(...)
#>   9. ├─base::saveRDS(...)
#>  10. ├─base::do.call(...)
#>  11. ├─base (local) `<fn>`(...)
#>  12. ├─global `<fn>`(input = base::quote("bosky-moa_reprex.R"))
#>  13. │ └─rmarkdown::render(input, quiet = TRUE, envir = globalenv(), encoding = "UTF-8")
#>  14. │   └─knitr::knit(knit_input, knit_output, envir = envir, quiet = quiet)
#>  15. │     └─knitr:::process_file(text, output)
#>  16. │       ├─base::withCallingHandlers(...)
#>  17. │       ├─base::withCallingHandlers(...)
#>  18. │       ├─knitr:::process_group(group)
#>  19. │       └─knitr:::process_group.block(group)
#>  20. │         └─knitr:::call_block(x)
#>  21. │           └─knitr:::block_exec(params)
#>  22. │             └─knitr:::eng_r(options)
#>  23. │               ├─knitr:::in_input_dir(...)
#>  24. │               │ └─knitr:::in_dir(input_dir(), expr)
#>  25. │               └─knitr (local) evaluate(...)
#>  26. │                 └─evaluate::evaluate(...)
#>  27. │                   └─evaluate:::evaluate_call(...)
#>  28. │                     ├─evaluate (local) handle(...)
#>  29. │                     │ └─base::try(f, silent = TRUE)
#>  30. │                     │   └─base::tryCatch(...)
#>  31. │                     │     └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>  32. │                     │       └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>  33. │                     │         └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>  34. │                     ├─base::withCallingHandlers(...)
#>  35. │                     ├─base::withVisible(value_fun(ev$value, ev$visible))
#>  36. │                     └─knitr (local) value_fun(ev$value, ev$visible)
#>  37. │                       └─knitr (local) fun(x, options = options)
#>  38. │                         ├─base::withVisible(knit_print(x, ...))
#>  39. │                         ├─knitr::knit_print(x, ...)
#>  40. │                         └─knitr:::knit_print.default(x, ...)
#>  41. │                           └─evaluate (local) normal_print(x)
#>  42. │                             ├─base::print(x)
#>  43. │                             └─ggplot2:::print.ggplot(x)
#>  44. │                               ├─ggplot2::ggplot_gtable(data)
#>  45. │                               └─ggplot2:::ggplot_gtable.ggplot_built(data)
#>  46. │                                 └─ggplot2:::by_layer(...)
#>  47. │                                   ├─rlang::try_fetch(...)
#>  48. │                                   │ ├─base::tryCatch(...)
#>  49. │                                   │ │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>  50. │                                   │ │   └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>  51. │                                   │ │     └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>  52. │                                   │ └─base::withCallingHandlers(...)
#>  53. │                                   └─ggplot2 (local) f(l = layers[[i]], d = data[[i]])
#>  54. │                                     └─l$draw_geom(d, layout)
#>  55. │                                       └─ggplot2 (local) draw_geom(..., self = self)
#>  56. │                                         └─self$geom$draw_layer(...)
#>  57. │                                           └─ggplot2 (local) draw_layer(..., self = self)
#>  58. │                                             └─base::lapply(...)
#>  59. │                                               └─ggplot2 (local) FUN(X[[i]], ...)
#>  60. │                                                 ├─rlang::inject(self$draw_panel(data, panel_params, coord, !!!params))
#>  61. │                                                 └─self$draw_panel(data, panel_params, coord)
#>  62. │                                                   └─deggust (local) draw_panel(...)
#>  63. │                                                     ├─grid::polygonGrob(...)
#>  64. │                                                     │ └─grid::grob(...)
#>  65. │                                                     └─grid::gpar(...)
#>  66. │                                                       └─grid:::validGP(list(...))
#>  67. │                                                         └─grid (local) numnotnull("lwd")
#>  68. │                                                           └─grid (local) check.length(gparname)
#>  69. │                                                             └─base::stop(...)
#>  70. └─base::.handleSimpleError(...)
#>  71.   └─rlang (local) h(simpleError(msg, call))
#>  72.     └─handlers[[1L]](cnd)
#>  73.       └─cli::cli_abort(...)
#>  74.         └─rlang::abort(...)

Created on 2023-04-10 with [reprex v2.0.2](https://reprex.tidyverse.org/)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.