diff --git a/NEWS.md b/NEWS.md index d2a96008c4..3e5a66d4e9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -330,6 +330,8 @@ * (internal) The ViewScale class has a `make_fixed_copy()` method to permit copying trained position scales (#3441). * Improved consistency of curve direction in `geom_curve()` (@teunbrand, #5069) +* `linetype = NA` is now interpreted to mean 'no line' instead of raising errors + (@teunbrand, #6269). # ggplot2 3.5.1 diff --git a/R/legend-draw.R b/R/legend-draw.R index 9a1b607a2e..6e0996886f 100644 --- a/R/legend-draw.R +++ b/R/legend-draw.R @@ -199,8 +199,6 @@ draw_key_crossbar <- function(data, params, size) { draw_key_path <- function(data, params, size) { if (is.null(data$linetype)) { data$linetype <- 0 - } else { - data$linetype[is.na(data$linetype)] <- 0 } grob <- segmentsGrob(0.1, 0.5, 0.9, 0.5, gp = gg_par( @@ -388,8 +386,6 @@ draw_key_vline <- function(data, params, size) { draw_key_timeseries <- function(data, params, size) { if (is.null(data$linetype)) { data$linetype <- 0 - } else { - data$linetype[is.na(data$linetype)] <- 0 } grid::linesGrob( diff --git a/R/scale-linetype.R b/R/scale-linetype.R index a53f660c72..f3d48aa4c5 100644 --- a/R/scale-linetype.R +++ b/R/scale-linetype.R @@ -7,8 +7,15 @@ #' #' @inheritParams discrete_scale #' @inheritDotParams discrete_scale -expand -position -na.value -scale_name -palette -#' @param na.value The linetype to use for `NA` values. #' @rdname scale_linetype +#' @details +#' Lines can be referred to by number, name or hex code. Contrary to base R +#' graphics, `NA`s are interpreted as blanks. +#' +#' \if{html}{\figure{linetype_table.svg}{Named linetypes by number and name}} +#' \if{latex}{\figure{linetype_table.pdf}} +#' +#' #' @seealso #' The documentation for [differentiation related aesthetics][aes_linetype_size_shape]. #' @@ -35,22 +42,20 @@ #' scale_linetype_identity() + #' facet_grid(linetype ~ .) + #' theme_void(20) -scale_linetype <- function(name = waiver(), ..., na.value = NA, aesthetics = "linetype") { +scale_linetype <- function(name = waiver(), ..., aesthetics = "linetype") { discrete_scale( aesthetics, name = name, palette = NULL, - na.value = na.value, ... ) } #' @rdname scale_linetype #' @export -scale_linetype_binned <- function(name = waiver(), ..., na.value = NA, aesthetics = "linetype") { +scale_linetype_binned <- function(name = waiver(), ..., aesthetics = "linetype") { binned_scale( aesthetics, name = name, palette = NULL, - na.value = na.value, ... ) } diff --git a/R/utilities-grid.R b/R/utilities-grid.R index c231f0b279..a935d5b38f 100644 --- a/R/utilities-grid.R +++ b/R/utilities-grid.R @@ -41,6 +41,9 @@ gg_par <- function(..., stroke = NULL, pointsize = NULL) { stroke[is.na(stroke)] <- 0 args$fontsize <- pointsize * .pt + stroke * .stroke / 2 } + if (!is.null(args$lty) && anyNA(args$lty)) { + args$lty[is.na(args$lty)] <- if (is.character(args$lty)) "blank" else 0 + } inject(gpar(!!!args)) } diff --git a/man/figures/linetype_table.pdf b/man/figures/linetype_table.pdf new file mode 100644 index 0000000000..29710c0883 Binary files /dev/null and b/man/figures/linetype_table.pdf differ diff --git a/man/figures/linetype_table.svg b/man/figures/linetype_table.svg new file mode 100644 index 0000000000..9ea0a7ea82 --- /dev/null +++ b/man/figures/linetype_table.svg @@ -0,0 +1,63 @@ + + + + + + + + + + + + + + + + + + + +"blank"/NA +"solid" +"dashed" +"dotted" +"dotdash" +"longdash" +"twodash" +0/NA +1 +2 +3 +4 +5 +6 +"44" +"13" +"1343" +"73" +"2262" + + + + + + + + +Number +Name +Hex code +Display + + diff --git a/man/scale_linetype.Rd b/man/scale_linetype.Rd index d0b2fb9349..5c6e9a691f 100644 --- a/man/scale_linetype.Rd +++ b/man/scale_linetype.Rd @@ -7,23 +7,13 @@ \alias{scale_linetype_discrete} \title{Scale for line patterns} \usage{ -scale_linetype(name = waiver(), ..., na.value = NA, aesthetics = "linetype") +scale_linetype(name = waiver(), ..., aesthetics = "linetype") -scale_linetype_binned( - name = waiver(), - ..., - na.value = NA, - aesthetics = "linetype" -) +scale_linetype_binned(name = waiver(), ..., aesthetics = "linetype") scale_linetype_continuous(...) -scale_linetype_discrete( - name = waiver(), - ..., - na.value = NA, - aesthetics = "linetype" -) +scale_linetype_discrete(name = waiver(), ..., aesthetics = "linetype") } \arguments{ \item{name}{The name of the scale. Used as the axis or legend title. If @@ -89,8 +79,6 @@ notation. \item{\code{super}}{The super class to use for the constructed scale} }} -\item{na.value}{The linetype to use for \code{NA} values.} - \item{aesthetics}{The names of the aesthetics that this scale works with.} } \description{ @@ -99,6 +87,13 @@ University of Manchester. Continuous values can not be mapped to line types unless \code{scale_linetype_binned()} is used. Still, as linetypes has no inherent order, this use is not advised. } +\details{ +Lines can be referred to by number, name or hex code. Contrary to base R +graphics, \code{NA}s are interpreted as blanks. + +\if{html}{\figure{linetype_table.svg}{Named linetypes by number and name}} +\if{latex}{\figure{linetype_table.pdf}} +} \examples{ base <- ggplot(economics_long, aes(date, value01)) base + geom_line(aes(group = variable))