-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Make position guides customizable #3398
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 20 commits
1d788aa
422fcd3
1d4daf0
4a7e54e
0496e55
0c0c01a
8215477
d6c35c7
1f092f2
1281280
c0bc334
6244983
50ef6f5
2b2f4fb
4a45706
f91a546
7c435ae
e9358ed
8942c56
e060347
b05111a
5e5d748
2d040c7
b52fa59
2ef4fe1
f11609a
b738de5
0cc850e
92e16ae
f930075
6448aa0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,6 +103,73 @@ CoordCartesian <- ggproto("CoordCartesian", Coord, | |
) | ||
}, | ||
|
||
setup_panel_guides = function(self, panel_params, guides, params = list()) { | ||
aesthetics <- c("x", "y", "x.sec", "y.sec") | ||
names(aesthetics) <- aesthetics | ||
|
||
# resolve the specified guide from the scale and/or guides | ||
guides <- lapply(aesthetics, function(aesthetic) { | ||
resolve_guide( | ||
aesthetic, | ||
panel_params[[aesthetic]], | ||
guides, | ||
default = guide_axis(), | ||
null = guide_none() | ||
) | ||
}) | ||
|
||
# resolve the guide definition as a "guide" S3 | ||
guides <- lapply(guides, validate_guide) | ||
|
||
# if there is an "position" specification in the scale, pass this on to the guide | ||
# ideally, this should be specified in the guide | ||
guides <- lapply(aesthetics, function(aesthetic) { | ||
guide <- guides[[aesthetic]] | ||
scale <- panel_params[[aesthetic]] | ||
# position could be NULL here for an empty scale | ||
guide$position <- guide$position %|W|% scale$position | ||
guide | ||
}) | ||
|
||
panel_params$guides <- guides | ||
panel_params | ||
}, | ||
|
||
train_panel_guides = function(self, panel_params, layers, default_mapping, params = list()) { | ||
aesthetics <- c("x", "y", "x.sec", "y.sec") | ||
names(aesthetics) <- aesthetics | ||
|
||
panel_params$guides <- lapply(aesthetics, function(aesthetic) { | ||
axis <- substr(aesthetic, 1, 1) | ||
guide <- panel_params$guides[[aesthetic]] | ||
guide <- guide_train(guide, panel_params[[aesthetic]]) | ||
guide <- guide_transform(guide, self, panel_params) | ||
guide <- guide_geom(guide, layers, default_mapping) | ||
guide | ||
}) | ||
|
||
panel_params | ||
}, | ||
|
||
labels = function(self, labels, panel_params) { | ||
positions_x <- c("top", "bottom") | ||
positions_y <- c("left", "right") | ||
|
||
list( | ||
x = lapply(c(1, 2), function(i) panel_guide_label( | ||
panel_params$guides, | ||
position = positions_x[[i]], | ||
default_label = labels$x[[i]] | ||
) | ||
), | ||
y = lapply(c(1, 2), function(i) panel_guide_label( | ||
panel_params$guides, | ||
position = positions_y[[i]], | ||
default_label = labels$y[[i]]) | ||
) | ||
) | ||
}, | ||
|
||
render_bg = function(panel_params, theme) { | ||
guide_grid( | ||
theme, | ||
|
@@ -114,24 +181,16 @@ CoordCartesian <- ggproto("CoordCartesian", Coord, | |
}, | ||
|
||
render_axis_h = function(panel_params, theme) { | ||
arrange <- panel_params$x.arrange %||% c("secondary", "primary") | ||
arrange_scale_keys <- c("primary" = "x", "secondary" = "x.sec")[arrange] | ||
arrange_scales <- panel_params[arrange_scale_keys] | ||
|
||
list( | ||
top = draw_view_scale_axis(arrange_scales[[1]], "top", theme), | ||
bottom = draw_view_scale_axis(arrange_scales[[2]], "bottom", theme) | ||
top = panel_guides_grob(panel_params$guides, position = "top", theme = theme), | ||
bottom = panel_guides_grob(panel_params$guides, position = "bottom", theme = theme) | ||
) | ||
}, | ||
|
||
render_axis_v = function(panel_params, theme) { | ||
arrange <- panel_params$y.arrange %||% c("primary", "secondary") | ||
arrange_scale_keys <- c("primary" = "y", "secondary" = "y.sec")[arrange] | ||
arrange_scales <- panel_params[arrange_scale_keys] | ||
|
||
list( | ||
left = draw_view_scale_axis(arrange_scales[[1]], "left", theme), | ||
right = draw_view_scale_axis(arrange_scales[[2]], "right", theme) | ||
left = panel_guides_grob(panel_params$guides, position = "left", theme = theme), | ||
right = panel_guides_grob(panel_params$guides, position = "right", theme = theme) | ||
) | ||
} | ||
) | ||
|
@@ -153,10 +212,41 @@ view_scales_from_scale <- function(scale, coord_limits = NULL, expand = TRUE) { | |
view_scales | ||
} | ||
|
||
draw_view_scale_axis <- function(view_scale, axis_position, theme) { | ||
if(is.null(view_scale) || view_scale$is_empty()) { | ||
panel_guide_label <- function(guides, position, default_label) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't all these helpers below be placed in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right now |
||
guides <- guides_filter_by_position(guides, position) | ||
|
||
if (length(guides) == 0) { | ||
default_label | ||
} else { | ||
guides[[1]]$title %||% waiver() %|W|% default_label | ||
paleolimbot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
panel_guides_grob <- function(guides, position, theme) { | ||
guides <- guides_filter_by_position(guides, position) | ||
grobs <- lapply(guides, guide_gengrob, theme) | ||
|
||
if (length(grobs) == 0) { | ||
return(zeroGrob()) | ||
} else if (length(grobs) == 1) { | ||
grobs[[1]] | ||
} else { | ||
warning( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is something weird going on with the logic here... If I modify your own examples I get: p <- ggplot(mpg, aes(cty * 100, hwy * 100)) +
geom_point() +
facet_wrap(vars(class))
# axis guides can be customized in the scale_* functions or
# using guides()
p + scale_x_continuous(guide = guide_axis(position = 'top'))
#> Warning messages:
#> 1: In panel_guides_grob(panel_params$guides, position = "top", theme = theme) :
#> More than one position guide found at `position = "top". Only showing the first guide.
#> 2: In panel_guides_grob(panel_params$guides, position = "top", theme = theme) :
#> More than one position guide found at `position = "top". Only showing the first guide.
#> 3: In panel_guides_grob(panel_params$guides, position = "top", theme = theme) :
#> More than one position guide found at `position = "top". Only showing the first guide.
#> 4: In panel_guides_grob(panel_params$guides, position = "top", theme = theme) :
#> More than one position guide found at `position = "top". Only showing the first guide.
#> 5: In panel_guides_grob(panel_params$guides, position = "top", theme = theme) :
#> More than one position guide found at `position = "top". Only showing the first guide.
#> 6: In panel_guides_grob(panel_params$guides, position = "top", theme = theme) :
#> More than one position guide found at `position = "top". Only showing the first guide.
#> 7: In panel_guides_grob(panel_params$guides, position = "top", theme = theme) :
#> More than one position guide found at `position = "top". Only showing the first guide. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch! I tweaked this so that it uses There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you provide an example of how it doesn't play nicely with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is what I had in mind: library(ggplot2)
p <- ggplot(mpg, aes(cty, hwy)) +
geom_point()
p +
guides(x.sec = guide_axis()) +
labs(x.sec = "fish") Created on 2019-09-22 by the reprex package (v0.2.1) |
||
"More than one position guide found at `position = \"", position, "\". ", | ||
"Only showing the first guide." | ||
) | ||
grobs[[1]] | ||
} | ||
} | ||
|
||
guides_filter_by_position <- function(guides, position) { | ||
has_position <- vapply( | ||
guides, | ||
function(guide) identical(guide$position, position), | ||
logical(1) | ||
) | ||
|
||
draw_axis(view_scale$break_positions(), view_scale$get_labels(), axis_position, theme) | ||
guides <- guides[has_position] | ||
guides_order <- vapply(guides, function(guide) as.numeric(guide$order)[1], numeric(1)) | ||
guides[order(guides_order)] | ||
} |
Uh oh!
There was an error while loading. Please reload this page.