Mhairi McNeill Mhairi McNeill - 1 month ago 14
R Question

Can someone explain the behaviour of named vectors in manual ggplot scales?

When using a named vector to set colours in a manual scale in ggplot2, if you use a variable as one of the names, the colour will not appear in the final plot.

library(ggplot2)

first_species <- 'setosa'
colours <- c(first_species = 'black',
'versicolor' = 'cadetblue',
'virginica' = 'hotpink')

ggplot(iris) +
aes(x = Petal.Length, y = Petal.Width, colour = Species) +
geom_point() +
scale_colour_manual(values = colours)


Missing a colour

However, if I create the named vector using names, the plot appears as expected:

colours <- c('black', 'cadetblue', 'hotpink')
names(colours) <- c(first_species, 'versicolor', 'virginica')

ggplot(iris) +
aes(x = Petal.Length, y = Petal.Width, colour = Species) +
geom_point() +
scale_colour_manual(values = colours)


Correct plot

Can anyone explain this behaviour to me? I suspect it is related to non-standard evaluation (ggplot2 is using the name rather than the value?).

Suggestions the best way to use a variable in the manual scale setting functions, would be appreciated!

Answer

When you do something like:

c(n=1)

Its like doing:

runif(n=10)

The n is a name here, and isn't evaluated like it was a variable. Its a named argument. That you can quote names like runif("n"=10) is a feature of the parser that lets you put otherwise unparsable names as arguments:

list("this has spaces" = 99) # works, list(this has spaces=99) fails