Skip to content

Alignment of lines within barplot bars

5 messages · snowboarder101, Greg Snow

#
Hi,

I am dealing with a similar issue, I am trying to use a barplot in
conjunction with some points plotted and want to line the two up. This all
works fine, but I am dealing with the additional complication that I have
two y-ranges. The barplot is based on one range, while the points are based
on another. The way I have been dealing with this is to use barplot(), and
then use a par(new=T), and then do the plot. So I have axis(2) with the
points, and axis(4) with the barplot values. However, the points don't line
up. When I do it the other way and do barplot(), points() with the barplot()
x coordinates, the range isn't correct, and not all points appear. 

I can force barplot() to use my range of points, but then the bars are too
small. I can also try to normalize my points to be in the same range as the
barplot() data, but then I have issues labeling it appropriately. That is,
my axis(2) is incorrect, and I need this whole thing to function dynamically
in a function, so I can't explicitly list the labels.

Any thoughts on this would be appreciated. 

--
View this message in context: http://r.789695.n4.nabble.com/Alignment-of-lines-within-barplot-bars-tp2533115p3434209.html
Sent from the R help mailing list archive at Nabble.com.
#
Look at the updateusr function in the TeachingDemos package.
#
Thanks for the reply. I looked at this before as I saw you had posted this,
but this either doesn't work in my situation, or I am using it wrong. 

Take the example in the function:

tmp <- barplot(1:4)
updateusr(tmp[1:2], 0:1, 1:2, 0:1)
lines(1:4, c(1,3,2,2), lwd=3, type='b',col='red')

But I want to use par(new=T) and plot(), so:

tmp <- barplot(1:4)
par(new=T)
updateusr(tmp[1:2], 0:1, 1:2, 0:1)
plot(1:4, c(1,3,2,2), lwd=3, type='b',col='red')

The reason I do this is that the y-axis then corresponds to my plot() data.
In my specific case, it's more like this:

tmp <- barplot(c(1.5,40),yaxt='n',names.arg=1:2,ylim=c(0,1.25*40))
axis(4)
par(new=T)
updateusr(tmp[1:2], 1:2)
plot(1:2,c(0.01,0.02),xaxt='n',pch=16)

I want those points in the middle of the bars... but I want to keep the
y-axis labels from the second plot. the par(new=T) probably wipes out the
coordinates, so I'm not sure how use this? 

Thanks Again. 


--
View this message in context: http://r.789695.n4.nabble.com/Alignment-of-lines-within-barplot-bars-tp2533115p3437369.html
Sent from the R help mailing list archive at Nabble.com.
9 days later
#
Sorry I took so long to get back on this, I have been out of town.

For your case you need to provide adjustments for both the x and y to updateusr, try this:

library(TeachingDemos)
tmp <- barplot(c(1.5,40),yaxt='n',names.arg=1:2,ylim=c(0,1.25*40))
axis(4)
tmp2 <- par('usr')
updateusr(tmp[1:2], tmp2[3:4], 1:2, c(0,0.022) )
points( 1:2, c(0.01, 0.02), pch=16 )
axis(2)


You can make further adjustments to the new y axis by changing the 0.022 in the updateusr call.