Search code examples
rggplot2plotsurvivalsurvminer

How to find a label in a gg plot object generated by ggforest() to edit its content?


I have generated a ggforest plot (packages: survival, survminer)

fit <- coxph(formula, data)
p <- ggforest(fit, data)

and want to edit (remove) the label "gASV11PFS" in the plot

plot to be edited

I tried to find variable where the description is stored with str(p, max.level = NA) but it does not display this variable.

> str(p, max.level = NA)
List of 11
 $ data       : list()
  ..- attr(*, "class")= chr "waiver"
 $ layers     :List of 1
  ..$ :Classes 'LayerInstance', 'Layer', 'ggproto', 'gg' <ggproto object: Class LayerInstance, Layer, gg>
    aes_params: list
    compute_aesthetics: function
    compute_geom_1: function
    compute_geom_2: function
    compute_position: function
    compute_statistic: function
    computed_geom_params: list
    computed_mapping: NULL
    computed_stat_params: list
    constructor: call
    data: data.frame
    draw_geom: function
    finish_statistics: function
    geom: <ggproto object: Class GeomDrawGrob, GeomCustomAnn, Geom, gg>
        aesthetics: function
        default_aes: uneval
        draw_group: function
        draw_key: function
        draw_layer: function
        draw_panel: function
        extra_params: 
        handle_na: function
        non_missing_aes: 
        optional_aes: 
        parameters: function
        rename_size: FALSE
        required_aes: 
        setup_data: function
        setup_params: function
        use_defaults: function
        super:  <ggproto object: Class GeomCustomAnn, Geom, gg>
    geom_params: list
    inherit.aes: FALSE
    layer_data: function
    map_statistic: function
    mapping: NULL
    position: <ggproto object: Class PositionIdentity, Position, gg>
        compute_layer: function
        compute_panel: function
        required_aes: 
        setup_data: function
        setup_params: function
        super:  <ggproto object: Class Position, gg>
    print: function
    setup_layer: function
    show.legend: NA
    stat: <ggproto object: Class StatIdentity, Stat, gg>
        aesthetics: function
        compute_group: function
        compute_layer: function
        compute_panel: function
        default_aes: uneval
        dropped_aes: 
        extra_params: na.rm
        finish_layer: function
        non_missing_aes: 
        optional_aes: 
        parameters: function
        required_aes: 
        retransform: TRUE
        setup_data: function
        setup_params: function
        super:  <ggproto object: Class Stat, gg>
    stat_params: list
    super:  <ggproto object: Class Layer, gg> 
 $ scales     :Classes 'ScalesList', 'ggproto', 'gg' <ggproto object: Class ScalesList, gg>
    add: function
    add_defaults: function
    add_missing: function
    backtransform_df: function
    clone: function
    find: function
    get_scales: function
    has_scale: function
    input: function
    map_df: function
    n: function
    non_position_scales: function
    scales: list
    train_df: function
    transform_df: function
    super:  <ggproto object: Class ScalesList, gg> 
 $ guides     :Classes 'Guides', 'ggproto', 'gg' <ggproto object: Class Guides, gg>
    add: function
    assemble: function
    build: function
    draw: function
    get_custom: function
    get_guide: function
    get_params: function
    get_position: function
    guides: NULL
    merge: function
    missing: <ggproto object: Class GuideNone, Guide, gg>
        add_title: function
        arrange_layout: function
        assemble_drawing: function
        available_aes: any
        build_decor: function
        build_labels: function
        build_ticks: function
        build_title: function
        draw: function
        draw_early_exit: function
        elements: list
        extract_decor: function
        extract_key: function
        extract_params: function
        get_layer_key: function
        hashables: list
        measure_grobs: function
        merge: function
        override_elements: function
        params: list
        process_layers: function
        setup_elements: function
        setup_params: function
        train: function
        transform: function
        super:  <ggproto object: Class GuideNone, Guide, gg>
    package_box: function
    print: function
    process_layers: function
    setup: function
    subset_guides: function
    train: function
    update_params: function
    super:  <ggproto object: Class Guides, gg> 
 $ mapping    : Named list()
  ..- attr(*, "class")= chr "uneval"
 $ theme      :List of 136
  ..$ line                            : list()
  .. ..- attr(*, "class")= chr [1:2] "element_blank" "element"
  ..$ rect                            : list()
  .. ..- attr(*, "class")= chr [1:2] "element_blank" "element"
  ..$ text                            :List of 11
  .. ..$ family       : chr ""
  .. ..$ face         : chr "plain"
  .. ..$ colour       : chr "black"
  .. ..$ size         : num 14
  .. ..$ hjust        : num 0.5
  .. ..$ vjust        : num 0.5
  .. ..$ angle        : num 0
  .. ..$ lineheight   : num 0.9
  .. ..$ margin       : 'margin' num [1:4] 0points 0points 0points 0points
  .. .. ..- attr(*, "unit")= int 8
  .. ..$ debug        : logi FALSE
  .. ..$ inherit.blank: logi TRUE
  .. ..- attr(*, "class")= chr [1:2] "element_text" "element"
  ..$ title                           : NULL
  ..$ aspect.ratio                    : NULL
  ..$ axis.title                      : list()
  .. ..- attr(*, "class")= chr [1:2] "element_blank" "element"
  ..$ axis.title.x                    : NULL
  ..$ axis.title.x.top                : NULL
  ..$ axis.title.x.bottom             : NULL
  ..$ axis.title.y                    : NULL
  ..$ axis.title.y.left               : NULL
  ..$ axis.title.y.right              : NULL
  ..$ axis.text                       : list()
  .. ..- attr(*, "class")= chr [1:2] "element_blank" "element"
  ..$ axis.text.x                     : NULL
  ..$ axis.text.x.top                 : NULL
  ..$ axis.text.x.bottom              : NULL
  ..$ axis.text.y                     : NULL
  ..$ axis.text.y.left                : NULL
  ..$ axis.text.y.right               : NULL
  ..$ axis.text.theta                 : NULL
  ..$ axis.text.r                     : NULL
  ..$ axis.ticks                      : list()
  .. ..- attr(*, "class")= chr [1:2] "element_blank" "element"
  ..$ axis.ticks.x                    : NULL
  ..$ axis.ticks.x.top                : NULL
  ..$ axis.ticks.x.bottom             : NULL
  ..$ axis.ticks.y                    : NULL
  ..$ axis.ticks.y.left               : NULL
  ..$ axis.ticks.y.right              : NULL
  ..$ axis.ticks.theta                : NULL
  ..$ axis.ticks.r                    : NULL
  ..$ axis.minor.ticks.x.top          : NULL
  ..$ axis.minor.ticks.x.bottom       : NULL
  ..$ axis.minor.ticks.y.left         : NULL
  ..$ axis.minor.ticks.y.right        : NULL
  ..$ axis.minor.ticks.theta          : NULL
  ..$ axis.minor.ticks.r              : NULL
  ..$ axis.ticks.length               : 'simpleUnit' num 0points
  .. ..- attr(*, "unit")= int 8
  ..$ axis.ticks.length.x             : NULL
  ..$ axis.ticks.length.x.top         : NULL
  ..$ axis.ticks.length.x.bottom      : NULL
  ..$ axis.ticks.length.y             : NULL
  ..$ axis.ticks.length.y.left        : NULL
  ..$ axis.ticks.length.y.right       : NULL
  ..$ axis.ticks.length.theta         : NULL
  ..$ axis.ticks.length.r             : NULL
  ..$ axis.minor.ticks.length         : 'simpleUnit' num 0points
  .. ..- attr(*, "unit")= int 8
  ..$ axis.minor.ticks.length.x       : NULL
  ..$ axis.minor.ticks.length.x.top   : NULL
  ..$ axis.minor.ticks.length.x.bottom: NULL
  ..$ axis.minor.ticks.length.y       : NULL
  ..$ axis.minor.ticks.length.y.left  : NULL
  ..$ axis.minor.ticks.length.y.right : NULL
  ..$ axis.minor.ticks.length.theta   : NULL
  ..$ axis.minor.ticks.length.r       : NULL
  ..$ axis.line                       : list()
  .. ..- attr(*, "class")= chr [1:2] "element_blank" "element"
  ..$ axis.line.x                     : NULL
  ..$ axis.line.x.top                 : NULL
  ..$ axis.line.x.bottom              : NULL
  ..$ axis.line.y                     : NULL
  ..$ axis.line.y.left                : NULL
  ..$ axis.line.y.right               : NULL
  ..$ axis.line.theta                 : NULL
  ..$ axis.line.r                     : NULL
  ..$ legend.background               : list()
  .. ..- attr(*, "class")= chr [1:2] "element_blank" "element"
  ..$ legend.margin                   : 'margin' num [1:4] 0points 0points 0points 0points
  .. ..- attr(*, "unit")= int 8
  ..$ legend.spacing                  : 'simpleUnit' num 14points
  .. ..- attr(*, "unit")= int 8
  ..$ legend.spacing.x                : NULL
  ..$ legend.spacing.y                : NULL
  ..$ legend.key                      : list()
  .. ..- attr(*, "class")= chr [1:2] "element_blank" "element"
  ..$ legend.key.size                 : 'simpleUnit' num 15.4points
  .. ..- attr(*, "unit")= int 8
  ..$ legend.key.height               : NULL
  ..$ legend.key.width                : NULL
  ..$ legend.key.spacing              : 'simpleUnit' num 7points
  .. ..- attr(*, "unit")= int 8
  ..$ legend.key.spacing.x            : NULL
  ..$ legend.key.spacing.y            : NULL
  ..$ legend.frame                    : NULL
  ..$ legend.ticks                    : NULL
  ..$ legend.ticks.length             : 'rel' num 0.2
  ..$ legend.axis.line                : NULL
  ..$ legend.text                     :List of 11
  .. ..$ family       : NULL
  .. ..$ face         : NULL
  .. ..$ colour       : NULL
  .. ..$ size         : 'rel' num 0.857
  .. ..$ hjust        : NULL
  .. ..$ vjust        : NULL
  .. ..$ angle        : NULL
  .. ..$ lineheight   : NULL
  .. ..$ margin       : NULL
  .. ..$ debug        : NULL
  .. ..$ inherit.blank: logi TRUE
  .. ..- attr(*, "class")= chr [1:2] "element_text" "element"
  ..$ legend.text.position            : NULL
  ..$ legend.title                    :List of 11
  .. ..$ family       : NULL
  .. ..$ face         : NULL
  .. ..$ colour       : NULL
  .. ..$ size         : NULL
  .. ..$ hjust        : num 0
  .. ..$ vjust        : NULL
  .. ..$ angle        : NULL
  .. ..$ lineheight   : NULL
  .. ..$ margin       : NULL
  .. ..$ debug        : NULL
  .. ..$ inherit.blank: logi TRUE
  .. ..- attr(*, "class")= chr [1:2] "element_text" "element"
  ..$ legend.title.position           : NULL
  ..$ legend.position                 : chr "none"
  ..$ legend.position.inside          : NULL
  ..$ legend.direction                : NULL
  ..$ legend.byrow                    : NULL
  ..$ legend.justification            : chr "center"
  ..$ legend.justification.top        : NULL
  ..$ legend.justification.bottom     : NULL
  ..$ legend.justification.left       : NULL
  ..$ legend.justification.right      : NULL
  ..$ legend.justification.inside     : NULL
  ..$ legend.location                 : NULL
  ..$ legend.box                      : NULL
  ..$ legend.box.just                 : NULL
  ..$ legend.box.margin               : 'margin' num [1:4] 0points 0points 0points 0points
  .. ..- attr(*, "unit")= int 8
  ..$ legend.box.background           : list()
  .. ..- attr(*, "class")= chr [1:2] "element_blank" "element"
  ..$ legend.box.spacing              : 'simpleUnit' num 14points
  .. ..- attr(*, "unit")= int 8
  .. [list output truncated]
  ..- attr(*, "class")= chr [1:2] "theme" "gg"
  ..- attr(*, "complete")= logi TRUE
  ..- attr(*, "validate")= logi TRUE
 $ coordinates:Classes 'CoordCartesian', 'Coord', 'ggproto', 'gg' <ggproto object: Class CoordCartesian, Coord, gg>
    aspect: function
    backtransform_range: function
    clip: off
    default: FALSE
    distance: function
    expand: FALSE
    is_free: function
    is_linear: function
    labels: function
    limits: list
    modify_scales: function
    range: function
    render_axis_h: function
    render_axis_v: function
    render_bg: function
    render_fg: function
    setup_data: function
    setup_layout: function
    setup_panel_guides: function
    setup_panel_params: function
    setup_params: function
    train_panel_guides: function
    transform: function
    super:  <ggproto object: Class CoordCartesian, Coord, gg> 
 $ facet      :Classes 'FacetNull', 'Facet', 'ggproto', 'gg' <ggproto object: Class FacetNull, Facet, gg>
    compute_layout: function
    draw_back: function
    draw_front: function
    draw_labels: function
    draw_panels: function
    finish_data: function
    init_scales: function
    map_data: function
    params: list
    setup_data: function
    setup_params: function
    shrink: TRUE
    train_scales: function
    vars: function
    super:  <ggproto object: Class FacetNull, Facet, gg> 
 $ plot_env   :<environment: 0x7fbdc486a828> 
 $ layout     :Classes 'Layout', 'ggproto', 'gg' <ggproto object: Class Layout, gg>
    coord: NULL
    coord_params: list
    facet: NULL
    facet_params: list
    finish_data: function
    get_scales: function
    layout: NULL
    map_position: function
    panel_params: NULL
    panel_scales_x: NULL
    panel_scales_y: NULL
    render: function
    render_labels: function
    reset_scales: function
    resolve_label: function
    setup: function
    setup_panel_guides: function
    setup_panel_params: function
    train_position: function
    super:  <ggproto object: Class Layout, gg> 
 $ labels     : list()
 - attr(*, "class")= chr [1:2] "gg" "ggplot"

How to find where the label is stored and edit it?

I tried also:

p + geom_label(show.legend = NA)
p + geom_text(show.legend = NA)

but it doesn't change anything.

As an example you can try:

fit <- coxph(Surv(time, status) ~ age, data = lung)
p <- ggforest(fit, data = lung)
p

and in the example the label "age" is to be edited in p


Solution

  • It's much easier to just change it on the data. Adapting the example from ggforest's help:

    # Original
    model <- coxph( Surv(time, status) ~ sex + rx + adhere, data = colon )
    ggforest(model)
    

    Original plot

    # Edited
    names(colon)[4] <- "Lorem ipsum"
    model <- coxph( Surv(time, status) ~ `Lorem ipsum` + rx + adhere, data = colon )
    ggforest(model)
    

    enter image description here

    If you want to remove the label altogether, just rename the variable to " ":

    names(colon)[4] <- " "
    model <- coxph(Surv(time, status) ~ ` ` + rx + adhere, data = colon)
    ggforest(model)
    

    enter image description here

    And if you want to remove multiple labels, rename multiple variables, with a different number of spaces for each:

    names(colon)[4] <- " "
    names(colon)[3] <- "  "
    names(colon)[8] <- "   "
    model <- coxph(Surv(time, status) ~ ` ` + `  ` + `   `, data = colon)
    ggforest(model)
    

    enter image description here