If you're using the dev version of ggplot2 you may want to take a peek at the release notes, as it addresses changes in aes() mapping that I think are relevant here:
Looking at the traceback for your function, it seems the issue is in the line
ggplot(date = . , aes(x = reorder(x, -Freq), y = Freq, fill = x))
library(rlang)
library(tidyverse)
qw <- structure(list(weekday = structure(c(2L, 6L, 7L, 5L, 1L, 3L,
4L, 2L, 6L, 7L, 5L, 1L, 3L, 4L, 2L, 6L, 7L, 5L, 1L, 3L, 4L, 2L,
6L, 7L, 5L, 1L, 3L, 4L, 2L, 6L, 7L, 5L, 1L, 3L, 4L), .Label = c("Friday",
"Monday", "Saturday", "Sunday", "Thursday", "Tuesday", "Wednesday"
), class = "factor"), Target = c(0, 0, 1, 1, 0, 1, 1, 1, 1, 0,
0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0,
1, 0, 0, 1), type = structure(c(3L, 3L, 2L, 3L, 1L, 3L, 1L, 3L,
1L, 1L, 3L, 2L, 1L, 3L, 3L, 1L, 3L, 1L, 2L, 2L, 2L, 2L, 1L, 1L,
1L, 3L, 2L, 1L, 2L, 1L, 2L, 1L, 3L, 2L, 3L), .Label = c("Advertising",
"Agriculture", "Bank"), class = "factor")), .Names = c("weekday",
"Target", "type"), row.names = c(NA, -35L), class = "data.frame")
cateby_label_graph <- function(x){
x <- syms(x)
qw %>%
group_by(!!!x, Target) %>%
summarise(Freq = n()) %>%
ggplot(data = . , aes(x = reorder(x, -Freq), y = Freq, fill = x)) +
geom_bar(stat = 'identity') +
labs(y = "", x = "") +
facet_grid(Target~., scales="free") +
theme(legend.position = 'none')
}
cateby_label_graph('type')
#> Error in unique.default(x, nmax = nmax): unique() applies only to vectors
Created on 2018-07-02 by the reprex package (v0.2.0).
The traceback for the error reads:
19: unique.default(x, nmax = nmax)
18: unique(x, nmax = nmax)
17: factor(x)
16: FUN(X[[i]], ...)
15: lapply(INDEX, as.factor)
14: tapply(X = X, INDEX = x, FUN = FUN, ...)
13: reorder.default(x, -Freq)
12: reorder(x, -Freq)
11: FUN(X[[i]], ...)
10: lapply(aesthetics[new_aesthetics], rlang::eval_tidy, data = data)
9: scales_add_defaults(plot$scales, data, aesthetics, plot$plot_env)
8: f(..., self = self)
7: l$compute_aesthetics(d, plot)
6: f(l = layers[[i]], d = data[[i]])
5: by_layer(function(l, d) l$compute_aesthetics(d, plot))
4: ggplot_build.ggplot(x)
3: ggplot_build(x)
2: print.ggplot(x)
1: function (x, ...)
UseMethod("print")(x)
So, the error might be that you're, in effect, applying unique() to a factor.