[R] plotting sorted factors
Rui Barradas
ruipbarradas at sapo.pt
Fri May 25 15:58:52 CEST 2012
Hello,
The problem is that the factors are not orderd by the row number. If you
want to check their order, use
str(sortdata)
and you'll see Santa-Rosa was attributed factor level 4 (in the output,
first variable, the 3rd and 4th).
Try the following.
sortdata <- read.table(text="
county year x1 x2 x3 x4 x5 x6 x7 rank
141 Escambia 2002 6.50 5.95 13.70 20.64 20.49 65.86 100.65 1
142 Escambia 1999 7.31 5.33 15.67 18.94 20.99 73.22 100.53 1
539 Santa-Rosa 2006 10.70 5.99 11.33 15.34 18.69 57.72 76.03 2
540 Santa-Rosa 2007 13.31 5.86 16.15 13.74 29.02 66.80 80.68 2
441 Okaloosa 2004 10.45 6.12 12.02 22.42 22.36 63.38 95.30 3
442 Okaloosa 2005 8.23 8.53 17.83 12.53 25.44 67.19 83.25 3
443 Okaloosa 2006 9.17 4.77 17.88 15.53 20.44 71.56 86.98 3
444 Okaloosa 2003 8.83 6.29 9.79 21.43 34.14 65.44 84.88 3
621 Walton 1999 3.64 11.84 5.21 22.87 23.70 43.46 71.26 4
622 Walton 2000 0.00 5.91 2.47 4.97 7.05 46.11 75.41 4
623 Walton 2001 5.89 6.18 4.61 13.75 20.19 49.38 46.44 4
271 Holmes 2001 12.62 5.18 14.89 15.28 9.10 66.61 74.57 5
272 Holmes 2002 0.00 3.88 5.03 14.53 23.90 64.91 122.85 5
636 Washington 2004 0.00 6.53 16.70 7.78 17.23 48.77 53.22 6
", header=TRUE, stringsAsFactors=FALSE)
str(sortdata) # county is character
sortdata$county <- with(sortdata, factor(county, levels=unique(county)))
str(sortdata) # county is factor, Santa-Rosa is 2
# it now works
plot(sortdata$county,sortdata$x1,col=(c(rainbow(70))))
# preferable? I think it's more intuitive.
boxplot(x1~county, data=sortdata, col=rainbow(70))
Hope this helps,
Rui Barradas
Em 22-05-2012; 23:17, "Peterso" escreveu:
> The "county" is a factor and I want to plot county vs variable x1 in
the order that the counties are listed now,
> which is in the order of the rank variable. But when plotting, county
is sorted in alphabetical order again. How can
> I plot the graph with the county in the rank order?
>
> the following command plots county in alphabetical order even though
the data frame sortdata is sorted by rank.
>
> plot(sortdata$county,sortdata$x1,col=(c(rainbow(70))))
>
>
> below is part of sortdata
>
> county year x1 x2 x3 x4 x5 x6 x7 rank
> 141 Escambia 2002 6.50 5.95 13.70 20.64 20.49 65.86 100.65 1
> 142 Escambia 1999 7.31 5.33 15.67 18.94 20.99 73.22 100.53 1
> 539 Santa-Rosa 2006 10.70 5.99 11.33 15.34 18.69 57.72 76.03 2
> 540 Santa-Rosa 2007 13.31 5.86 16.15 13.74 29.02 66.80 80.68 2
> 441 Okaloosa 2004 10.45 6.12 12.02 22.42 22.36 63.38 95.30 3
> 442 Okaloosa 2005 8.23 8.53 17.83 12.53 25.44 67.19 83.25 3
> 443 Okaloosa 2006 9.17 4.77 17.88 15.53 20.44 71.56 86.98 3
> 444 Okaloosa 2003 8.83 6.29 9.79 21.43 34.14 65.44 84.88 3
> 621 Walton 1999 3.64 11.84 5.21 22.87 23.70 43.46 71.26 4
> 622 Walton 2000 0.00 5.91 2.47 4.97 7.05 46.11 75.41 4
> 623 Walton 2001 5.89 6.18 4.61 13.75 20.19 49.38 46.44 4
> 271 Holmes 2001 12.62 5.18 14.89 15.28 9.10 66.61 74.57 5
> 272 Holmes 2002 0.00 3.88 5.03 14.53 23.90 64.91 122.85 5
> 636 Washington 2004 0.00 6.53 16.70 7.78 17.23 48.77 53.22 6
>
> Thanks,
> Johnny Liseth
> Graduate Student in Statistics
> University of West Florida
More information about the R-help
mailing list