@@ -103,6 +103,75 @@ CoordCartesian <- ggproto("CoordCartesian", Coord,
103103 )
104104 },
105105
106+ setup_panel_guides = function (self , panel_params , guides , params = list ()) {
107+ aesthetics <- c(" x" , " y" , " x.sec" , " y.sec" )
108+ names(aesthetics ) <- aesthetics
109+
110+ # resolve the specified guide from the scale and/or guides
111+ guides <- lapply(aesthetics , function (aesthetic ) {
112+ resolve_guide(
113+ aesthetic ,
114+ panel_params [[aesthetic ]],
115+ guides ,
116+ default = guide_axis(),
117+ null = guide_none()
118+ )
119+ })
120+
121+ # resolve the guide definition as a "guide" S3
122+ guides <- lapply(guides , validate_guide )
123+
124+ # if there is an "position" specification in the scale, pass this on to the guide
125+ # ideally, this should be specified in the guide
126+ guides <- lapply(aesthetics , function (aesthetic ) {
127+ guide <- guides [[aesthetic ]]
128+ scale <- panel_params [[aesthetic ]]
129+ # position could be NULL here for an empty scale
130+ guide $ position <- guide $ position %| W | % scale $ position
131+ guide
132+ })
133+
134+ panel_params $ guides <- guides
135+ panel_params
136+ },
137+
138+ train_panel_guides = function (self , panel_params , layers , default_mapping , params = list ()) {
139+ aesthetics <- c(" x" , " y" , " x.sec" , " y.sec" )
140+ names(aesthetics ) <- aesthetics
141+
142+ panel_params $ guides <- lapply(aesthetics , function (aesthetic ) {
143+ axis <- substr(aesthetic , 1 , 1 )
144+ guide <- panel_params $ guides [[aesthetic ]]
145+ guide <- guide_train(guide , panel_params [[aesthetic ]])
146+ guide <- guide_transform(guide , self , panel_params )
147+ guide <- guide_geom(guide , layers , default_mapping )
148+ guide
149+ })
150+
151+ panel_params
152+ },
153+
154+ labels = function (self , labels , panel_params ) {
155+ positions_x <- c(" top" , " bottom" )
156+ positions_y <- c(" left" , " right" )
157+
158+ list (
159+ x = lapply(c(1 , 2 ), function (i ) {
160+ panel_guide_label(
161+ panel_params $ guides ,
162+ position = positions_x [[i ]],
163+ default_label = labels $ x [[i ]]
164+ )
165+ }),
166+ y = lapply(c(1 , 2 ), function (i ) {
167+ panel_guide_label(
168+ panel_params $ guides ,
169+ position = positions_y [[i ]],
170+ default_label = labels $ y [[i ]])
171+ })
172+ )
173+ },
174+
106175 render_bg = function (panel_params , theme ) {
107176 guide_grid(
108177 theme ,
@@ -114,24 +183,16 @@ CoordCartesian <- ggproto("CoordCartesian", Coord,
114183 },
115184
116185 render_axis_h = function (panel_params , theme ) {
117- arrange <- panel_params $ x.arrange %|| % c(" secondary" , " primary" )
118- arrange_scale_keys <- c(" primary" = " x" , " secondary" = " x.sec" )[arrange ]
119- arrange_scales <- panel_params [arrange_scale_keys ]
120-
121186 list (
122- top = draw_view_scale_axis( arrange_scales [[ 1 ]], " top" , theme ),
123- bottom = draw_view_scale_axis( arrange_scales [[ 2 ]], " bottom" , theme )
187+ top = panel_guides_grob( panel_params $ guides , position = " top" , theme = theme ),
188+ bottom = panel_guides_grob( panel_params $ guides , position = " bottom" , theme = theme )
124189 )
125190 },
126191
127192 render_axis_v = function (panel_params , theme ) {
128- arrange <- panel_params $ y.arrange %|| % c(" primary" , " secondary" )
129- arrange_scale_keys <- c(" primary" = " y" , " secondary" = " y.sec" )[arrange ]
130- arrange_scales <- panel_params [arrange_scale_keys ]
131-
132193 list (
133- left = draw_view_scale_axis( arrange_scales [[ 1 ]], " left" , theme ),
134- right = draw_view_scale_axis( arrange_scales [[ 2 ]], " right" , theme )
194+ left = panel_guides_grob( panel_params $ guides , position = " left" , theme = theme ),
195+ right = panel_guides_grob( panel_params $ guides , position = " right" , theme = theme )
135196 )
136197 }
137198)
@@ -153,10 +214,24 @@ view_scales_from_scale <- function(scale, coord_limits = NULL, expand = TRUE) {
153214 view_scales
154215}
155216
156- draw_view_scale_axis <- function (view_scale , axis_position , theme ) {
157- if (is.null(view_scale ) || view_scale $ is_empty()) {
158- return (zeroGrob())
159- }
217+ panel_guide_label <- function (guides , position , default_label ) {
218+ guide <- guide_for_position(guides , position ) %|| % guide_none(title = NULL )
219+ guide $ title %| W | % default_label
220+ }
221+
222+ panel_guides_grob <- function (guides , position , theme ) {
223+ guide <- guide_for_position(guides , position ) %|| % guide_none()
224+ guide_gengrob(guide , theme )
225+ }
226+
227+ guide_for_position <- function (guides , position ) {
228+ has_position <- vapply(
229+ guides ,
230+ function (guide ) identical(guide $ position , position ),
231+ logical (1 )
232+ )
160233
161- draw_axis(view_scale $ break_positions(), view_scale $ get_labels(), axis_position , theme )
234+ guides <- guides [has_position ]
235+ guides_order <- vapply(guides , function (guide ) as.numeric(guide $ order )[1 ], numeric (1 ))
236+ Reduce(guide_merge , guides [order(guides_order )])
162237}
0 commit comments