[R] axis placement with stacked barplots and the asp=1 parameter

Marc Schwartz MSchwartz at MedAnalytics.com
Fri Jan 21 15:37:09 CET 2005


On Fri, 2005-01-21 at 12:09 +0000, Kavithan Siva wrote:
> Hi
> 
> Here's some example code which illustrates the issue:
> 
> dataSeries<-array(0, c(5, 2))
> 
> dataSeries[1, 1] <- 5
> dataSeries[2, 1] <- 5
> dataSeries[3, 1] <- 0
> dataSeries[4, 1] <- 2
> dataSeries[5, 1] <- 0
> 
> dataSeries[1, 2] <- 7
> dataSeries[2, 2] <- 0
> dataSeries[3, 2] <- 0
> dataSeries[4, 2] <- 0
> dataSeries[5, 2] <- 1
> 
> barplot(dataSeries, asp=1)
> 
> Removing the asp=1 parameter produces a correct looking plot. I need to
> use the asp=1 parameter because I was re-writing the barplot function to
> create stacked charts with rounded corners (instead of rectangles). For
> the arc at the corners of the bars to look correct I need asp=1. I've
> attached a .ps file to show you what I was trying to achieve.
> 
> Thanks
> Kav

There are a couple of options to consider here. First, note that using
the 'asp' argument forces the x and y axes to be scaled such that one
unit of measurement in each direction is the same. As you are seeing,
this impacts the look of curves and of course would be relevant to
certain plots where the horizontal and vertical measures need to be
"square".

One option is to leave the plot as is, but "move" the y axis closer to
the bars. Using your same data above:

# Draw the plot, but no axes
barplot(dataSeries, asp=1, axes= FALSE)

# Now using the 'line' argument, move the axis closer
# to the bars. Negative values move the axis to the right.
axis(2, line = -14)


Another option, which results in something closer to the barplot when
not using the 'asp' argument, is to adjust the 'width' argument to
increase the horizontal size of the bars:

# Draw the barplot and increase the width of the bars
# Do not draw the axes
barplot(dataSeries, asp=1, width = 8, axes = FALSE)

# Now draw the y axis
axis(2, at = seq(0, 12, 2))

Note however, that in this example, the vertical dimension is
"shortened", since the 'asp' argument is still trying to properly set
the aspect ratio and the larger bar width increases the range of the x
axis.


Finally(?), another option to consider is to set par(pty = "s") as an
alternative to using 'asp = 1'. This results in a square plot region,
rather than a maximal (typically rectangular) one. See ?par for more
info here. In this case:

par(pty = "s")
barplot(dataSeries)

This might result in better looking arcs for your corners.

Without having your modified barplot() function, it is hard to know
which might work best here, but hopefully this might provide some
possibilities.

HTH,

Marc Schwartz




More information about the R-help mailing list