[R] Superimpose ksmooth() onto barplot

Greg Snow Greg.Snow at imail.org
Wed Feb 10 19:08:02 CET 2010


One option is to use the updateusr function in the TeachingDemos package, the help page has examples of adding lines to barplots.

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Nathan S. Watson-Haigh
> Sent: Tuesday, February 09, 2010 4:44 PM
> To: r-help at r-project.org
> Subject: [R] Superimpose ksmooth() onto barplot
> 
> I'd like to superimpose a ksmooth() onto a barplot().
> 
> My data is:
>  > d
>                  2009-06-20 2009-06-21 2009-06-22 2009-06-23 2009-06-24
> 2009-06-25 2009-06-26 2009-06-27 2009-06-28 2009-06-29 2009-06-30
> 2009-07-01 2009-07-02
> Same Breed (B)       12.64      21.08      13.52      12.51
> 13.71       9.91      14.24       7.18      11.81       5.92
> 12.04      17.96       2.99
> Different Breed       3.70       3.43       5.43       4.43
> 3.69       1.83       2.95       2.93       4.44       3.47
> 9.90      10.63       2.92
>                  2009-07-04 2009-07-05 2009-07-06 2009-07-07 2009-07-08
> 2009-07-09 2009-07-10 2009-07-11 2009-07-12 2009-07-13 2009-07-14
> 2009-07-15 2009-07-16
> Same Breed (B)       27.81      26.79      24.93      23.39
> 27.52      28.24      12.60       2.25       1.20       0.51
> 0.18       1.31       0.40
> Different Breed      12.16       4.53       8.51      11.77
> 5.65      11.67       5.81       0.53       1.26       0.42
> 1.10       0.20       0.29
>                  2009-07-17 2009-07-18 2009-07-19 2009-07-21 2009-07-22
> 2009-07-23 2009-07-24 2009-07-25 2009-07-26 2009-07-27 2009-07-28
> 2009-07-29 2009-10-29
> Same Breed (B)        0.24       0.03       0.05      13.34
> 7.70       9.50       9.12       8.62      10.60       7.46
> 13.17      15.55       3.17
> Different Breed       0.13       0.01       0.05       4.98
> 2.75       4.38       2.17       3.51       1.97       2.68
> 1.60       2.47       0.38
>                  2009-10-30 2009-10-31 2009-11-01 2009-11-02 2009-11-03
> 2009-11-04 2009-11-05 2009-11-06 2009-11-07 2009-11-08 2009-11-09
> 2009-11-24 2009-11-25
> Same Breed (B)        1.39       3.57       3.53       1.30
> 2.26       3.41       3.44       4.04       1.71       2.16
> 5.01       4.34       3.58
> Different Breed       0.51       0.85       1.49       1.99
> 6.83       1.91       1.54       1.44       0.60       2.08
> 1.55       2.13       1.44
>                  2009-11-26 2009-11-27 2009-11-28 2009-11-29 2009-11-30
> Same Breed (B)        2.12       2.34       1.90       4.99       2.11
> Different Breed       4.21       1.00       2.28       2.90       1.68
> 
> I thought the following might work ok, but the x-axis doesn't quite
> line
> up correctly for the lines because of the spacing between the bars. I
> could set the space to zero, but does anyone have any thoughts on how
> to
> maintain the spacing and "stretch" the lines data?
> barplot(as.matrix(d), beside=FALSE, las=2,
> col=as.numeric(as.factor(rownames(d))))
> lines(ksmooth(1:ncol(d), colSums(d), kernel="normal",
> bandwidth=bandwidth), type="l", lwd=2, col="blue")
> lines(ksmooth(1:ncol(d), d[1,], kernel="normal", bandwidth=bandwidth),
> type="l", lwd=2, col=as.numeric(as.factor(rownames(d)))[1])
> lines(ksmooth(1:ncol(d), d[2,], kernel="normal", bandwidth=bandwidth),
> type="l", lwd=2, col=as.numeric(as.factor(rownames(d)))[2])
> legend("topright", c(rownames(d), "Total"),
> fill=c(as.numeric(as.factor(rownames(d))), "blue"))
> 
> Cheers,
> Nathan
> 
> --
> --------------------------------------------------------
> Dr. Nathan S. Watson-Haigh
> OCE Post Doctoral Fellow
> CSIRO Livestock Industries
> University Drive
> Townsville, QLD 4810
> Australia
> 
> Tel: +61 (0)7 4753 8548
> Fax: +61 (0)7 4753 8600
> Web: http://www.csiro.au/people/Nathan.Watson-Haigh.html
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list