[R] barplot & plot together

Peter Dalgaard p.dalgaard at biostat.ku.dk
Thu Dec 18 11:21:00 CET 2003


"juli g. pausas" <juli at ceam.es> writes:

> Dear colleges,
> I'm trying to combine a barplot and a plot in a single figure as follows:
> 
> data <- 1:6
> t <- barplot(data, axes=F)
> par(new= T)
> plot(t, data, type="b")
> 
> However, as you can see in the example, the dots of the second plot do
> not fall in the midpoint of the bars in the first. Any trick for
> setting the 2 plots at the same scale?
> I have unsuccessfully tried:
> plot(t, data, type="b", xlim=c(0,7))
> plot(t, data, type="b", xlim=c(min(t),max(t)))
> 
> (R 1.8.1, for Windows)

The canonical trick for getting two plots on the same scale is to set
xlim (and ylim) on *both*. On barplots, this gets a bit tricky since
you have to leave room for the column width (the actual calculation
can be read inside barplot.default). However, I'd try for something like

 t <- barplot(data,names=1:6,ylim=range(c(0,data*1.01)))
 points(t, data, type="b")


-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list