最简单的方式是调整标签函数,只提取第一个变量的标签。你可以通过以下方式实现:
mpg %>%
filter(manufacturer=='audi')%>%
ggplot(aes(cty, hwy)) +
geom_point(aes(col = model)) +
facet_wrap(~year+model, labeller=function(x) {x[1]})
mpg %>%
filter(manufacturer=='audi')%>%
ggplot(aes(cty, hwy)) +
geom_point(aes(col = model)) +
facet_wrap(~interaction(year,model), labeller=as_labeller(function(x) gsub("\\..*$", "", x)))
另一种选择是定义自定义标签器函数。我在“标签器”文档中发现关于输入和输出格式的解释有些令人困惑。因此,希望这个简单的示例可以帮助其他人。
library(tidyverse)
mpg %>%
filter(manufacturer=='audi')%>%
ggplot(aes(cty, hwy)) +
geom_point(aes(col = model)) +
facet_wrap(year~model,
labeller = function(df) {
list(as.character(df[,1]))
})