见this existing question for validating color。
对于形状,您可以使用未导出的 ggplot 函数来验证形状名称
ggplot2:::translate_shape_string(4) # ok
ggplot2:::translate_shape_string("cross") # ok
ggplot2:::translate_shape_string("oops") # bad
ggplot2:::translate_shape_string(30) # bad
您可以查看它是否引发错误。但是因为这是一个未导出的函数,所以不能保证它在未来版本的 ggplot2 中工作或维护,所以使用风险自负。
或者在 ggplot specs vignette vignette("ggplot2-specs", package="ggplot2") 中有代码似乎给出了所有可能值的列表。您可以根据该列表检查潜在的字符串值。
shape_names <- c(
"circle", paste("circle", c("open", "filled", "cross", "plus", "small")), "bullet",
"square", paste("square", c("open", "filled", "cross", "plus", "triangle")),
"diamond", paste("diamond", c("open", "filled", "plus")),
"triangle", paste("triangle", c("open", "filled", "square")),
paste("triangle down", c("open", "filled")),
"plus", "cross", "asterisk"
)