Skip to content
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

Convert linetype NAs to blank lines #6270

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# ggplot2 (development version)

* `linetype = NA` is now interpreted to mean 'no line' instead of raising errors
(@teunbrand, #6269).
* New parameters for `geom_label()` (@teunbrand and @steveharoz, #5365):
* The `linewidth` aesthetic is now applied and replaces the `label.size`
argument.
Expand Down
4 changes: 0 additions & 4 deletions R/legend-draw.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
15 changes: 10 additions & 5 deletions R/scale-linetype.R
Original file line number Diff line number Diff line change
Expand Up @@ -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].
#'
Expand All @@ -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,
...
)
}
Expand Down
3 changes: 3 additions & 0 deletions R/utilities-grid.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
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

Check warning on line 45 in R/utilities-grid.R

View check run for this annotation

Codecov / codecov/patch

R/utilities-grid.R#L45

Added line #L45 was not covered by tests
}

inject(gpar(!!!args))
}
Expand Down
Binary file added man/figures/linetype_table.pdf
Binary file not shown.
63 changes: 63 additions & 0 deletions man/figures/linetype_table.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 10 additions & 15 deletions man/scale_linetype.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading