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

雨燕双飞 提交于 2019-12-17 20:36:20

问题


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!


回答1:


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))


来源:https://stackoverflow.com/questions/18634431/r-in-barplot-midpoints-are-not-centered-w-r-t-bars

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!