Re: [R] Help: Side-by-side named barplot bars

2007-10-18 Thread hadley wickham
On 10/18/07, Jim Lemon <[EMAIL PROTECTED]> wrote:
> Sergey Goriatchev wrote:
> > Hello,
> >
> > Let me describe what I have and what I want to do:
> >
> > I have two (7x6) matrices (call them A and B) that have same row and
> > column names (rows=species, columns=variables) but contain numerical
> > values that differ (same experiment done twice and values calculated
> > and put in different matrices).
> > ...
> > But this is not very effective. What I want to do is to combine the
> > two similar barplots for each experiment into one. So, for each
> > species I create just one barplot where I plot 6 clusters each
> > consisting of 2 bars, one bar for same varibale but different
> > experiment. I want to put the values as text at the top of each bar
> > and below each cluster I want to put the name of the variable (they
> > are the same for each experiment, remember).
> >
> Hi Sergey,
> See if this does what you want:
>
> library(plotrix)
> mat1<-matrix(rnorm(42,3),nrow=7)
> mat2<-matrix(rnorm(42,3),nrow=7)
> colnames(mat1)<-colnames(mat2)<-
>   c("First","Second","Third","Fourth","Fifth","Sixth")
> rownames(mat1)<-rownames(mat2)<-
>   c("Fextus decrepitans","Gryphon laxus",
>   "Domus flaccidus","Cogitans prostrata","Osmia putrescens",
>   "Aria excruciata","Pestis ubiquitus")

I'm a strong believer in always storing data in data.frames, so the
first thing I'd do with your data is put it in that form.  This also
has the nice side effect of making the data self-documenting.  I'd
process Jim's example data as follows:

library(reshape)

df1 <- melt(mat1)
df2 <- melt(mat2)

names(df1) <- names(df2) <- c("species", "variable", "value")
df1$expt <- "one"
df2$expt <- "two"

df <- rbind(df1, df2)

It's then easy to plot the data with lattice or ggplot2.  The examples
below use ggplot2.

library(ggplot2)

qplot(variable, value, data=df, geom="bar", stat="identity",
fill=expt, position="dodge", facets = . ~ species)

Another option would be to use a dot plot instead - this has a much
higher data-ink ratio, and is arguably more correct when the values
don't represent sums.

qplot(variable, value, data=df, colour=expt, facets = . ~ species)

You could also connect the two experiments with a line:

qplot(variable, value, data=df, colour=expt, facets = . ~ species) +
geom_line(aes(group=variable), colour="grey")

If you really want the labels (and I wouldn't recommend it - it's a
graph not a table), you can add the following line to either of the
above calls:

+ geom_text(aes(label = formatC(value, digits=2), vjust = 0))

If you want the axes flipped (so it's easier to read the text labels), add:

+ coord_flip()

Regards,

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] Help: Side-by-side named barplot bars

2007-10-18 Thread Jim Lemon
Sergey Goriatchev wrote:
> Hello,
> 
> Let me describe what I have and what I want to do:
> 
> I have two (7x6) matrices (call them A and B) that have same row and
> column names (rows=species, columns=variables) but contain numerical
> values that differ (same experiment done twice and values calculated
> and put in different matrices).
> ...
> But this is not very effective. What I want to do is to combine the
> two similar barplots for each experiment into one. So, for each
> species I create just one barplot where I plot 6 clusters each
> consisting of 2 bars, one bar for same varibale but different
> experiment. I want to put the values as text at the top of each bar
> and below each cluster I want to put the name of the variable (they
> are the same for each experiment, remember).
> 
Hi Sergey,
See if this does what you want:

library(plotrix)
mat1<-matrix(rnorm(42,3),nrow=7)
mat2<-matrix(rnorm(42,3),nrow=7)
colnames(mat1)<-colnames(mat2)<-
  c("First","Second","Third","Fourth","Fifth","Sixth")
rownames(mat1)<-rownames(mat2)<-
  c("Fextus decrepitans","Gryphon laxus",
  "Domus flaccidus","Cogitans prostrata","Osmia putrescens",
  "Aria excruciata","Pestis ubiquitus")
maintitle<-paste("Plot of",rownames(mat1)[1])
barp(rbind(mat1[1,],mat2[1,]),names.arg=colnames(mat1),
  col=c(2,3),main=maintitle,ylim=c(0,6))
text(1:6-0.2,mat1[1,]+0.3,round(mat1[1,],2),srt=90)
text(1:6+0.2,mat2[1,]+0.3,round(mat2[1,],2),srt=90)
...

Jim

__
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] Help: Side-by-side named barplot bars

2007-10-18 Thread Sergey Goriatchev
Hello,

Let me describe what I have and what I want to do:

I have two (7x6) matrices (call them A and B) that have same row and
column names (rows=species, columns=variables) but contain numerical
values that differ (same experiment done twice and values calculated
and put in different matrices).

Up to now I was producing two set of barplots, each set for each
matrix of values.
On each page I presented two barplots:
For each row of matrix A I plotted the values ofvariables as vertical
bars (6 bars), the names under the bars were column names from the
matrix, and I also put the value as text at the top of the bar (with
text() function). The second barplot was for the same row in B matrix.
By putting them on the same page in a pdf document I can look at the
bars for each variable and the values texted on the bars and compare
the differences between two experiments.

But this is not very effective. What I want to do is to combine the
two similar barplots for each experiment into one. So, for each
species I create just one barplot where I plot 6 clusters each
consisting of 2 bars, one bar for same varibale but different
experiment. I want to put the values as text at the top of each bar
and below each cluster I want to put the name of the variable (they
are the same for each experiment, remember).

I remembered that Michael Crawley did a side-by-side barplot in his
Introduction to R book, I used his idea before but now I have two
matrices and his method is not applicable.

Could someone please advise me how to do these side-by-side barplots?
A little bit of code showing the procedure would be appreciated.

Thank you in advance!

Best,
Sergey G
University of Zurich

P.S. I do not grasp how to post replies to my own posts (want to thank
the people that take their time to help out. Want to thank Gregory
Warnes for letting me know of gplots package, now my plots are very
informative!). Do I just put in the subject of email the name of my
previous post?

__
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.