Reputation: 11
I have a factor with levels Xa, aXa and aX. Since R treats the factors in alphabetical order, the default facet wraps are coming as aX, aXa and Xa. I want the Xa to be the first graph in the wrap. I tried the following code:
data_small<- read.csv("agg_cond_subj_123.csv")
data_small<-fct_relevel(data_small$pos, "Xa", "aXa", "aX")
And then fed it to ggplot:
data_small %>%
ggplot(aes(lg, prop))+
geom_boxplot()+
facet_wrap(~pos)+
labs(x="Language group",
y="Accuracy (%)")
Xa was still treated in the last order. I tried piping it directly through fct_reorder()
data_small %>%
fct_reorder(pos, "Xa", "aXa", "aX")
ggplot(aes(lg, prop))+
geom_boxplot()+
facet_wrap(~pos)+
labs(x="Language group",
y="Accuracy`enter code here` (%)")
but it gave an error: Error: f
must be a factor (or character vector).
I looked at some related solutions on the platform already but they were not fulfilling my purpose.
Upvotes: 0
Views: 2609
Reputation: 2609
Use functions that relevel the factor variable inside mutate
:
data_small %>%
mutate(pos = factor(pos, levels = c("Xa", "aXa", "aX"))) %>%
ggplot(aes(lg, prop))+
geom_boxplot()+
facet_wrap(~pos)+
labs(x="Language group",
y="Accuracy`enter code here` (%)")
Upvotes: 1