Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
503 views
in Technique[技术] by (71.8m points)

vector - 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...