|
@ -14,7 +14,7 @@ |
|
|
#' |
|
|
#' |
|
|
#' \if{latex}{ |
|
|
#' \if{latex}{ |
|
|
#' A sample of the output from \code{coord_proj()} using the Winkel-Tripel projection: |
|
|
#' A sample of the output from \code{coord_proj()} using the Winkel-Tripel projection: |
|
|
#' |
|
|
#'`` |
|
|
#' \figure{coordproj01.png}{options: width=10cm} |
|
|
#' \figure{coordproj01.png}{options: width=10cm} |
|
|
#' } |
|
|
#' } |
|
|
#' |
|
|
#' |
|
@ -100,19 +100,20 @@ coord_proj <- function(proj=NULL, inverse = FALSE, degrees = TRUE, |
|
|
#' @export |
|
|
#' @export |
|
|
CoordProj <- ggproto("CoordProj", Coord, |
|
|
CoordProj <- ggproto("CoordProj", Coord, |
|
|
|
|
|
|
|
|
transform = function(self, data, scale_details) { |
|
|
transform = function(self, data, panel_params) { |
|
|
|
|
|
|
|
|
trans <- project4(self, data$x, data$y) |
|
|
trans <- project4(self, data$x, data$y) |
|
|
out <- cunion(trans[c("x", "y")], data) |
|
|
out <- cunion(trans[c("x", "y")], data) |
|
|
|
|
|
|
|
|
out$x <- rescale(out$x, 0:1, scale_details$x.proj) |
|
|
out$x <- rescale(out$x, 0:1, panel_params$x.proj) |
|
|
out$y <- rescale(out$y, 0:1, scale_details$y.proj) |
|
|
out$y <- rescale(out$y, 0:1, panel_params$y.proj) |
|
|
|
|
|
|
|
|
out |
|
|
out |
|
|
|
|
|
|
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
distance = function(x, y, scale_details) { |
|
|
distance = function(x, y, panel_params) { |
|
|
max_dist <- dist_central_angle(scale_details$x.range, scale_details$y.range) |
|
|
max_dist <- dist_central_angle(panel_params$x.range, panel_params$y.range) |
|
|
dist_central_angle(x, y) / max_dist |
|
|
dist_central_angle(x, y) / max_dist |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
@ -120,13 +121,13 @@ CoordProj <- ggproto("CoordProj", Coord, |
|
|
diff(ranges$y.proj) / diff(ranges$x.proj) |
|
|
diff(ranges$y.proj) / diff(ranges$x.proj) |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
train = function(self, scale_details) { |
|
|
setup_panel_params = function(self, scale_x, scale_y, params = list()) { |
|
|
|
|
|
|
|
|
# range in scale |
|
|
# range in scale |
|
|
ranges <- list() |
|
|
ranges <- list() |
|
|
for (n in c("x", "y")) { |
|
|
for (n in c("x", "y")) { |
|
|
|
|
|
|
|
|
scale <- scale_details[[n]] |
|
|
scale <- get(paste0("scale_", n)) |
|
|
limits <- self$limits[[n]] |
|
|
limits <- self$limits[[n]] |
|
|
|
|
|
|
|
|
if (is.null(limits)) { |
|
|
if (is.null(limits)) { |
|
@ -153,7 +154,8 @@ CoordProj <- ggproto("CoordProj", Coord, |
|
|
ret$y$proj <- proj[3:4] |
|
|
ret$y$proj <- proj[3:4] |
|
|
|
|
|
|
|
|
for (n in c("x", "y")) { |
|
|
for (n in c("x", "y")) { |
|
|
out <- scale_details[[n]]$break_info(ranges[[n]]) |
|
|
out <- get(paste0("scale_", n))$break_info(ranges[[n]]) |
|
|
|
|
|
# out <- panel_params[[n]]$break_info(ranges[[n]]) |
|
|
ret[[n]]$range <- out$range |
|
|
ret[[n]]$range <- out$range |
|
|
ret[[n]]$major <- out$major_source |
|
|
ret[[n]]$major <- out$major_source |
|
|
ret[[n]]$minor <- out$minor_source |
|
|
ret[[n]]$minor <- out$minor_source |
|
@ -170,9 +172,10 @@ CoordProj <- ggproto("CoordProj", Coord, |
|
|
details |
|
|
details |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
render_bg = function(self, scale_details, theme) { |
|
|
render_bg = function(self, panel_params, theme) { |
|
|
xrange <- expand_range(scale_details$x.range, 0.2) |
|
|
|
|
|
yrange <- expand_range(scale_details$y.range, 0.2) |
|
|
xrange <- expand_range(panel_params$x.range, 0.2) |
|
|
|
|
|
yrange <- expand_range(panel_params$y.range, 0.2) |
|
|
|
|
|
|
|
|
# Limit ranges so that lines don't wrap around globe |
|
|
# Limit ranges so that lines don't wrap around globe |
|
|
xmid <- mean(xrange) |
|
|
xmid <- mean(xrange) |
|
@ -182,17 +185,17 @@ CoordProj <- ggproto("CoordProj", Coord, |
|
|
yrange[yrange < ymid - 90] <- ymid - 90 |
|
|
yrange[yrange < ymid - 90] <- ymid - 90 |
|
|
yrange[yrange > ymid + 90] <- ymid + 90 |
|
|
yrange[yrange > ymid + 90] <- ymid + 90 |
|
|
|
|
|
|
|
|
xgrid <- with(scale_details, expand.grid( |
|
|
xgrid <- with(panel_params, expand.grid( |
|
|
y = c(seq(yrange[1], yrange[2], length.out = 50), NA), |
|
|
y = c(seq(yrange[1], yrange[2], length.out = 50), NA), |
|
|
x = x.major |
|
|
x = x.major |
|
|
)) |
|
|
)) |
|
|
ygrid <- with(scale_details, expand.grid( |
|
|
ygrid <- with(panel_params, expand.grid( |
|
|
x = c(seq(xrange[1], xrange[2], length.out = 50), NA), |
|
|
x = c(seq(xrange[1], xrange[2], length.out = 50), NA), |
|
|
y = y.major |
|
|
y = y.major |
|
|
)) |
|
|
)) |
|
|
|
|
|
|
|
|
xlines <- self$transform(xgrid, scale_details) |
|
|
xlines <- self$transform(xgrid, panel_params) |
|
|
ylines <- self$transform(ygrid, scale_details) |
|
|
ylines <- self$transform(ygrid, panel_params) |
|
|
|
|
|
|
|
|
if (nrow(xlines) > 0) { |
|
|
if (nrow(xlines) > 0) { |
|
|
grob.xlines <- element_render( |
|
|
grob.xlines <- element_render( |
|
@ -218,49 +221,49 @@ CoordProj <- ggproto("CoordProj", Coord, |
|
|
)) |
|
|
)) |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
render_axis_h = function(self, scale_details, theme) { |
|
|
render_axis_h = function(self, panel_params, theme) { |
|
|
arrange <- scale_details$x.arrange %||% c("primary", "secondary") |
|
|
arrange <- panel_params$x.arrange %||% c("primary", "secondary") |
|
|
|
|
|
|
|
|
if (is.null(scale_details$x.major)) { |
|
|
if (is.null(panel_params$x.major)) { |
|
|
return(list( |
|
|
return(list( |
|
|
top = zeroGrob(), |
|
|
top = zeroGrob(), |
|
|
bottom = zeroGrob() |
|
|
bottom = zeroGrob() |
|
|
)) |
|
|
)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
x_intercept <- with(scale_details, data.frame( |
|
|
x_intercept <- with(panel_params, data.frame( |
|
|
x = x.major, |
|
|
x = x.major, |
|
|
y = y.range[1] |
|
|
y = y.range[1] |
|
|
)) |
|
|
)) |
|
|
pos <- self$transform(x_intercept, scale_details) |
|
|
pos <- self$transform(x_intercept, panel_params) |
|
|
|
|
|
|
|
|
axes <- list( |
|
|
axes <- list( |
|
|
bottom = guide_axis(pos$x, scale_details$x.labels, "bottom", theme), |
|
|
bottom = guide_axis(pos$x, panel_params$x.labels, "bottom", theme), |
|
|
top = guide_axis(pos$x, scale_details$x.labels, "top", theme) |
|
|
top = guide_axis(pos$x, panel_params$x.labels, "top", theme) |
|
|
) |
|
|
) |
|
|
axes[[which(arrange == "secondary")]] <- zeroGrob() |
|
|
axes[[which(arrange == "secondary")]] <- zeroGrob() |
|
|
axes |
|
|
axes |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
render_axis_v = function(self, scale_details, theme) { |
|
|
render_axis_v = function(self, panel_params, theme) { |
|
|
arrange <- scale_details$y.arrange %||% c("primary", "secondary") |
|
|
arrange <- panel_params$y.arrange %||% c("primary", "secondary") |
|
|
|
|
|
|
|
|
if (is.null(scale_details$y.major)) { |
|
|
if (is.null(panel_params$y.major)) { |
|
|
return(list( |
|
|
return(list( |
|
|
left = zeroGrob(), |
|
|
left = zeroGrob(), |
|
|
right = zeroGrob() |
|
|
right = zeroGrob() |
|
|
)) |
|
|
)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
x_intercept <- with(scale_details, data.frame( |
|
|
x_intercept <- with(panel_params, data.frame( |
|
|
x = x.range[1], |
|
|
x = x.range[1], |
|
|
y = y.major |
|
|
y = y.major |
|
|
)) |
|
|
)) |
|
|
pos <- self$transform(x_intercept, scale_details) |
|
|
pos <- self$transform(x_intercept, panel_params) |
|
|
|
|
|
|
|
|
axes <- list( |
|
|
axes <- list( |
|
|
left = guide_axis(pos$y, scale_details$y.labels, "left", theme), |
|
|
left = guide_axis(pos$y, panel_params$y.labels, "left", theme), |
|
|
right = guide_axis(pos$y, scale_details$y.labels, "right", theme) |
|
|
right = guide_axis(pos$y, panel_params$y.labels, "right", theme) |
|
|
) |
|
|
) |
|
|
axes[[which(arrange == "secondary")]] <- zeroGrob() |
|
|
axes[[which(arrange == "secondary")]] <- zeroGrob() |
|
|
axes |
|
|
axes |
|
|