Reputation: 33
I am attempting to create a rotated bar-chart with years on the y-axis. Certain years have a value (i.e., a bar), the rest are zero.
For some reason, when I add a y-axis with the years, there is a mis-match between the bar and the year the value reflects. I'd be grateful if someone could explain why this occurs and how to get around it.
Here is a subset of my data:
x <- c(0, 0, 0, 0, 0, 0, 0, 1.82, 0, 0, 0, 0, 0, 1.04, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.28, 0, 0, 0, 0, 0, 0, 0, 0, 1.43, 1.55, 0, 0, 0, 0, 0, 0, 0, 1.22, 1.14, 2.05, 0, 0, 0, 0, 0)
y <- 1860:1909
barplot(x, horiz=TRUE, axes=F)
axis(2, at=seq(1, 50, 2), labels=seq(1860, 1909, 2), las=2)
The first value occurs in 1867, but appears at 1868 on the chart. I believe the data are fine because x[8] returns 1.82 and y[8] returns 1867, which is correct. The mis-match amplifies through the series; indeed the last three bars plot above the y-axis.
Perhaps I have missed something fundamental... many thanks in advance.
Upvotes: 2
Views: 50
Reputation: 206232
barplot()
doesn't plot the bars on integer values. It returns the values that it used as a matrix and then you can use that to label the axes
bp <- barplot(x, horiz=TRUE, axes=F)
axis(2, at=bp[,1][seq(1,50, by=2)], labels=seq(1860, 1909, 2), las=2)
Upvotes: 2