[R] Combining lines and barplot
ihaka at stat.auckland.ac.nz
Sun Jul 9 01:05:23 CEST 2000
On Sat, Jul 08, 2000 at 02:14:35PM -0400, Dirk Eddelbuettel wrote:
> With a data frame containing daily data, I would like to overlay a barplot of
> daily outcomes with the daily high and low. However, when use the
> barplot(X$"day_end", col="gray", ylim=c(0,120))
> lines(X$"day_high", col="blue")
> lines(X$"day_low", col="blue")
> the lines do not correspond to the same (daily) x-axis values. How can I
> make them do that?
There are quite a few serious problems with the basic graphics displays
in R. Some of these are inherited from S and some are of our own making.
(I am painfully away of these defficiencies as I am trying to teach a
course on graphics using R).
In the case of barplot, it doesn't do the intuitive thing (sorry).
Rather than centering the bars (or groups of bars) at x = 1, 2, 3,
..., it puts them at some interesting numbers of its own choosing.
The center points of bars are returned as the values of the function
and you could use these to add the lines you want to the graph.
Here is an example with fake data.
y <- sin(1:10/5)
err <- (1 + runif(10))/10
We'll use the values in "y" for the barplot and put lines +/- "err"
above and below.
x <- barplot(y, , col = "lightgray",
ylim = range(y + err, y - err, 0))
lines(x, y + err)
lines(x, y - err)
However, it's not clear that this is a truly suitable graph for this kind
of data. There are many other possibilities. Here is an "error bar"
approach - using arrows with their head portions drawn at 90 degrees to
# Some x ordinates
x <- 1:10
# Here pch = 20, cex = 2 gives a big black dot
plot(x, y, pch = 20, cex = 2,
ylim = range(y + err, y - err, 0),
xlab = "Suitable X Label",
ylab = "Suitable Y Label")
# Join up the dots ...
# Error-ish bars
arrows(x, y - err, 1:10, y + err,
angle = 90, length = .1, code = 3)
(I make no claims that this is a good graph, it's just a meant to
show that other possibilities are easy).
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
More information about the R-help