[R] Barchart in lattice package: controlling order of bars in plot and color of a selected bar

Matthew Pettis matthew.pettis at gmail.com
Fri Jan 16 22:19:39 CET 2009


Hi,

I'm using the lattice function 'barchart' to make a series of 4
histograms.  Currently, the y-axis values are graphed in order of the
y-axis variable.  I'd like to have the y-axis values sorted in
ascending order of the x-axis values so that the longest bar
horizontally is on top of the graph (in it's seciton) and the shortest
bar is on the bottom.  I can do this in 'barplot' with normal
graphics; how can I do this in the lattice barchart function?  In
addition, i use the 'col' parameter in 'barplot' to make one
particular value be a different color than the rest.  Is this sort of
control available in lattice's barchart?  I didn't see it when reading
the documentation.

Thanks,
Matt

### Regular barplot code that works
# w02-08 ar pre-sorted in descending "Sum" oder,
# "barcolor" column has names of colors to use.

opar <- par(mfrow=c(1,4))
barplot(w02[,"Sum"], horiz=T, col=w02[,"barcolor"],
names.arg=w02[,"sts.dist"], main="2002")
barplot(w04[,"Sum"], horiz=T, col=w04[,"barcolor"],
names.arg=w04[,"sts.dist"], main="2004")
barplot(w06[,"Sum"], horiz=T, col=w06[,"barcolor"],
names.arg=w06[,"sts.dist"], main="2006")
barplot(w08[,"Sum"], horiz=T, col=w08[,"barcolor"],
names.arg=w08[,"sts.dist"], main="2008")

### Attempt with lattice
# work contains w02-08 stacked on top of each other

barchart(sts.dist[order(Sum),] ~ Sum | year, data=work, layout=c(4,1),
main="Ranking of best turnout by SD")




More information about the R-help mailing list