@@ -741,6 +741,91 @@ Guides <- ggproto(
741
741
}
742
742
)
743
743
744
+ # Data accessor -----------------------------------------------------------
745
+
746
+ # ' Extract tick information from guides
747
+ # '
748
+ # ' `get_guide_data()` builds a plot and extracts information from guide keys. This
749
+ # ' information typically contains positions, values and/or labels, depending
750
+ # ' on which aesthetic is queried or guide is used.
751
+ # '
752
+ # ' @param plot A `ggplot` or `ggplot_build` object.
753
+ # ' @param aesthetic A string that describes a single aesthetic for which to
754
+ # ' extract guide information. For example: `"colour"`, `"size"`, `"x"` or
755
+ # ' `"y.sec"`.
756
+ # ' @param panel An integer giving a panel number for which to return position guide
757
+ # ' information.
758
+ # '
759
+ # ' @return
760
+ # ' One of the following:
761
+ # ' * A `data.frame` representing the guide key, when the guide is unique for
762
+ # ' the aesthetic.
763
+ # ' * A `list` when the coord does not support position axes or multiple guides
764
+ # ' match the aesthetic.
765
+ # ' * `NULL` when no guide key could be found.
766
+ # ' @export
767
+ # ' @keywords internal
768
+ # '
769
+ # ' @examples
770
+ # ' # A standard plot
771
+ # ' p <- ggplot(mtcars) +
772
+ # ' aes(mpg, disp, colour = drat, size = drat) +
773
+ # ' geom_point() +
774
+ # ' facet_wrap(vars(cyl), scales = "free_x")
775
+ # '
776
+ # ' # Guide information for legends
777
+ # ' get_guide_data(p, "size")
778
+ # '
779
+ # ' # Note that legend guides can be merged
780
+ # ' merged <- p + guides(colour = "legend")
781
+ # ' get_guide_data(merged, "size")
782
+ # '
783
+ # ' # Guide information for positions
784
+ # ' get_guide_data(p, "x", panel = 2)
785
+ # '
786
+ # ' # Coord polar doesn't support proper guides, so we get a list
787
+ # ' polar <- p + coord_polar()
788
+ # ' get_guide_data(polar, "theta", panel = 2)
789
+ get_guide_data <- function (plot = last_plot(), aesthetic , panel = 1L ) {
790
+
791
+ check_string(aesthetic , allow_empty = FALSE )
792
+ aesthetic <- standardise_aes_names(aesthetic )
793
+
794
+ if (! inherits(plot , " ggplot_built" )) {
795
+ plot <- ggplot_build(plot )
796
+ }
797
+
798
+ if (! aesthetic %in% c(" x" , " y" , " x.sec" , " y.sec" , " theta" , " r" )) {
799
+ # Non position guides: check if aesthetic in colnames of key
800
+ keys <- lapply(plot $ plot $ guides $ params , `[[` , " key" )
801
+ keep <- vapply(keys , function (x ) any(colnames(x ) %in% aesthetic ), logical (1 ))
802
+ keys <- switch (sum(keep ) + 1 , NULL , keys [[which(keep )]], keys [keep ])
803
+ return (keys )
804
+ }
805
+
806
+ # Position guides: find the right layout entry
807
+ check_number_whole(panel )
808
+ layout <- plot $ layout $ layout
809
+ select <- layout [layout $ PANEL == panel , , drop = FALSE ]
810
+ if (nrow(select ) == 0 ) {
811
+ return (NULL )
812
+ }
813
+ params <- plot $ layout $ panel_params [select $ PANEL ][[1 ]]
814
+
815
+ # If panel params don't have guides, we probably have old coord system
816
+ # that doesn't use the guide system.
817
+ if (is.null(params $ guides )) {
818
+ # Old system: just return relevant parameters
819
+ aesthetic <- paste(aesthetic , c(" major" , " minor" , " labels" , " range" ), sep = " ." )
820
+ params <- params [intersect(names(params ), aesthetic )]
821
+ return (params )
822
+ } else {
823
+ # Get and return key
824
+ key <- params $ guides $ get_params(aesthetic )$ key
825
+ return (key )
826
+ }
827
+ }
828
+
744
829
# Helpers -----------------------------------------------------------------
745
830
746
831
matched_aes <- function (layer , guide ) {
0 commit comments