在 ggplot2 中使用命名向量以手动比例设置颜色时,如果使用变量作为名称之一,颜色将不会出现在最终绘图中。
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)
但是,如果我使用名称创建命名向量,则该图将按预期显示:
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)
谁能向我解释这种行为?我怀疑它与非标准评估有关(ggplot2 使用的是名称而不是值?)。
建议在手动比例设置功能中使用变量的最佳方法,将不胜感激!