[R] barchart with bars attached to y=0-line

Deepayan Sarkar deepayan.sarkar at gmail.com
Wed Jul 16 18:48:35 CEST 2008


On 7/16/08, Henning Wildhagen <HWildhagen at gmx.de> wrote:
> Dear R users,
>
>  i am using the following code to produce barcharts with lattice:
>
>  Compound<-c("Glutamine", "Arginine", "Glutamate", "Glycine", "Serine",
>  "Glucose", "Fructose", "Raffinose",
>  "Glycerol", "Galacglycerol", "Threitol", "Galactinol", "Galactitol")
>
>
>  Class<-c("aminos","aminos","aminos","aminos","aminos","sugars","sugars","sugars","glycerols","glycerols","sugar
>  alcohols","sugar alcohols","sugar alcohols")
>
>  set.seed(5)
>  Ratio<-rnorm(13, 0.5, 3)
>
>  df<-data.frame(Compound, Class,Ratio)
>
>  library(lattice)
>
>  P<-barchart(data=df,Ratio~Compound|Class)
>
>  However, I would like to have the bars attached to an imaginary y=0-line so
>  that they go up if Ratio>0 and down if Ratio<0.
>  I saw some older entries in the mailing list supposing to use
>  panel.barchart. However, I did not fully understand the usage of this
>  function. Also, it was annouced to add an option to "barchart" to make it
>  easier to get this type of plot.
>
>  Has anyone an idea what the easiest solution might be?

barchart(data=df,Ratio~Compound|Class, origin = 0)

>  A second problem is concerning the display of the "Compound"-objects in
>  accordance with the conditioning variable "Class". In other words: Is it
>  possible to use the whole space of each panel for only those
>  "Compound"-objects which belong to the "Class" displayed in that particular
>  panel? Of course, for this purpose the panels have to be "detached" to
>  allow appropiate labling of the x-axis.

barchart(data=df,Ratio~Compound|Class, origin = 0,
         scales = list(x = "free"))

But this would work better if the levels of Compound are adjacent
within Class; e.g.

barchart(data=df,Ratio~reorder(Compound, as.numeric(Class))|Class,
          origin = 0, scales = list(x = "free"))

-Deepayan



More information about the R-help mailing list