Hi-
I am looking to re-order a categorical variable. I have the dataframe ordered the way that I want,
Aminoacid_data <- read_csv("~/Desktop/Aminoacid_data.csv")
in this dataset, there is a categorical variable called "Species" and I viewed the labels in the correct order here:
Aminoacid_data$Species
[1] "At" "At" "At" "At" "At" "At" "At" "At" "At" "At" "At" "At" "At" "At" "At" "At" "At" "At" "Ns" "Ns" "Ns"
[22] "Ns" "Ns" "Ns" "Ns" "Ns" "Ns" "Ns" "Ns" "Ns" "Ns" "Ns" "Ns" "Ns" "Ns" "Ns" "Mc" "Mc" "Mc" "Mc" "Mc" "Mc"
[43] "Mc" "Mc" "Mc" "Mc" "Mc" "Mc" "Mc" "Mc" "Mc" "Mc" "Mc" "Mc" "Lj" "Lj" "Lj" "Lj" "Lj" "Lj" "Lj" "Lj" "Lj"
[64] "Lj" "Lj" "Lj" "Lj" "Lj" "Lj" "Lj" "Lj" "Lj" "Ps" "Ps" "Ps" "Ps" "Ps" "Ps" "Ps" "Ps" "Ps" "Ps" "Ps" "Ps"
[85] "Ps" "Ps" "Ps" "Ps" "Ps" "Ps"
OK the levels go in the order of At, Ns, Mc, Lj, Ps
So I next make a summary of the mean and SD of each group so that I can plot them, using this code:
AA_summary <- Aminoacid_data %>%
group_by(Species, Genotype, Timepoint) %>%
summarise(mean_Asn=mean(Asn), mean_Ile=mean(Ile), mean_Total=mean(Total_amino_acids), sd_Asn=sd(Asn), sd_Ile=sd(Ile), sd_Total=sd(Total_amino_acids))
I then check the order of the levels:
AA_summary$Species
AA_summary$Species
[1] At At At At At At Lj Lj Lj Lj Lj Lj Mc Mc Mc Mc Mc Mc Ns Ns Ns Ns Ns Ns Ps Ps Ps Ps Ps Ps
THE ORDER OF THE LEVELS CHANGED GRRRR
So I tried re-ordering the levels:
AA_summary$Species <- factor(AA_summary$Species, levels=c("At", "Ns", "Mc", "Lj", "Ps"))
and I checked the order again but still got the output above, with the levels in this order:
At, Lj, Mc, Ns, Ps
It looks like the levels are in alphabetical order..... is there some way to turn this off??
Thanks,
Erik