在“一次性”过滤基础数据帧之后,如何将变量的值分配给ggplot标题。
library(tidyverse)
#THIS WORKS
d <- mtcars %>%
filter(carb==4)
d %>%
ggplot()+
labs(title=paste(unique(d$carb)))+
geom_bar(aes(x=am,
fill=gear),
stat="count")
#THIS DOESN'T WORK
mtcars %>%
filter(carb==4) %>%
ggplot()+
labs(title=paste(data=. %>% distinct(carb) %>% pull()))+
geom_bar(aes(x=am,
fill=gear),
stat="count")
#> Error in as.vector(x, "character"): cannot coerce type 'closure' to vector of type 'character'
#THIS ALSO DOESN'T WORK
mtcars %>%
filter(carb==3) %>%
ggplot()+
labs(title=paste(.$carb))+
geom_bar(aes(x=am,
fill=gear),
stat="count")
#> Error in paste(.$carb): object '.' not found
于2020年4月23日由
reprex package
(v0.3.0)