Description Usage Arguments Value See Also Examples
Factors, Redux
1 |
x |
A vector of data. |
levels |
An optional character vector to be used as the levels. If
specified, |
logic |
The logical principle by which levels of |
... |
Additional arguments for |
A factor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | set.seed(1)
x <- sample(letters, size = 20, replace = TRUE)
if (requireNamespace("ggplot2")) {
library(ggplot2)
## refactor on-the-fly to get ggplot2 barplot sorted by frequency
ggplot(data.frame(x = refactor(x)), aes(x)) + geom_bar()
## or create the factor explicitly
xfact <- refactor(x)
ggplot(data.frame(x = xfact), aes(x)) + geom_bar()
## set levels in order of appearance in the data
xfact_appear <- refactor(x, logic = "appear")
ggplot(data.frame(x = xfact_appear), aes(x)) + geom_bar()
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.