Skip to content

Commit

Permalink
overhaul non-standard (maybe secondary) aesthetics for vector & isoli…
Browse files Browse the repository at this point in the history
…ne geoms
  • Loading branch information
corybrunson committed Jan 23, 2025
1 parent bd96205 commit 32ce3a3
Show file tree
Hide file tree
Showing 71 changed files with 485 additions and 370 deletions.
36 changes: 20 additions & 16 deletions R/geom-isoline.r
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,9 @@
#' - `vjust`
#' - `family`
#' - `fontface`
#' - `text_colour`, `text_alpha`, `text_size`, `text_angle`,
#' - `group`
#'

#' The prefixed aesthetics `text_*` are used by the text elements and will
#' inherit any values passed to their un-prefixed counterparts.
#'

#' @import ggplot2
#' @inheritParams ggplot2::layer
#' @template param-geom
Expand All @@ -45,6 +40,9 @@
#' one.
#' @param text_dodge Numeric; the orthogonal distance of the text from the axis
#' or isoline, as a proportion of the minimum of the plot width and height.
#' @param text.size,text.angle,text.colour,text.color,text.alpha Default
#' aesthetics for tick mark labels. Set to NULL to inherit from the data's
#' aesthetics.
#' @template return-layer
#' @family geom layers
#' @example inst/examples/ex-geom-isoline.r
Expand All @@ -55,6 +53,8 @@ geom_isoline <- function(
by = NULL, num = NULL,
text_dodge = .03,
...,
text.size = 3, text.angle = 0,
text.colour = NULL, text.color = NULL, text.alpha = NULL,
parse = FALSE, check_overlap = FALSE,
na.rm = FALSE,
show.legend = NA, inherit.aes = TRUE
Expand All @@ -71,6 +71,10 @@ geom_isoline <- function(
isoline_text = isoline_text,
by = by, num = num,
text_dodge = text_dodge,
text.size = text.size,
text.angle = text.angle,
text.colour = text.color %||% text.colour,
text.alpha = text.alpha,
parse = parse,
check_overlap = check_overlap,
na.rm = na.rm,
Expand All @@ -93,12 +97,10 @@ GeomIsoline <- ggproto(
# isoline
colour = "black", alpha = .8,
linewidth = .5, linetype = "dashed",
# mark needs
center = 0, scale = 1,
# isoline mark text
text_colour = "black", text_alpha = .8, text_size = 3, text_angle = 0,
hjust = "inward", vjust = 1,
family = "", fontface = 1
family = "", fontface = 1,
# mark needs
center = 0, scale = 1
),

setup_params = function(data, params) {
Expand Down Expand Up @@ -129,6 +131,8 @@ GeomIsoline <- ggproto(
isoline_text = TRUE,
by = NULL, num = NULL,
text_dodge = .03,
text.size = 3, text.angle = 0,
text.colour = NULL, text.color = NULL, text.alpha = NULL,
parse = FALSE, check_overlap = FALSE,
na.rm = TRUE
) {
Expand Down Expand Up @@ -166,6 +170,11 @@ GeomIsoline <- ggproto(

if (isoline_text) {
text_data <- data
# specify independent aesthetics
text_data$size <- text.size %||% text_data$size
# text_data$angle <- text.angle %||% text_data$angle
text_data$colour <- text.colour %||% text_data$colour
text_data$alpha <- text.alpha %||% text_data$alpha

# omit labels at origin
text_data <- subset(text_data, x_t != 0 | y_t != 0)
Expand All @@ -181,14 +190,9 @@ GeomIsoline <- ggproto(
# update text angle and put in degrees
text_data <- transform(
text_data,
angle = (atan(- 1 / tan(angle)) + text_angle) * 180 / pi
angle = (atan(- 1 / tan(angle)) + text.angle) * 180 / pi
)

# specify aesthetics
text_data$colour <- text_data$text_colour
text_data$alpha <- text_data$text_alpha
text_data$size <- text_data$text_size

# isoline text grobs
grobs <- c(grobs, list(GeomText$draw_panel(
data = text_data, panel_params = panel_params, coord = coord,
Expand Down
26 changes: 17 additions & 9 deletions R/geom-vector.r
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@
#' - `linetype`
#' - `label`
#' - `size`
#' - `angle`, `hjust`, `vjust`
#' - `label_colour`, `label_alpha`
#' - `family`, `fontface`, `lineheight`
#' - `angle`
#' - `hjust`
#' - `vjust`
#' - `family`
#' - `fontface`
#' - `lineheight`
#' - `group`
#'

Expand All @@ -47,6 +50,8 @@
#' `NULL` for no arrows.
#' @param vector_labels Logical; whether to include labels radiating outward
#' from the vectors.
#' @param label.colour,label.color,label.alpha Default aesthetics for labels.
#' Set to NULL to inherit from the data's aesthetics.
#' @template return-layer
#' @family geom layers
#' @example inst/examples/ex-geom-vector.r
Expand All @@ -56,6 +61,8 @@ geom_vector <- function(
arrow = default_arrow, lineend = "round", linejoin = "mitre",
vector_labels = TRUE,
...,
label.colour = NULL, label.color = NULL, label.alpha = NULL,
parse = FALSE, check_overlap = FALSE,
na.rm = FALSE,
show.legend = NA, inherit.aes = TRUE
) {
Expand All @@ -70,6 +77,9 @@ geom_vector <- function(
params = list(
arrow = arrow, lineend = lineend, linejoin = linejoin,
vector_labels = vector_labels,
label.colour = label.color %||% label.colour,
parse = parse,
check_overlap = check_overlap,
na.rm = na.rm,
...
)
Expand All @@ -89,7 +99,6 @@ GeomVector <- ggproto(
default_aes = aes(
colour = "black", linewidth = 0.5, linetype = 1, alpha = NA,
label = "", size = 3.88, angle = 0, hjust = .5, vjust = .5,
label_colour = "black", label_alpha = NA,
family = "", fontface = 1, lineheight = 1.2
),

Expand All @@ -106,6 +115,7 @@ GeomVector <- ggproto(
data, panel_params, coord,
vector_labels = TRUE,
arrow = default_arrow, lineend = "round", linejoin = "mitre",
label.colour = NULL, label.alpha = NULL,
parse = FALSE, check_overlap = FALSE,
na.rm = FALSE
) {
Expand All @@ -128,11 +138,9 @@ GeomVector <- ggproto(

if (vector_labels) {
label_data <- data

# specify aesthetics (if necessary)
label_data$colour <- label_data$label_colour
label_data$alpha <- label_data$label_alpha
label_data$label_colour <- label_data$label_alpha <- NULL
# specify independent aesthetics
label_data$colour <- label.colour %||% label_data$colour
label_data$alpha <- label.alpha %||% label_data$alpha

if (is.character(label_data$hjust)) {
label_data$hjust <- compute_just(label_data$hjust, label_data$x)
Expand Down
32 changes: 30 additions & 2 deletions R/zzz-biplot-geoms.r
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,11 @@ geom_rows_isoline <- function(
num = NULL,
text_dodge = 0.03,
...,
text.size = 3,
text.angle = 0,
text.colour = NULL,
text.color = NULL,
text.alpha = NULL,
parse = FALSE,
check_overlap = FALSE,
na.rm = FALSE,
Expand All @@ -1049,7 +1054,9 @@ geom_rows_isoline <- function(
geom = GeomIsoline, position = position, show.legend = show.legend,
inherit.aes = inherit.aes, params = list(isoline_text = isoline_text,
by = by, num = num, text_dodge = text_dodge,
parse = parse, check_overlap = check_overlap,
text.size = text.size, text.angle = text.angle,
text.colour = text.color %||% text.colour,
text.alpha = text.alpha, parse = parse, check_overlap = check_overlap,
na.rm = na.rm, ...))

}
Expand All @@ -1066,6 +1073,11 @@ geom_cols_isoline <- function(
num = NULL,
text_dodge = 0.03,
...,
text.size = 3,
text.angle = 0,
text.colour = NULL,
text.color = NULL,
text.alpha = NULL,
parse = FALSE,
check_overlap = FALSE,
na.rm = FALSE,
Expand All @@ -1076,7 +1088,9 @@ geom_cols_isoline <- function(
geom = GeomIsoline, position = position, show.legend = show.legend,
inherit.aes = inherit.aes, params = list(isoline_text = isoline_text,
by = by, num = num, text_dodge = text_dodge,
parse = parse, check_overlap = check_overlap,
text.size = text.size, text.angle = text.angle,
text.colour = text.color %||% text.colour,
text.alpha = text.alpha, parse = parse, check_overlap = check_overlap,
na.rm = na.rm, ...))

}
Expand Down Expand Up @@ -1265,6 +1279,11 @@ geom_rows_vector <- function(
linejoin = "mitre",
vector_labels = TRUE,
...,
label.colour = NULL,
label.color = NULL,
label.alpha = NULL,
parse = FALSE,
check_overlap = FALSE,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
Expand All @@ -1273,6 +1292,8 @@ geom_rows_vector <- function(
geom = GeomVector, position = position, show.legend = show.legend,
inherit.aes = inherit.aes, params = list(arrow = arrow,
lineend = lineend, linejoin = linejoin, vector_labels = vector_labels,
label.colour = label.color %||% label.colour,
parse = parse, check_overlap = check_overlap,
na.rm = na.rm, ...))

}
Expand All @@ -1289,6 +1310,11 @@ geom_cols_vector <- function(
linejoin = "mitre",
vector_labels = TRUE,
...,
label.colour = NULL,
label.color = NULL,
label.alpha = NULL,
parse = FALSE,
check_overlap = FALSE,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
Expand All @@ -1297,6 +1323,8 @@ geom_cols_vector <- function(
geom = GeomVector, position = position, show.legend = show.legend,
inherit.aes = inherit.aes, params = list(arrow = arrow,
lineend = lineend, linejoin = linejoin, vector_labels = vector_labels,
label.colour = label.color %||% label.colour,
parse = parse, check_overlap = check_overlap,
na.rm = na.rm, ...))

}
Loading

0 comments on commit 32ce3a3

Please sign in to comment.