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?
On 19 Jun 2014, at 15:42, message <letter at openmailbox.org> wrote:> Readers, > > For data set: > > a, 90, 10 > b, 60, 40 > c, , > d, , 50 > > A plot was attempted:Wonder who attempted this. :-)> > dataset<-as.matrix(read.csv("datafile.csv",header=FALSE))Look at your dataset; I?d say it is clearly not what you want.> 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?library(ggplot2) library(reshape2) ddd <- read.csv("testdata.csv", header=FALSE) ggplot(melt(ddd, id.vars="V1"), aes(x=V1, y=value, fill=variable)) + geom_bar(stat="identity") + coord_flip() Best, Bart> > ______________________________________________ > 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.
> 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.
On Thu, 19 Jun 2014 01:42:30 PM message 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 andan> 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? >Hi message, Try this: # this is what you had after reading the data in testdf<-data.frame(letters[1:4],c(90,60,NA,NA),c(10,40,NA,50)) testmat<-as.matrix(t(testdf[,2:3])) colnames(testmat)<-testdf[,1] rownames(testmat)<-NULL barplot(testmat[,4:1],horiz=TRUE,col=c(NA,"gray")) Jim