Skip to content

Plotting Factors -- Sorting x-axis

3 messages · Taylor, Eric HLS:EX, David Winsemius, Bill Venables

#
On Jan 6, 2011, at 6:13 PM, Taylor, Eric HLS:EX wrote:

            
Since they are most likely factor variables (but even if not):

dfrm$Months <- factor(dfrm$Months, levels= month.abb)

Then the level ordering will be as expected.

David Winsemius, MD
West Hartford, CT
#
That rather depends on what kind of plot you want to use.

Here is one option that you can use without any changes:

######
con <- textConnection("
   Months Prec
1     Jan   102.1
2     Feb    69.7
3     Mar    44.7
4     Apr    32.1
5     May    24.0
6     Jun    18.7
7     Jul    14.0
8     Aug    20.0
9     Sep    32.4
10    Oct    58.9
11    Nov    94.5
12    Dec   108.2
")
tab <- read.table(con)
closeAllConnections()
rm(con)

with(tab, barplot(Prec, names = as.character(Months)))

#####
## If you want to adjust the factor so that the levels remain in natural months order, then
#####

tab <- within(tab, 
	Months <- factor(Months, levels = month.abb))

with(tab, barplot(Prec, names = levels(Months)))

######