[R] more barplot presentation questions

Marc Schwartz MSchwartz at medanalytics.com
Wed Nov 5 04:25:38 CET 2003


On Tue, 2003-11-04 at 17:15, Paul Sorenson wrote:
> Thanks to those who pointed me at the solutions to the legend
> overprinting the bars.  I took the "easy" way of rescaling the y axis,
> picking the scaling factor for stacked bars is somewhat problematic
> but sufficient for my application.
> 
> I have another couple of barplot questions:
> 
> 	- Can I extend the major ticks on the Y axis across the page?  Or
> both axes to form a grid?

Yes....use par("tck"). The value used represents the proportion (0 - 1)
of the plotting region, that the tick marks will be, for the given axis
side .

Example:
# draw the barplot
barplot(1:10, axes = FALSE)

# draw the x axis with lines going vertical
axis(1, tck = 1)

# draw the y axis with lines going horizontal
axis(2, tck = 1)

# put a box around the whole thing
box()

In this case, the grid will overlay the bars. See ?par for more
information.

> 	- A really neat graph for me would be a combination of side-by-side
> and stacked bars in a single plot to display an additional category.
> 
> The background on the second problem is that I am displaying software
> defect metrics.  For each month (the bins) the categories of interest
> are:
> 	- new/fixed/closed
> 	- numeric severity (1 - 5)
> 
> I am currently displaying 5 separate graphs (6 when you take the
> aggregate into account) with new/fixed/closed side-by-side.  If within
> the side-by-side graphs I could show the severity stacked that would
> be very neat.
> 
> Cheers


This is just a bit more complex. If I understand what you are looking to
do, consider that a single barplot that has 39 five-segment stacked bars
(12 months x 3 status groups, plus 3 bars for the totals) is extremely
busy.  If you really, really wanted that, you could use the combination
of 'beside = FALSE' to get stacked bars and then use the 'space'
argument in barplot(), to separate groups of bars by setting 'space' to
a vector of values.

Here is a very brief example:

barplot(matrix(1:36, ncol = 12), beside = FALSE, space = c(1, 0, 0))

The above will have 4 groups of 3 bars each, where each bar will consist
of 3 colored segments. The 'space' argument starts with a spacing of 1
bar width before the first bar, 0 before the second and 0 before the
third, repeated for each group. The numbers are the proportion of the
average bar widths. See ?barplot for more information. If you want to
annotate the bars, keep in mind that barplot() returns the bar midpoints
(ie.  mp <- barplot(....) )

If you are looking to visually trend/compare groups, you may be best
served by staying with your multiple barplots, but do it within a single
graphic. This can be achieved by using par("mfrow") to break the single
display into multiple sections (ie. a grid of 2 rows by 3 columns) where
you can then use barplot() 6 times, once for each severity level. That
way, it is all on a single page, rather than 6 pages.

A quick example, though using repetitive barplot data just for layout
ideas:

# create some data
data <- matrix(1:36, ncol = 12)

# save current graphic pars
op <- par(no.readonly = TRUE)

# break up the graphic device into a 2 row by 3 column grid
par(mfrow = c(2, 3))

# now draw each barplot, one to each grid position
barplot(data, beside = FALSE)
barplot(data, beside = FALSE)
barplot(data, beside = FALSE)
barplot(data, beside = FALSE)
barplot(data, beside = FALSE)
barplot(data, beside = FALSE)

# restore graphic pars
par(op)

Again see ?par for more information.

In the above example, each grid position can represent one severity
level, with the final being your aggregate. Each individual stacked
barplot() represents your monthly data for each status level.

Alternatively, using barchart() in the grid/lattice package you can
achieve a similar result in a Trellis style graphic, where you can
'condition' the plots on the severity level. You might want to run the
barchart() examples to get a feel for what can be done there.

HTH,

Marc Schwartz




More information about the R-help mailing list