Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: in barplot midpoints are not centered w.r.t. bars

I have just noticed something strange using barplot in R. Let y be the vector

> y
[1] 24924006 15310556 11638412  9542834  8696133

Using barplot on y I arrive at the vector of midpoints

bp <- barplot(y)

Plotting both bars and midpoints I notice that the bars are not centered .w.r.t. the midpoints...and this is odd; in summary, I use

bp <- barplot(y)
points(bp)

with

as outcome. Could you please help me solving this little puzzle? I would just have bars with centered mid-points. Thanks!

like image 578
Avitus Avatar asked Sep 05 '13 10:09

Avitus


1 Answers

If you save the barplot() result as an object you get the midpoints for the bars.

bp <- barplot(y)
bp
     [,1]
[1,]  0.7
[2,]  1.9
[3,]  3.1
[4,]  4.3
[5,]  5.5

If you use them now in other plotting functions those midpoints should be as x values. In call plot(bp) they are used as y values and x values are sequence numbers 1,2,3,4,5 - so they do not correspond to midpoints.

Providing also y values, points are plotted as expected.

bp <- barplot(y)
points(bp,c(10,20,30,40,50))
like image 169
Didzis Elferts Avatar answered Sep 17 '22 20:09

Didzis Elferts