[R] RE : add arrows to barchart with groups

From: GOUACHE David <D.GOUACHE_at_arvalisinstitutduvegetal.fr>
Date: Wed, 02 May 2007 16:36:46 +0200


Hello and thank you for this first response

I'll rephrase what I mean by "more general": in a case where I have only 2 levels in groups, but in which I have more than 2 levels for variable s, and in a case where the second bar is not necessarily higher than the first. Thanks in advance.
Best regards,

David Gouache
Arvalis - Institut du Végétal

-----Message d'origine-----
De : Deepayan Sarkar [mailto:deepayan.sarkar_at_gmail.com] Envoyé : vendredi 27 avril 2007 19:52
À : GOUACHE David
Cc : r-help_at_stat.math.ethz.ch
Objet : Re: [R] add arrows to barchart with groups

On 4/27/07, GOUACHE David <D.GOUACHE_at_arvalisinstitutduvegetal.fr> wrote:
> Hello Rhelpers,
>
> I am trying to represent the following data (hereafter named donnees) in a barchart with a grouping variable :
>
>
> site traitement date s res
> 1 NT 17/10/2005 normal 76.2
> 1 T 17/10/2005 normal 103.2
> 1 NT 23/11/2005 tardif 81.6
> 1 T 23/11/2005 tardif 98
> 2 NT 15/10/2005 normal 72.71
> 2 T 15/10/2005 normal 94.47
> 2 NT 15/11/2005 tardif 79.65
> 2 T 15/11/2005 tardif 94.7
>
> barchart(res~s|site,groups=traitement,data=donnees)
>
> What I'd like to do is for each site represent with an arrow the difference in value of variable res between normal and tardif values of variable s.
> I've found one way of doing it:
>
>
> trellis.focus("panel",1,1)
> xx<-trellis.panelArgs()$x
> yy<-trellis.panelArgs()$y
> panel.arrows(as.numeric(xx)[c(1,3)]-0.1,yy[c(1,3)],as.numeric(xx)[c(1,3)]-0.1,yy[c(2,4)],lwd=2,code=3)
> panel.text(as.numeric(xx)[c(1,3)]-0.35,c(87,87),paste(yy[c(2,4)]-yy[c(1,3)],"\nq/ha"),font=2)
> trellis.focus("panel",2,1)
> xx<-trellis.panelArgs()$x
> yy<-trellis.panelArgs()$y
> panel.arrows(as.numeric(xx)[c(1,3)]-0.1,yy[c(1,3)],as.numeric(xx)[c(1,3)]-0.1,yy[c(2,4)],lwd=2,code=3)
> panel.text(as.numeric(xx)[c(1,3)]-0.35,c(87,87),paste(yy[c(2,4)]-yy[c(1,3)],"\nq/ha"),font=2)
> trellis.unfocus()
>
> But I would prefer doing this within a custom panel function so I can apply it more generally, and I haven't been able to figure out how...
> Could anyone give me a hand?

The obvious analog (just copy/pasting your code) is:

my.panel <- function(x, y, ...)
{

    panel.barchart(x, y, ...)
    xx <- x
    yy <- y
    panel.arrows(as.numeric(xx)[c(1,3)]-0.1, yy[c(1,3)],

                 as.numeric(xx)[c(1,3)]-0.1, yy[c(2,4)],
                 lwd = 2, code = 3)
    panel.text(as.numeric(xx)[c(1,3)] - 0.35, c(87,87),
               paste(yy[c(2,4)] - yy[c(1,3)], "\nq/ha"),
               font=2)

}

and this seems to work:

barchart(res~s|site,groups=traitement,data=donnees,

         panel = my.panel)

barchart(res~s|site,groups=traitement,data=donnees,

         panel = my.panel,
         origin = 0)

I'm not sure what else you are looking for and what you mean by "more general". For example, it's not clear what you want to happen If you have more than 2 levels in 'groups', or if the second bar is not always higher than the first.

-Deepayan



R-help_at_stat.math.ethz.ch 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. Received on Wed 02 May 2007 - 14:54:35 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 02 May 2007 - 15:01:21 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.