[R] Bar plot between two different liniar models

Marc Schwartz marc_schwartz at comcast.net
Tue Jan 13 19:02:04 CET 2009


on 01/13/2009 08:43 AM joe1985 wrote:
> Hello
> 
> I have a problem that i ant make a Bar plot like the one i have tried to
> illustrate below (made in paint); 
> http://www.nabble.com/file/p21437080/LG5%2Bgraf%2Bredigeret.jpg 
> 
> http://www.nabble.com/file/p21437080/LG5%2Bgraf%2Bredigeret.JPG
> LG5+graf+redigeret.JPG 
> 
> Where each line represents a model;
> 
> model1 = 0.58*x+12.65
> model2 = 1.16*x+12.65
> 
> But i only want the bars and with y-values above and below the bars (the
> y-value from model1 below and the y-value from model2 above). 
> 
> Does anyone know if thats possible? 


library(fortunes)

> fortune("Yoda")

Evelyn Hall: I would like to know how (if) I can extract some of the
information from the summary of my nlme.
Simon Blomberg: This is R. There is no if. Only how.
   -- Evelyn Hall and Simon 'Yoda' Blomberg
      R-help (April 2005)



Something like this should get you in the right direction:


x <- -3:1

y1 <- 0.58 * x + 12.65
y2 <- 1.16 * x + 12.65



As shown in the online images:

matplot(x, cbind(y1, y2), type = "l", ylim = c(8, 15),
        lty = c("solid", "dashed"), col = "black")

segments(x, y1, x, y2, lwd = 6)

text(x[-4], y1[-4], label = round(y1[-4], 1), pos = c(3, 3, 3, 1))

text(x[-4], y2[-4], label = round(y2[-4], 1), pos = c(1, 1, 1, 3))




If you only want the bars, you could use:

plot(x, y1, type = "n", ylim = c(8, 15))

segments(x, y1, x, y2, lwd = 6)

text(x[-4], y1[-4], label = round(y1[-4], 1), pos = c(3, 3, 3, 1))

text(x[-4], y2[-4], label = round(y2[-4], 1), pos = c(1, 1, 1, 3))



I am not clear on how you want to label or not label the point at x = 0,
based upon the online images.

See ?matplot, ?segments, ?par and ?text

Also, note par("lend") in ?par, which can adjust the shape of the ends
of the line segments, if you do not want them rounded. However, if you
go 'square' for example, you may not get a plotted point a x = 0, in
which case you can add it in with points().

Also, if you have the actual model objects in R, you can use abline()
for plotting the fitted lines, fitted() to get the model fitted Y values
and predict() to get fitted Y values at specific X values. See ?abline,
?fitted and ?predict.lm for more information.

HTH,

Marc Schwartz




More information about the R-help mailing list