José Santos Alegria

2003-Feb-03 12:31 UTC

### [R] Overlaying a moving average curve on top of a barplot

I''m using standard barplot (Windows version 1.6.2 of R) to represent a certain weekly metric "v" and I would like to properly overlay on top of it its moving average "mean.8" (window of 8 weeks). I must be doing something wrong since the moving average (using "lines") doesn''t overlay properly, i.e., both x-scales do not match! ... barplot(v[8:length(v)], col=7) lines(mean.8[1:length(mean.8)], lty=1, lwd=2, col=2) ... How do I make sure that both graphics are in synch as far as the x-scale and y-scales are concerned? Thanks, José A. S. Alegria [[alternate HTML version deleted]]

Morten Sickel

2003-Feb-03 12:45 UTC

### [R] Overlaying a moving average curve on top of a barplot

Jose Santos Alegria wrote:>I''m using standard barplot (Windows version 1.6.2 of R) to represent acertain weekly>metric "v" and I would like to properly overlay on top of it its movingaverage "mean.8">(window of 8 weeks). I must be doing something wrong since the movingaverage (using >"lines") doesn''t overlay properly, i.e., both x-scales do not match! Have you considered using "filter"? I made a somehov similiar plot this way: (prec being a data frame with ''columns'' date with dates of measurements and prec, precipitation at the actual date) <code> plot(prec) lines(prec$date,filter(prec$value,c(0.25,0.25,0.25,0.25))) </code> Points showing actual measurements and a four periods moving average as a line. Hope this helps. Morten -- Morten Sickel Norwegian Radiation Protection Authority http://www.nrpa.no

José Santos Alegria

2003-Feb-03 13:43 UTC

### [R] Overlaying a moving average curve on top of a barplot

My problem is not with "plot()" but with "barplot()"! I guess it may have to do with the fact that the barplot''s bars have a non-negligible width and the moving average line not! Is it? Jos? A. S. Alegria -----Original Message----- From: Morten Sickel [mailto:Morten.Sickel at nrpa.no] Sent: segunda-feira, 3 de Fevereiro de 2003 11:43 AM To: Jos? Santos Alegria; R help (E-post) Subject: RE: [R] Overlaying a moving average curve on top of a barplot Jose Santos Alegria wrote:>I''m using standard barplot (Windows version 1.6.2 of R) to represent acertain weekly>metric "v" and I would like to properly overlay on top of it its movingaverage "mean.8">(window of 8 weeks). I must be doing something wrong since the movingaverage (using >"lines") doesn''t overlay properly, i.e., both x-scales do not match! Have you considered using "filter"? I made a somehov similiar plot this way: (prec being a data frame with ''columns'' date with dates of measurements and prec, precipitation at the actual date) <code> plot(prec) lines(prec$date,filter(prec$value,c(0.25,0.25,0.25,0.25))) </code> Points showing actual measurements and a four periods moving average as a line. Hope this helps. Morten -- Morten Sickel Norwegian Radiation Protection Authority http://www.nrpa.no

Marc Schwartz

2003-Feb-03 17:19 UTC

### [R] Overlaying a moving average curve on top of a barplot

>-----Original Message----- >From: r-help-admin at stat.math.ethz.ch >[mailto:r-help-admin at stat.math.ethz.ch] On Behalf Of Jos? >Santos Alegria >Sent: Monday, February 03, 2003 5:30 AM >To: r-help at stat.math.ethz.ch >Subject: [R] Overlaying a moving average curve on top of a barplot > > >I''m using standard barplot (Windows version 1.6.2 of R) to >represent a certain weekly metric "v" and I would like to >properly overlay on top of it its moving average "mean.8" >(window of 8 weeks). I must be doing something wrong since the >moving average (using "lines") doesn''t overlay properly, i.e., >both x-scales do not match! > >... >barplot(v[8:length(v)], col=7) >lines(mean.8[1:length(mean.8)], lty=1, lwd=2, col=2) >... > >How do I make sure that both graphics are in synch as far as >the x-scale and y-scales are concerned? > >Thanks, > >Jos? A. S. AlegriaThe problem that you are having is that you need to get the bar midpoints from barplot() in order to use those values as the x coordinate values for lines(). barplot() returns the bar midpoints as an invisible vector (or matrix where appropriate). Modify your code to: mp <- barplot(v[8:length(v)], col=7) lines(mp, mean.8[1:length(mean.8)], lty=1, lwd=2, col=2) This puts the bar midpoints (x axis values) in mp, which you can then use for lines(). HTH, Marc

José Santos Alegria

2003-Feb-03 17:33 UTC

### [R] Overlaying a moving average curve on top of a barplot

It makes sense and it works! Thanks and best regards, Jos? A. S. Alegria -----Original Message----- From: Marc Schwartz [mailto:MSchwartz at MedAnalytics.com] Sent: segunda-feira, 3 de Fevereiro de 2003 4:18 PM To: Jos? Santos Alegria; r-help at stat.math.ethz.ch Subject: RE: [R] Overlaying a moving average curve on top of a barplot>-----Original Message----- >From: r-help-admin at stat.math.ethz.ch >[mailto:r-help-admin at stat.math.ethz.ch] On Behalf Of Jos? >Santos Alegria >Sent: Monday, February 03, 2003 5:30 AM >To: r-help at stat.math.ethz.ch >Subject: [R] Overlaying a moving average curve on top of a barplot > > >I''m using standard barplot (Windows version 1.6.2 of R) to >represent a certain weekly metric "v" and I would like to >properly overlay on top of it its moving average "mean.8" >(window of 8 weeks). I must be doing something wrong since the >moving average (using "lines") doesn''t overlay properly, i.e., >both x-scales do not match! > >... >barplot(v[8:length(v)], col=7) >lines(mean.8[1:length(mean.8)], lty=1, lwd=2, col=2) >... > >How do I make sure that both graphics are in synch as far as >the x-scale and y-scales are concerned? > >Thanks, > >Jos? A. S. AlegriaThe problem that you are having is that you need to get the bar midpoints from barplot() in order to use those values as the x coordinate values for lines(). barplot() returns the bar midpoints as an invisible vector (or matrix where appropriate). Modify your code to: mp <- barplot(v[8:length(v)], col=7) lines(mp, mean.8[1:length(mean.8)], lty=1, lwd=2, col=2) This puts the bar midpoints (x axis values) in mp, which you can then use for lines(). HTH, Marc