Thanks Peter and Dennis for your help! On Thu, Nov 26, 2009 at 3:50 PM, Dennis Murphy <djmu...@gmail.com> wrote:
> Hi: > > In addition to Peter's suggestions, I would suggest that you get rid of the > row names as the > first column of your data frame, as it confuses barchart (and the > interpretation of the plot). > I used x for your data frame, and this is the bar chart I ended up with: > > > x > Sample Col1 Col2 Col3 > 1 Row1 -2 4 -1 > 2 Row2 3 -2 4 > > 3 Row3 3 5 -2 > 4 Row4 4 1 -1 > > The bar chart using your code, and the data set in this form (Sample is > column1), is given below: > > dta <- x # change names to conform to > your code > > barchart(data.matrix(dta), > horizontal=FALSE, > stack=TRUE, > par.settings = simpleTheme(col = 2:4), > auto.key=list(space="right", title=names(dimnames(dta))[2]), > scales = list(y = list(at = -2:8)) ) > > Notice that barchart tries to incorporate the row names as legitimate > categories; I can't make > heads or tails out of this... > > As it turns out, dta is a data frame with dim(dta) = c(4, 4). You want rows > 1:4 to be row labels, > not data, so... > > x <- x[, 2:4] > x > Col1 Col2 Col3 > 1 -2 4 -1 > 2 3 -2 4 > 3 3 5 -2 > 4 4 1 -1 > rownames(x) <- paste('Row', 1:4, sep = '') > x > > Col1 Col2 Col3 > Row1 -2 4 -1 > Row2 3 -2 4 > Row3 3 5 -2 > Row4 4 1 -1 > > x now has dimension c(4, 3). Let's try barchart again... > > barchart(data.matrix(x), horizontal = FALSE, stack = TRUE, > par.settings = simpleTheme(col = 2:4), > auto.key = list(space = 'right', title = names(colnames(x))), > scales = list(y = list(at = -2:8)) ) > > Not your conventional stacked bar chart, but at least it seems more > interepretable. > > HTH, > Dennis > > > > > On Thu, Nov 26, 2009 at 10:53 AM, Peng Cai <pengcaimaill...@gmail.com>wrote: > >> Hi R Users, >> >> I'm trying to plot a stacked barplot. Here is data: >> >> Sample Col1 Col2 Col3 >> Row1 -2 4 -1 >> Row2 3 -2 4 >> Row3 3 5 -2 >> Row4 4 1 -1 >> >> I'm using following R code: >> >> library(lattice) >> dta<-read.table("data.txt", header=TRUE, row.names="Sample") >> barchart(data.matrix(dta), >> horizontal=FALSE, >> stack=TRUE, >> col=2:4, >> auto.key=list(space="right", >> title=names(dimnames(dta))[2]) >> ) >> >> Above code is working fine, but I need help with: >> >> 1) Legend boxes have default colors, whereas I'm looking them to match >> with >> barplot colors (col=2:4). >> 2) Can I increase scale for y axis, like currently it plotting >> -2,0,2,4,... >> I would like it as -2,-1,0,1,... >> >> Any help would be greatly appreciated, >> >> Thanks, >> Peng >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> 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. >> > > [[alternative HTML version deleted]] ______________________________________________ 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.