|
| 1 | +#' ggplot Chart objects |
| 2 | +#' |
| 3 | +#' A base class in the \pkg{struct} package for ggplot2-based charts. |
| 4 | +#' ggplot_chart extends the base chart class to provide common functionality |
| 5 | +#' for all ggplot2 chart objects. |
| 6 | +#' |
| 7 | +#' The layers slot can contain any ggplot2 layer including: |
| 8 | +#' - Geoms (geom_point, geom_line, etc.) |
| 9 | +#' - Stats (stat_ellipse, stat_smooth, etc.) |
| 10 | +#' - Scales (scale_color_manual, scale_x_continuous, etc.) |
| 11 | +#' - Themes (theme_minimal, theme_bw, etc.) |
| 12 | +#' - Facets (facet_wrap, facet_grid, etc.) |
| 13 | +#' - Labels (labs, xlab, ylab, etc.) |
| 14 | +#' |
| 15 | +#' @export |
| 16 | +#' @param ... Additional parameters passed to chart constructor |
| 17 | +#' @return A ggplot_chart object |
| 18 | +#' @examples |
| 19 | +#' # Create a ggplot chart (not typically called directly) |
| 20 | +#' gc = ggplot_chart() |
| 21 | +#' @rdname ggplot_chart |
| 22 | +#' @include chart_class.R layer_entity_class.R struct_preset_class.R typed_list_class.R global_preset_registry.R |
| 23 | +ggplot_chart = function(...) { |
| 24 | + # new object |
| 25 | + out = new_struct('ggplot_chart', ...) |
| 26 | + return(out) |
| 27 | +} |
| 28 | + |
| 29 | +.ggplot_chart<-setClass( |
| 30 | + "ggplot_chart", |
| 31 | + contains = 'chart', |
| 32 | + slots = c( |
| 33 | + data = 'data.frame', |
| 34 | + layers = 'typed_list', |
| 35 | + mapping = 'entity' |
| 36 | + ), |
| 37 | + prototype = list( |
| 38 | + name = 'ggplot chart', |
| 39 | + description = 'A base class for ggplot2-based charts. Supports all ggplot2 layer types including geoms, stats, scales, themes, facets, and labels.', |
| 40 | + type = 'ggplot', |
| 41 | + data = data.frame(), |
| 42 | + layers = typed_list(.type = 'Layer'), |
| 43 | + mapping = entity( |
| 44 | + name = 'Plot aesthetics', |
| 45 | + description = 'A list of plot aesthetics and their mapping to the data', |
| 46 | + value = NULL, |
| 47 | + type = c('typed_list.uneval', 'uneval', 'NULL') |
| 48 | + ) |
| 49 | + ) |
| 50 | +) |
| 51 | + |
| 52 | +#' @rdname ggplot_chart |
| 53 | +#' @export |
| 54 | +setMethod(f = "chart_build", |
| 55 | + signature = c("ggplot_chart", 'DatasetExperiment'), |
| 56 | + definition = function(obj, dobj) { |
| 57 | + # This is a base method that should be overridden by specific chart types |
| 58 | + warning('chart_build method not implemented for "', class(obj), '"') |
| 59 | + return(obj) |
| 60 | + } |
| 61 | +) |
| 62 | + |
| 63 | +#' @rdname ggplot_chart |
| 64 | +#' @export |
| 65 | +setMethod(f = "chart_plot", |
| 66 | + signature = c("ggplot_chart", 'DatasetExperiment'), |
| 67 | + definition = function(obj, dobj) { |
| 68 | + # Build the chart |
| 69 | + obj = chart_build(obj, dobj) |
| 70 | + |
| 71 | + # Create the ggplot object |
| 72 | + p = ggplot2::ggplot(data = obj@data, mapping = obj@mapping@value) |
| 73 | + |
| 74 | + # Add all layers (geoms, stats, scales, themes, labels, facets, etc.) |
| 75 | + if (length(obj@layers) > 0) { |
| 76 | + for (i in seq_along(obj@layers)) { |
| 77 | + layer = obj@layers[[i]] |
| 78 | + if (!is.null(layer)) { |
| 79 | + p = p + layer |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return(p) |
| 85 | + } |
| 86 | +) |
| 87 | + |
| 88 | +#' @rdname ggplot_chart |
| 89 | +#' @export |
| 90 | +setMethod(f = 'show', |
| 91 | + signature = c('ggplot_chart'), |
| 92 | + definition = function(object) { |
| 93 | + callNextMethod() |
| 94 | + cat('data: ', nrow(object@data), ' rows x ', ncol(object@data), ' columns\n', sep = '') |
| 95 | + cat('layers: ', length(object@layers), ' layers\n', sep = '') |
| 96 | + } |
| 97 | +) |
| 98 | + |
| 99 | +# Helper functions for common ggplot operations |
| 100 | +xlab = function(label) { |
| 101 | + return(ggplot2::xlab(label)) |
| 102 | +} |
| 103 | + |
| 104 | +ylab = function(label) { |
| 105 | + return(ggplot2::ylab(label)) |
| 106 | +} |
0 commit comments