[R] bar plot stacked

William Dunlap wdunlap at tibco.com
Thu Jun 19 16:57:12 CEST 2014


> dataset<-as.matrix(read.csv("datafile.csv",header=FALSE))

You should always inspect the dataset immediately after reading it in, before
doing other manipulations on it.  Converting data from one format to
another is always going to be error-prone.  Plotting it, say with
pairs() or just plot(), is good and so is printing it, say with
summary(), str(), and print().

In your case I get (after copying your file to the clipboard):
R> df <- read.csv("clipboard",header=FALSE)
R> df
  V1 V2 V3
1  a 90 10
2  b 60 40
3  c NA NA
4  d NA 50
R> str(df)
'data.frame':   4 obs. of  3 variables:
 $ V1: Factor w/ 4 levels "a","b","c","d": 1 2 3 4
 $ V2: int  90 60 NA NA
 $ V3: int  10 40 NA 50

Because the first column is not numeric, as.matrix(df) will make a
character matrix and then barplot will get confused.

You can use the row.names=1 argument to read.csv to make the letters
the row names of df or you can convert just the 2nd and 3rd columns to
a matrix.

Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Thu, Jun 19, 2014 at 6:42 AM, message <letter at openmailbox.org> wrote:
> Readers,
>
> For data set:
>
> a, 90, 10
> b, 60, 40
> c, ,
> d, , 50
>
> A plot was attempted:
>
> dataset<-as.matrix(read.csv("datafile.csv",header=FALSE))
> barplot<-(dataset,horiz=TRUE)
>
> A warning message is returned, about NAs introduced by coercion and an
> undesirable graph. The desired output is something similar to:
>
> a       ---------*
> b       ------****
> c
> d       ****
>
> Whereby a legend would be produced to describe '*' and '-'. Any help please?
>
> ______________________________________________
> 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.



More information about the R-help mailing list