I am writing units tests for my package and having trouble extracting the legend title component of the following plot (p
) object.
# setup
set.seed(123)
library(tidyverse)
library(ggstatsplot)
# plot
(p <- ggstatsplot::ggcorrmat(data = ggplot2::msleep))
I can extract the data associated with this plot and other labels as well, but can't seem to figure out how to get the legend title.
# data
(dat <- as_tibble(ggplot2::layer_data(p)))
#> # A tibble: 36 x 15
#> fill x y PANEL group xmin xmax ymin ymax colour size
#> <chr> <int> <int> <fct> <int> <dbl> <dbl> <dbl> <dbl> <chr> <dbl>
#> 1 #009~ 1 1 1 1 0.5 1.5 0.5 1.5 black 0.1
#> 2 #60B~ 2 1 1 7 1.5 2.5 0.5 1.5 black 0.1
#> 3 #FCD~ 3 1 1 13 2.5 3.5 0.5 1.5 black 0.1
#> 4 #E69~ 4 1 1 19 3.5 4.5 0.5 1.5 black 0.1
#> 5 #FFD~ 5 1 1 25 4.5 5.5 0.5 1.5 black 0.1
#> 6 #FFE~ 6 1 1 31 5.5 6.5 0.5 1.5 black 0.1
#> 7 #60B~ 1 2 1 2 0.5 1.5 1.5 2.5 black 0.1
#> 8 #009~ 2 2 1 8 1.5 2.5 1.5 2.5 black 0.1
#> 9 #FFD~ 3 2 1 14 2.5 3.5 1.5 2.5 black 0.1
#> 10 #F3B~ 4 2 1 20 3.5 4.5 1.5 2.5 black 0.1
#> # ... with 26 more rows, and 4 more variables: linetype <dbl>,
#> # alpha <lgl>, width <lgl>, height <lgl>
# labels
p$labels
#> $xlab
#> NULL
#>
#> $ylab
#> NULL
#>
#> $title
#> NULL
#>
#> $subtitle
#> NULL
#>
#> $caption
#> atop(expr = paste(bold("X"), " = correlation non-significant at ",
#> italic("p"), " < ", 0.05, sep = ""), " (Adjustment: None)")
#>
#> $fill
#> [1] "value"
#>
#> $x
#> [1] "Var1"
#>
#> $y
#> [1] "Var2"
The text displayed as legend title here comes from input to name
argument in ggplot2::scale_fill_gradient2
. So I tried to find a name
element in scales
list, but it's not there.
p$scales
#> <ggproto object: Class ScalesList, gg>
#> add: function
#> clone: function
#> find: function
#> get_scales: function
#> has_scale: function
#> input: function
#> n: function
#> non_position_scales: function
#> scales: list
#> super: <ggproto object: Class ScalesList, gg>
p$scales$scales
#> [[1]]
#> <ScaleContinuous>
#> Range:
#> Limits: -1 -- 1
Any ideas on how I extract this information from the plot object?