Skip to content

Wrong error message with NA name in scale_linetype_manual, inconsistent with scale_color_manual #2206

@jsams

Description

@jsams

When NAs are included as a name with the scale_linetype_manual, an incorrect error occurs:

Error in grid.Call.graphics(C_lines, x$x, x$y, index, x$arrow) :
  invalid hex digit in 'color' or 'lty'

The error says that the linetype or color given is invalid even when all colors and linetypes are in fact valid. The problem is with an NA in the names of the value vector given to the scale_linetype_manual function.

However, the same vector of names with colors (with color identifiers rather than linetype identifiers) does not produce an error (although the line does not get drawn either).

It seems as if an NA value is given a specific linetype, then the line should be drawn. Even if you disagree, the error message given should point the user at the correct problem, an NA in the names, and not send them down a rabbit hole trying to diagnose a non-problem.

Here is some code that demonstrates both the inconsistent behavior and the error

library(ggplot2)
library(data.table)
library(RColorBrewer)


# Setup
line_types = c('solid', 'dotted', 'longdash')
color_group = 'Paired' # 'Dark2'...
IS_POOL = c(NA_character_, letters) # pool of names to label lines
IPTS = 6 # how many lines to plot
IS = IS_POOL[1:IPTS]
DENPTS = 20 # the number of points used to draw the density
BUG_FIX = F

if (BUG_FIX) {
    IS[is.na(IS)] = "NA"
}

dt = data.table(index=rep(1:IPTS, each=DENPTS),
                 x=rep(seq(-4,4, length.out=DENPTS), times=IPTS))
dt[, i := IS[index]]
setkey(dt, i, x)
dt[, `:=`(mean=rnorm(1), var=runif(1, 0.5, 2)), by=.(i)]
dt[, density := dnorm(x, mean, var)]


# set up line and color sequences
colors = brewer.pal(ceiling(length(IS) / length(line_types)), color_group)
colorrep = rep(colors, each=length(line_types))[1:length(IS)]
names(colorrep) = IS
linerep = rep(line_types, times=length(colors))[1:length(IS)]
names(linerep) = IS

# this works fine
plt = ggplot(dt) + geom_line(aes(x=x, y=density, color=i, linetype=i))
plt

#  this works fine, despite the NA in colorrep
plt = plt + scale_color_manual(values=colorrep)
plt

# this fails with, even though linerep and colorrep names are setup identically
# Error in grid.Call.graphics(C_lines, x$x, x$y, index, x$arrow) :
#   invalid hex digit in 'color' or 'lty'
plt + scale_linetype_manual(values=linerep)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behaviorscales 🐍

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions