Skip to content

Multiple stacked barplots on the same graph?

11 messages · Stéphane CRUVEILLER, Dieter Menne, Domenico Vistocco +3 more

#
Dear R-Users,

I would like to know whether it is possible to draw several
stacked barplots (i.e. side by side on the same sheet)...


my data look like :

                            Cond1  Cond1' Cond2   Cond2'
Compartment 1    11,81    2,05    12,49    0,70   
Compartment 2     10,51    1,98    13,56    0,85
Compartment 3     1,95    0,63    2,81    0,22  
Compartment 4     2,08    0,17    3,13    0,06   
Compartment 5     2,51    0,20    4,58    0,03

ps: Cond1' values should be stacked on Cond1, Cond2' on Cond 2 and so 
on... and series 1 and 2
should be side by side for each compartement....

Thanks for any help.

St?phane.

-- 
"La science a certes quelques magnifiques r?ussites ? son actif mais
? tout prendre, je pr?f?re de loin ?tre heureux plut?t qu'avoir raison." 
D. Adams
#
St?phane CRUVEILLER <scruveil <at> genoscope.cns.fr> writes:
Check the following example on xyplot/lattice:

barchart(yield ~ variety | site, data = barley,
         groups = year, layout = c(1,6), stack = TRUE, 
         auto.key = list(points = FALSE, rectangles = TRUE, space = "right"),
         ylab = "Barley Yield (bushels/acre)",
         scales = list(x = list(rot = 45)))

You have to reformat your data to the long format first for this, e.g. by using
reshape.

Comp  Cond  x
1     1     11.81
1     2      2.05

With allow.multiple=TRUE, it might be possible to avoid the reformatting, but I
never have used that form, because I the "long" had many advantages.

Dieter
#
Perhaps this could be useful:
 > x=scan()
11.81 10.51  1.95  2.08  2.51  2.05  1.98  0.63  0.17  0.20
12.49 13.56 2.81  3.13  4.58  0.70  0.85  0.22  0.06  0.03

 > x=matrix(x,5,4,byrow=T)
 > rownames(x)=paste("comp",1:5,sep="")
 > colnames(x)=paste("c",1:4,sep="")

 > library(ggplot2)
 > dfm=melt(x)
 > qplot(as.factor(x=X1),y=value,geom="histogram",data=dfm,fill=X2)

domenico vistocco
St?phane CRUVEILLER wrote:
#
Hi,

I tried this method but it seems that there is something wrong with my 
data frame:


when I type in:

 > qplot(x=as.factor(Categorie),y=Total,data=mydata)

It displays a graph with 2 points in each category...
but if  I add the parameter geom="histogram"

 > qplot(x=as.factor(Categorie),y=Total,data=mydata,geom="histogram")


Error in storage.mode(test) <- "logical" :
        object "y" not found

any hint about this...

thx, St?phane.


========================================================
the real data frame

                                                    Categorie     Part 
Total  chr1  chr2    pl
1                                     Amino acid biosynthesis   common  
3.03  4.55  1.68  0.00
2          Purines, pyrimidines, nucleosides, and nucleotides   common  
1.65  2.37  1.06  0.00
3                      Fatty acid and phospholipid metabolism   common  
1.52  1.77  1.55  0.00
4  Biosynthesis of cofactors, prosthetic groups, and carriers   common  
2.85  4.68  1.02  0.00
5                             Central intermediary metabolism   common  
3.40  3.19  4.57  0.00
6                                           Energy metabolism   common 
11.81 12.49 13.87  0.17
7                              Transport and binding proteins   common 
10.51 13.56  7.85  4.27
8                                              DNA metabolism   common  
1.95  2.81  0.98  1.03
9                                               Transcription   common  
2.08  3.13  1.06  0.34
10                                          Protein synthesis   common  
2.51  4.58  0.27  0.00
11                                               Protein fate   common  
2.23  3.26  1.20  0.68
12                                       Regulatory functions   common  
7.63  7.30  9.88  0.68
13                                        Signal transduction   common  
1.88  2.06  2.13  0.00
14                                              Cell envelope   common  
2.76  3.41  2.53  0.17
15                                         Cellular processes   common  
7.21  7.90  7.71  1.54
16              Mobile and extrachromosomal element functions   common  
1.08  0.22  0.40  8.38
17                                           Unknown function   common 
20.75 22.45 22.38  5.30
18                                    Amino acid biosynthesis specific  
0.35  0.16  0.71  0.00
19         Purines, pyrimidines, nucleosides, and nucleotides specific  
0.17  0.06  0.35  0.00
20                     Fatty acid and phospholipid metabolism specific  
0.08  0.09  0.09  0.00
21 Biosynthesis of cofactors, prosthetic groups, and carriers specific  
0.18  0.19  0.22  0.00
22                            Central intermediary metabolism specific  
0.42  0.09  0.98  0.00
23                                          Energy metabolism specific  
2.05  0.70  3.90  2.22
24                             Transport and binding proteins specific  
1.98  0.85  3.24  3.25
25                                             DNA metabolism specific  
0.63  0.22  0.22  4.44
26                                              Transcription specific  
0.17  0.06  0.22  0.51
27                                          Protein synthesis specific  
0.20  0.03  0.49  0.00
28                                               Protein fate specific  
0.30  0.32  0.31  0.17
29                                       Regulatory functions specific  
1.58  0.66  2.79  1.88
30                                        Signal transduction specific  
0.27  0.06  0.62  0.00
31                                              Cell envelope specific  
0.83  0.63  1.33  0.00
32                                         Cellular processes specific  
1.38  0.38  1.95  4.62
33              Mobile and extrachromosomal element functions specific  
3.56  1.14  0.44 28.72
34                                           Unknown function specific 
11.63  6.17 16.00 24.27
Domenico Vistocco wrote:
-- 
"La science a certes quelques magnifiques r?ussites ? son actif mais
? tout prendre, je pr?f?re de loin ?tre heureux plut?t qu'avoir raison." 
D. Adams
#
--- Domenico Vistocco <vistocco at unicas.it> wrote:

            
qplot(as.factor(x=X1),y=value,geom="histogram",data=dfm,fill=X2)
Very nice but bit garish. :)

What about a dotchart instead?

 dotchart(x, labels=rownames(x),col=c(1:4), pch=16)
#
Hi, I am trying to get 95% CI s around a quantile growth curve, but  
my result looks strange to me.

Here is the function I defined myself

boot.qregress<-function(mat1, group,  quantile, int, seed.1){

     boot.fit<-NULL
     set.seed(seed.1)
     for (i in 1:int){

     index<-sample((unique(mat1$Subject[mat1$Group==group])), length 
(unique(mat1$Subject[mat1$Group==group])), replace=TRUE)

     #make the bootstrapping dataset
     mat.junk<-NULL
     for (j in 1: length(index)){

         mat.junk<-rbind(mat.junk, mat1[mat1$Subject==index[j], ])	
     	}

    boot.fit<-cbind(boot.fit, cobs(mat.junk$Day, mat.junk$Weight,   
constraint="none",  degree=2, tau=quantile, lambda=-1)$fitted)	
     	
    	}

    boot.fit 	

}


The curves I made from the bootstrapping is attached, I don't  
understand why for a group, the 5% curve drops suddenly around time  
130. I am thinking about missingness since before 130 there are 50  
patients, but after day 130 there are only 40 patients for this group.

Any suggestions on the R-code (especially about how to do the  
bootstrapping for the growth curves) or why the drops happened would  
be appreciated.

Thanks a lot,

Suyan
?
#
On Dec 4, 2007 11:19 AM, John Kane <jrkrideau at yahoo.ca> wrote:
which would be (roughly equivalent to)

qplot(X2, value, data=dfm, colour=X2, facets = . ~ X1)

in ggplot.

Hadley
#
On Dec 4, 2007 10:34 AM, St?phane CRUVEILLER <scruveil at genoscope.cns.fr> wrote:
Could you copy and paste the output of dput(mydata) ?

(And I'd probably write the plot call as: qplot(Categorie, Total,
data=mydata, geom="bar"), since it is a bar plot, not a histogram)
#
Hi,

the same error message is displayed with geom="bar" as parameter.
here is the output of dput:

 > dput(mydata)
structure(list(Categorie = structure(c(1L, 12L, 8L, 2L, 5L, 7L,
16L, 6L, 15L, 11L, 10L, 13L, 14L, 3L, 4L, 9L, 17L, 1L, 12L, 8L,
2L, 5L, 7L, 16L, 6L, 15L, 11L, 10L, 13L, 14L, 3L, 4L, 9L, 17L
), .Label = c("Amino acid biosynthesis", "Biosynthesis of cofactors, 
prosthetic groups, and carriers",
"Cell envelope", "Cellular processes", "Central intermediary metabolism",
"DNA metabolism", "Energy metabolism", "Fatty acid and phospholipid 
metabolism",
"Mobile and extrachromosomal element functions", "Protein fate",
"Protein synthesis", "Purines, pyrimidines, nucleosides, and nucleotides",
"Regulatory functions", "Signal transduction", "Transcription",
"Transport and binding proteins", "Unknown function"), class = "factor"),
    Part = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("common",
    "specific"), class = "factor"), Total = c(3.03, 1.65, 1.52,
    2.85, 3.4, 11.81, 10.51, 1.95, 2.08, 2.51, 2.23, 7.63, 1.88,
    2.76, 7.21, 1.08, 20.75, 0.35, 0.17, 0.08, 0.18, 0.42, 2.05,
    1.98, 0.63, 0.17, 0.2, 0.3, 1.58, 0.27, 0.83, 1.38, 3.56,
    11.63), chr1 = c(4.55, 2.37, 1.77, 4.68, 3.19, 12.49, 13.56,
    2.81, 3.13, 4.58, 3.26, 7.3, 2.06, 3.41, 7.9, 0.22, 22.45,
    0.16, 0.06, 0.09, 0.19, 0.09, 0.7, 0.85, 0.22, 0.06, 0.03,
    0.32, 0.66, 0.06, 0.63, 0.38, 1.14, 6.17), chr2 = c(1.68,
    1.06, 1.55, 1.02, 4.57, 13.87, 7.85, 0.98, 1.06, 0.27, 1.2,
    9.88, 2.13, 2.53, 7.71, 0.4, 22.38, 0.71, 0.35, 0.09, 0.22,
    0.98, 3.9, 3.24, 0.22, 0.22, 0.49, 0.31, 2.79, 0.62, 1.33,
    1.95, 0.44, 16), pl = c(0, 0, 0, 0, 0, 0.17, 4.27, 1.03,
    0.34, 0, 0.68, 0.68, 0, 0.17, 1.54, 8.38, 5.3, 0, 0, 0, 0,
    0, 2.22, 3.25, 4.44, 0.51, 0, 0.17, 1.88, 0, 0, 4.62, 28.72,
    24.27)), .Names = c("Categorie", "Part", "Total", "chr1",
"chr2", "pl"), class = "data.frame", row.names = c(NA, -34L))


thx,

St?phane.
hadley wickham wrote:
-- 
"La science a certes quelques magnifiques r?ussites ? son actif mais
? tout prendre, je pr?f?re de loin ?tre heureux plut?t qu'avoir raison." 
D. Adams
#
This command works:

qplot(x=Categorie,y=Total,data=mydata,geom="bar",fill=Part)

for your data.

domenico vistocco
St?phane CRUVEILLER wrote:
#
And qplot(x=Categorie,y=Total,data=mydata,geom="bar",fill=Part) + coord_flip()

makes it a bit easier to read the labels.

Hadley
On Dec 5, 2007 8:33 AM, Domenico Vistocco <vistocco at unicas.it> wrote: