Understanding how “reorder” in R works

2019-06-07 20:26发布

问题:

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?

回答1:

If you look at v$V3 you get the following (print) output:

[1] AA AB BA
attr(,"scores")
AA AB BA 
12  1  2 
Levels: AB BA AA

In the lowest line, you find the specified order of levels.

This infomation could be obtained more easily with the levels function:

levels(v$V3)
[1] "AB" "BA" "AA"

The function reorder does not change the values in the vector (or their order) but sets (changes) the levels and the scores attribute to indicate the order of factor levels:

attributes(v$V3)
$levels
[1] "AB" "BA" "AA"

$class
[1] "factor"

$scores
AA AB BA 
12  1  2 


标签: r ggplot2