[R] Matrix barplot

Nutter, Benjamin NutterB at ccf.org
Fri Jul 25 14:26:36 CEST 2008


> data <- data.frame(Year=c(2000,2001,2002),
                     A=c(4,2,1), 
                     B=c(3,1,2), 
                     C=c(0,3,5))

> data.mat <- as.matrix(data)[,2:4]
> rownames(data.mat) <- data$Year
> data.mat <- t(data.mat)
> barplot(data.mat,beside=TRUE)

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Andreas Tille
Sent: Friday, July 25, 2008 8:14 AM
To: r-help at r-project.org
Subject: [R] Matrix barplot

Hi,

as a bloody R beginner I failed to solve the probably simple problem
to create a barplot of the following data read from a file

      Year      A     B    C
      2000      4     3    0
      2001      2     1    3
      2002      1     2    5

The Barplot should look like

     5 |                 C
     4 |   A             C
     3 |   AB      C     C
     2 |   AB    A C    BC
     1 |   AB    ABC   ABC
       +------------------
          2000  2001  2002

(well, something like that - the colors are encoded as letters in this
  ASCII-graphics - assume the coloring / shading as usual).

My problem is that if I read the table using

    data <- read.table(file='data.dat', sep = '\t', fill=TRUE,
header=TRUE )

everything looks is read as expected if I try

    data

but I have no idea how to separate the Matrix (A B C) to specify the
height parameter of barplot as a matrix and the names.arg parameter
for the inscription.  Whatever I tried I get

    'height' must be a vector or a matrix

and I have no idea to do this right.  So I think I'm just facing a
data conversion problem and have to turn data[['Year']]  into a vector
and the remaining table into a matrix.

I guess this is a really simple question - but I failed to find a
solution.

Kind regards

        Andreas.

-- 
http://fam-tille.de

______________________________________________
R-help at 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.



P Please consider the environment before printing this e-mail

Cleveland Clinic is ranked one of the top hospitals
in America by U.S. News & World Report (2007).  
Visit us online at http://www.clevelandclinic.org for
a complete listing of our services, staff and
locations.


Confidentiality Note:  This message is intended for use\...{{dropped:13}}



More information about the R-help mailing list