Skip to content
Prev 6433 / 398498 Next

Combining lines and barplot

On Sat, Jul 08, 2000 at 02:14:35PM -0400, Dirk Eddelbuettel wrote:
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
the shaft.

	# 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 ...

	lines(x, y)

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

	Ross
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Message-ID: <20000709110523.B12289@stat1.stat.auckland.ac.nz>
In-Reply-To: <14695.28555.50863.751307@sonny.eddelbuettel.com>; from Dirk Eddelbuettel on Sat, Jul 08, 2000 at 02:14:35PM -0400