Re: [R] barplot as Trellis graphic

2008-03-28 Thread hadley wickham
On Fri, Mar 28, 2008 at 1:42 AM, Agustin Lobo <[EMAIL PROTECTED]> wrote:
> Thanks for your detailed explanation.
>  You are right, a set of boxplots done with bwplot
>  is a much better graphic for this type of data:
>
>  bwplot(V1~VAR|f,data=datos2)
>
>  This was not a good example. The barplot would be suited
>  for counts, ie. species composition:
>  datos4 <-
>  data.frame(V1=round(runif(200,1,5)),SITE=factor(round(runif(200,1,3

The bar plot in ggplot2 (http://had.co.nz/ggplot2) automatically
aggregates your data:

install.packages("ggplot2")
library(ggplot2)

qplot(factor(V1), data=datos4, geom="bar", facets = . ~ SITE)


> Is there an R guide to Trellis graphics?

How about http://www.amazon.com/dp/0387759689 ?

Hadley

-- 
http://had.co.nz/

__
R-help@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.


Re: [R] barplot as Trellis graphic

2008-03-28 Thread Charilaos Skiadas
On Mar 28, 2008, at 2:42 AM, Agustin Lobo wrote:

> Thanks for your detailed explanation.
> You are right, a set of boxplots done with bwplot
> is a much better graphic for this type of data:
>
> bwplot(V1~VAR|f,data=datos2)
>
> This was not a good example. The barplot would be suited
> for counts, ie. species composition:
> datos4 <- data.frame(V1=round(runif(200,1,5)),SITE=factor(round 
> (runif(200,1,3
>
> where I would like a barplot of table(V1) for each site:
> par(mfrow=c(3,1))
> barplot(table(datos4$V1[datos4$SITE==1]))
> barplot(table(datos4$V1[datos4$SITE==2]))
> barplot(table(datos4$V1[datos4$SITE==3]))
>
> I'll try with barchart!

Perhaps this:

datos5 <- with(datos4,aggregate(rep(1,nrow(datos4)), list 
(V1=V1,SITE=SITE),sum))
barchart(x~ordered(V1)|SITE, data=datos5)

> Is there an R guide to Trellis graphics?

I found Paul Murrell's book useful.

> Agus

Haris Skiadas
Department of Mathematics and Computer Science
Hanover College

> Charilaos Skiadas escribió:
>> On Mar 27, 2008, at 1:47 PM, Agustin Lobo wrote:
>>> Thanks, it was a matter of reshaping the data matrix as I usually  
>>> have
>>> it, ie:
>>> datos <-
>>> data.frame(x=abs(round(rnorm(100,10,5))),y=abs(round(rnorm 
>>> (100,2,1))),f=factor(round(runif(100,1,3
>>>
>>> to become:
>>>
>>> datos2 <-
>>> data.frame(V1=c(datos[,1],datos[,2]),"VAR"=c(rep("x",100),rep("y", 
>>> 100)),f=factor(c(datos[,3],datos[,3])))
>>>
>>> and then
>>> require(lattice)
>>> barchart(V1~VAR|f,data=datos2)
>>>
>>> I get horizontal lines in the bars that I do not understand, though.
>> In order to understand the lines , you should ask: What does the  
>> height of each bar correspond to? As you have set things up, the  
>> "x" bar in panel "1" should somehow correspond to the all values:
>> datos2$V1[datos2$VAR=="x" & datos2$f==1]
>> [1] 15 13 14  1 18 14  8 12  7 19 10  1  5 14  7  9 14  7  5 10  6  
>> 12 10 11 11  7 15
>> [28]  9  4 12 17 10  4  5
>> So you should ask yourself, how you expect R to produce a single  
>> column, which in some sense corresponds to just one single number,  
>> its height, from these different values. My guess is that you want  
>> R to show you just the mean on each group. For me this is not a  
>> barplot, but anyway. What happens in the barplot you have now, I  
>> think, is this that R will start by constructing a bar with height  
>> 15, then put on it a bar of height 13, then on it a bar of height  
>> 14 and so on. So the lines you see account for the boxes that  
>> survive:
>>  > x<-datos2$V1[datos2$VAR=="x" & datos2$f==1]
>>  > unique(cummax(rev(x)))
>> [1]  5 10 17 19
>> I would recommend using boxplots instead of "barplots only showing  
>> the means". If you really want barplots of the means, I think you  
>> can do the following:
>> datos3 <- with(datos2, aggregate(x=V1, by=list(VAR=VAR,f=f), mean))
>> barchart(x~VAR|f, datos3)
>> Another option would be ggplot2 I think, but I'll let someone  
>> knowledgeable with that package speak up.
>>> Agus
>>>
>> Haris Skiadas
>> Department of Mathematics and Computer Science
>> Hanover College
>
> -- 
> Dr. Agustin Lobo
> Institut de Ciencies de la Terra "Jaume Almera" (CSIC)
> LLuis Sole Sabaris s/n
> 08028 Barcelona
> Spain
> Tel. 34 934095410
> Fax. 34 934110012
> email: [EMAIL PROTECTED]
> http://www.ija.csic.es/gt/obster

__
R-help@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.


Re: [R] barplot as Trellis graphic

2008-03-28 Thread Agustin Lobo
Thanks for your detailed explanation.
You are right, a set of boxplots done with bwplot
is a much better graphic for this type of data:

bwplot(V1~VAR|f,data=datos2)

This was not a good example. The barplot would be suited
for counts, ie. species composition:
datos4 <- 
data.frame(V1=round(runif(200,1,5)),SITE=factor(round(runif(200,1,3

where I would like a barplot of table(V1) for each site:
par(mfrow=c(3,1))
barplot(table(datos4$V1[datos4$SITE==1]))
barplot(table(datos4$V1[datos4$SITE==2]))
barplot(table(datos4$V1[datos4$SITE==3]))

I'll try with barchart!

Is there an R guide to Trellis graphics?

Agus


Charilaos Skiadas escribió:
> 
> On Mar 27, 2008, at 1:47 PM, Agustin Lobo wrote:
> 
>> Thanks, it was a matter of reshaping the data matrix as I usually have
>> it, ie:
>> datos <-
>> data.frame(x=abs(round(rnorm(100,10,5))),y=abs(round(rnorm(100,2,1))),f=factor(round(runif(100,1,3
>>  
>>
>>
>> to become:
>>
>> datos2 <-
>> data.frame(V1=c(datos[,1],datos[,2]),"VAR"=c(rep("x",100),rep("y",100)),f=factor(c(datos[,3],datos[,3])))
>>  
>>
>>
>> and then
>> require(lattice)
>> barchart(V1~VAR|f,data=datos2)
>>
>> I get horizontal lines in the bars that I do not understand, though.
> 
> In order to understand the lines , you should ask: What does the height 
> of each bar correspond to? As you have set things up, the "x" bar in 
> panel "1" should somehow correspond to the all values:
> 
> datos2$V1[datos2$VAR=="x" & datos2$f==1]
> [1] 15 13 14  1 18 14  8 12  7 19 10  1  5 14  7  9 14  7  5 10  6 12 10 
> 11 11  7 15
> [28]  9  4 12 17 10  4  5
> 
> 
> So you should ask yourself, how you expect R to produce a single column, 
> which in some sense corresponds to just one single number, its height, 
> from these different values. My guess is that you want R to show you 
> just the mean on each group. For me this is not a barplot, but anyway. 
> What happens in the barplot you have now, I think, is this that R will 
> start by constructing a bar with height 15, then put on it a bar of 
> height 13, then on it a bar of height 14 and so on. So the lines you see 
> account for the boxes that survive:
> 
>  > x<-datos2$V1[datos2$VAR=="x" & datos2$f==1]
>  > unique(cummax(rev(x)))
> [1]  5 10 17 19
> 
> 
> I would recommend using boxplots instead of "barplots only showing the 
> means". If you really want barplots of the means, I think you can do the 
> following:
> 
> datos3 <- with(datos2, aggregate(x=V1, by=list(VAR=VAR,f=f), mean))
> barchart(x~VAR|f, datos3)
> 
> Another option would be ggplot2 I think, but I'll let someone 
> knowledgeable with that package speak up.
> 
>> Agus
>>
> 
> Haris Skiadas
> Department of Mathematics and Computer Science
> Hanover College
> 
> 
> 
> 
> 

-- 
Dr. Agustin Lobo
Institut de Ciencies de la Terra "Jaume Almera" (CSIC)
LLuis Sole Sabaris s/n
08028 Barcelona
Spain
Tel. 34 934095410
Fax. 34 934110012
email: [EMAIL PROTECTED]
http://www.ija.csic.es/gt/obster

__
R-help@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.


Re: [R] barplot as Trellis graphic

2008-03-27 Thread Charilaos Skiadas

On Mar 27, 2008, at 1:47 PM, Agustin Lobo wrote:

> Thanks, it was a matter of reshaping the data matrix as I usually have
> it, ie:
> datos <-
> data.frame(x=abs(round(rnorm(100,10,5))),y=abs(round(rnorm 
> (100,2,1))),f=factor(round(runif(100,1,3
>
> to become:
>
> datos2 <-
> data.frame(V1=c(datos[,1],datos[,2]),"VAR"=c(rep("x",100),rep("y", 
> 100)),f=factor(c(datos[,3],datos[,3])))
>
> and then
> require(lattice)
> barchart(V1~VAR|f,data=datos2)
>
> I get horizontal lines in the bars that I do not understand, though.

In order to understand the lines , you should ask: What does the  
height of each bar correspond to? As you have set things up, the "x"  
bar in panel "1" should somehow correspond to the all values:

datos2$V1[datos2$VAR=="x" & datos2$f==1]
[1] 15 13 14  1 18 14  8 12  7 19 10  1  5 14  7  9 14  7  5 10  6 12  
10 11 11  7 15
[28]  9  4 12 17 10  4  5


So you should ask yourself, how you expect R to produce a single  
column, which in some sense corresponds to just one single number,  
its height, from these different values. My guess is that you want R  
to show you just the mean on each group. For me this is not a  
barplot, but anyway. What happens in the barplot you have now, I  
think, is this that R will start by constructing a bar with height  
15, then put on it a bar of height 13, then on it a bar of height 14  
and so on. So the lines you see account for the boxes that survive:

 > x<-datos2$V1[datos2$VAR=="x" & datos2$f==1]
 > unique(cummax(rev(x)))
[1]  5 10 17 19


I would recommend using boxplots instead of "barplots only showing  
the means". If you really want barplots of the means, I think you can  
do the following:

datos3 <- with(datos2, aggregate(x=V1, by=list(VAR=VAR,f=f), mean))
barchart(x~VAR|f, datos3)

Another option would be ggplot2 I think, but I'll let someone  
knowledgeable with that package speak up.

> Agus
>

Haris Skiadas
Department of Mathematics and Computer Science
Hanover College

__
R-help@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.


Re: [R] barplot as Trellis graphic

2008-03-27 Thread Agustin Lobo
Thanks, it was a matter of reshaping the data matrix as I usually have 
it, ie:
datos <- 
data.frame(x=abs(round(rnorm(100,10,5))),y=abs(round(rnorm(100,2,1))),f=factor(round(runif(100,1,3

to become:

datos2 <- 
data.frame(V1=c(datos[,1],datos[,2]),"VAR"=c(rep("x",100),rep("y",100)),f=factor(c(datos[,3],datos[,3])))

and then
require(lattice)
barchart(V1~VAR|f,data=datos2)

I get horizontal lines in the bars that I do not understand, though.

Agus




Deepayan Sarkar escribió:
> On 3/26/08, Agustin Lobo <[EMAIL PROTECTED]> wrote:
>> Dear list,
>>
>>  Is there any way of making barplots as a Trellis graphic?
> 
> Yes, the function to use is 'barchart'.
> 
> -Deepayan
> 

-- 
Dr. Agustin Lobo
Institut de Ciencies de la Terra "Jaume Almera" (CSIC)
LLuis Sole Sabaris s/n
08028 Barcelona
Spain
Tel. 34 934095410
Fax. 34 934110012
email: [EMAIL PROTECTED]
http://www.ija.csic.es/gt/obster

__
R-help@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.


Re: [R] barplot as Trellis graphic

2008-03-27 Thread Deepayan Sarkar
On 3/26/08, Agustin Lobo <[EMAIL PROTECTED]> wrote:
> Dear list,
>
>  Is there any way of making barplots as a Trellis graphic?

Yes, the function to use is 'barchart'.

-Deepayan

__
R-help@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.


[R] barplot as Trellis graphic (corrected)

2008-03-26 Thread Agustin Lobo
In the code of my previous message
the barplot should be:
barplot(apply(sel[,-1],2,mean))
instead of
barplot(sel)

Sorry for the confusion.

Agus

 Mensaje original 
Asunto: barplot as Trellis graphic
Fecha: Wed, 26 Mar 2008 22:24:04 +0100
De: Agustin Lobo <[EMAIL PROTECTED]>
Responder a: [EMAIL PROTECTED]
Para: r-help@r-project.org

Dear list,

Is there any way of making barplots as a Trellis graphic?
Currently I do something like:

class <- unique(mydata[,1])
par(mfrow=c(3,3))
for(i in class){
  sel <- mydata[mydata[,1]==i,]
  barplot(sel)
  title(i)
}

where class would take values between 1 and 9
and the first column of mydata would be the class.

But I'm looking for a nicer code and a nicer graphic
using the approach of the lattice package.

Thanks!

-- 
Dr. Agustin Lobo
Institut de Ciencies de la Terra "Jaume Almera" (CSIC)
LLuis Sole Sabaris s/n
08028 Barcelona
Spain
Tel. 34 934095410
Fax. 34 934110012
email: [EMAIL PROTECTED]
http://www.ija.csic.es/gt/obster


-- 
Dr. Agustin Lobo
Institut de Ciencies de la Terra "Jaume Almera" (CSIC)
LLuis Sole Sabaris s/n
08028 Barcelona
Spain
Tel. 34 934095410
Fax. 34 934110012
email: [EMAIL PROTECTED]
http://www.ija.csic.es/gt/obster


-- 
Dr. Agustin Lobo
Institut de Ciencies de la Terra "Jaume Almera" (CSIC)
LLuis Sole Sabaris s/n
08028 Barcelona
Spain
Tel. 34 934095410
Fax. 34 934110012
email: [EMAIL PROTECTED]
http://www.ija.csic.es/gt/obster

__
R-help@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.


[R] barplot as Trellis graphic

2008-03-26 Thread Agustin Lobo
Dear list,

Is there any way of making barplots as a Trellis graphic?
Currently I do something like:

class <- unique(mydata[,1])
par(mfrow=c(3,3))
for(i in class){
  sel <- mydata[mydata[,1]==i,]
  barplot(sel)
  title(i)
}

where class would take values between 1 and 9
and the first column of mydata would be the class.

But I'm looking for a nicer code and a nicer graphic
using the approach of the lattice package.

Thanks!

-- 
Dr. Agustin Lobo
Institut de Ciencies de la Terra "Jaume Almera" (CSIC)
LLuis Sole Sabaris s/n
08028 Barcelona
Spain
Tel. 34 934095410
Fax. 34 934110012
email: [EMAIL PROTECTED]
http://www.ija.csic.es/gt/obster

__
R-help@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.