Given the following data in /tmp/thefile.csv
(a CSV file):
AA,12
AB,1
BA,2
and the following R
code:
v = read.csv('/tmp/thefile.csv', header=F)
library('ggplot2')
v$V3 = reorder(v$V1, v$V2)
ggplot(v, aes(x=v$V3, y=v$V2), stat='identity') + geom_bar(fill='dark grey', stat="identity")
The plot shows the bars in ascending order, which is what I want. Without the reorder
line, the plot appears in alphabetical order according to the factor.
However:
> v
V1 V2 V3
1 AA 12 AA
2 AB 1 AB
3 BA 2 BA
shows that the order is still alphabetical. How is ggplot
aware of the numerical order?
If you look at
v$V3
you get the following (print
) output:In the lowest line, you find the specified order of levels.
This infomation could be obtained more easily with the
levels
function:The function
reorder
does not change the values in the vector (or their order) but sets (changes) thelevels
and thescores
attribute to indicate the order of factor levels: