-
Notifications
You must be signed in to change notification settings - Fork 2.1k
WIP: scale-like objects in panel_params that can be used to train guides #3356
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
Merged
paleolimbot
merged 10 commits into
tidyverse:master
from
paleolimbot:coord-cartesian-view-scale
Jun 18, 2019
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e61b974
remove assumption that the panel_params contain any particular elemen…
paleolimbot 6f3816b
move scale_range to coord_sf(), since it is no longer used in coord_c…
paleolimbot bbaceb0
Update scales documentation, tidy styling of Scales methods
paleolimbot 690c6f3
finish tidy styling of scale-.r
paleolimbot 7ec7a6e
small modifications to scales such that ViewScales can be applied gen…
paleolimbot 91b509d
move view scale construction outside coord-cartesian.R
paleolimbot 22e8ec1
move view scale to separate file
paleolimbot b6294dd
move all view scale code to scale-view.r, rename constructors to view…
paleolimbot 190f967
Better documentation of ViewScale objects
paleolimbot 9aff4e8
make view_scale_secondary() return a ViewScale subclass
paleolimbot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,50 +79,91 @@ CoordCartesian <- ggproto("CoordCartesian", Coord, | |
is_free = function() TRUE, | ||
|
||
distance = function(x, y, panel_params) { | ||
max_dist <- dist_euclidean(panel_params$x.range, panel_params$y.range) | ||
max_dist <- dist_euclidean(panel_params$x$dimension(), panel_params$y$dimension()) | ||
dist_euclidean(x, y) / max_dist | ||
}, | ||
|
||
range = function(panel_params) { | ||
list(x = panel_params$x.range, y = panel_params$y.range) | ||
list(x = panel_params$x$dimension(), y = panel_params$y$dimension()) | ||
}, | ||
|
||
backtransform_range = function(self, panel_params) { | ||
self$range(panel_params) | ||
}, | ||
|
||
transform = function(data, panel_params) { | ||
rescale_x <- function(data) rescale(data, from = panel_params$x.range) | ||
rescale_y <- function(data) rescale(data, from = panel_params$y.range) | ||
|
||
data <- transform_position(data, rescale_x, rescale_y) | ||
data <- transform_position(data, panel_params$x$rescale, panel_params$y$rescale) | ||
transform_position(data, squish_infinite, squish_infinite) | ||
}, | ||
|
||
setup_panel_params = function(self, scale_x, scale_y, params = list()) { | ||
train_cartesian <- function(scale, limits, name) { | ||
range <- scale_range(scale, limits, self$expand) | ||
c( | ||
view_scales_from_scale(scale_x, self$limits$x, self$expand), | ||
view_scales_from_scale(scale_y, self$limits$y, self$expand) | ||
) | ||
}, | ||
|
||
render_bg = function(panel_params, theme) { | ||
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. Maybe also make |
||
guide_grid( | ||
theme, | ||
panel_params$x$break_positions_minor(), | ||
panel_params$x$break_positions(), | ||
panel_params$y$break_positions_minor(), | ||
panel_params$y$break_positions() | ||
) | ||
}, | ||
|
||
out <- scale$break_info(range) | ||
out$arrange <- scale$axis_order() | ||
names(out) <- paste(name, names(out), sep = ".") | ||
out | ||
} | ||
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] | ||
|
||
c( | ||
train_cartesian(scale_x, self$limits$x, "x"), | ||
train_cartesian(scale_y, self$limits$y, "y") | ||
list( | ||
top = draw_view_scale_axis(arrange_scales[[1]], "top", theme), | ||
bottom = draw_view_scale_axis(arrange_scales[[2]], "bottom", 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) | ||
) | ||
} | ||
) | ||
|
||
scale_range <- function(scale, limits = NULL, expand = TRUE) { | ||
expansion <- if (expand) expand_default(scale) else c(0, 0) | ||
view_scales_from_scale <- function(scale, coord_limits = NULL, expand = TRUE) { | ||
expansion <- if (expand) expand_default(scale) else expand_scale(0, 0) | ||
limits <- scale$get_limits() | ||
|
||
if (is.null(limits)) { | ||
scale$dimension(expansion) | ||
if (is.null(coord_limits)) { | ||
continuous_range <- scale$dimension(expansion, limits) | ||
} else { | ||
range <- range(scale$transform(limits)) | ||
expand_range(range, expansion[1], expansion[2]) | ||
continuous_range <- range(scale$transform(coord_limits)) | ||
continuous_range <- expand_range4(continuous_range, expansion) | ||
} | ||
|
||
aesthetic <- scale$aesthetics[1] | ||
|
||
view_scales <- list( | ||
view_scale(scale, limits, continuous_range), | ||
paleolimbot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
sec = second_view_scale(scale, limits, continuous_range), | ||
arrange = scale$axis_order(), | ||
range = continuous_range | ||
) | ||
names(view_scales) <- c(aesthetic, paste0(aesthetic, ".", names(view_scales)[-1])) | ||
|
||
view_scales | ||
} | ||
|
||
draw_view_scale_axis <- function(view_scale, axis_position, theme) { | ||
if(is.null(view_scale) || view_scale$is_empty()) { | ||
return(zeroGrob()) | ||
} | ||
|
||
draw_axis(view_scale$break_positions(), view_scale$get_labels(), axis_position, theme) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.