Jonhnny Weslley
Jonhnny Weslley

Reputation: 1080

Using lattice to plot histograms of sorted categorical data

Consider the following data file:

column_name
foo
foo
bar
baz
baz
bar
foo
baz

How I can plot a histogram with using the lattice package, and sorting by a custom order (not alphabetic), e.g.: c('baz', 'foo', 'bar')?

Upvotes: 1

Views: 2625

Answers (1)

IRTFM
IRTFM

Reputation: 263411

(histogram( y ~ x | factor(column_name, levels=c('baz', 'foo', 'bar') ) )

Or perhaps:

(histogram( y ~  factor(column_name, levels=c('baz', 'foo', 'bar') ) )

Or even better put everything in a dataframe and then do:

dfrm$column_name <-  factor(dfrm$column_name, levels=c('baz', 'foo', 'bar') ) )
histogram( y ~ column_name, data=dfrm )

(Lattice functions generally expect to have the main data arguments come from a dataframe.)

Upvotes: 4

Related Questions