how to extract legend title information from ggplot objects

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?

I get an error when I run your code (not sure if it's a problem with my setup or if something is missing from your example), so I've used a different example from the ggcorrmat help. I've used ggplot_build to extract the legend title information, but there may be a more straightforward way:

p = ggcorrmat(
  data = datasets::iris,
  cor.vars = c(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width),
  sig.level = 0.01,
  ggtheme = ggplot2::theme_bw(),
  hc.order = TRUE,
  matrix.type = "lower",
  outline.col = "white",
  title = "Dataset: Iris"
)

pb = ggplot_build(p)
pb$plot$plot_env$legend.title
atop(atop(bold("sample size:"), italic(n) ~ "=" ~ 150L), atop(bold("correlation:"), 
    "pearson"))

I identified the location of the legend title expression by running map(pb$plot, ~names(.x)[grep("legend|title", names(.x))]) and inspecting the result.

4 Likes

Sorry about the reprex. Forgot that the example I was using would work only with the development version of the package on my local machine.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.