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]]
Peng Cai 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).replace the line col = 2:4, with par.settings = simpleTheme(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,...add the line scales = list(y = list(at = -2:8)), or whatever tick locations you prefer. -Peter Ehlers> > Any help would be greatly appreciated, > > Thanks, > Peng > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. > >
Thanks Peter and Dennis for your help! On Thu, Nov 26, 2009 at 3:50 PM, Dennis Murphy <djmuser@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 <pengcaimaillist@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]]
Hi, I'm plotting grouped barplot using following code and data. I need help with re-ordering the labels. 1. On x-axis the factor "AGE" is grouped in order "0--4", "15--18", "5--14"; whereas I would like to have it in "0--4", "5--14", "15--18". 2. If I need to re-order "RACE" variable. How can I do it assuming I need to change it on both the x-axis and legend. Currenlty the order is "Black","Other","White"; whereas I would like "White", "Black", "Other". Can anyone help please. I'm using following code, which is working fine except above issues. Code: library(lattice) ################### assuming data is read in object name "dta". attach(dta) barchart(sum ~ age | gender, data = dta, groups = race, stack = FALSE, ylab="Sum of admissions over 10 years (1996-2005)", xlab="Age", par.settings=simpleTheme(col = c("green1", "yellow1", "orange")), key=list(space="right", cex=1, text=list(c("Black","Other","White")), rectangles=list(size=1.7, border="white", col = c("green1", "yellow1", "orange"))), strip = strip.custom(bg="greenyellow") ) detach(dta) Data: age gender race sum 0--4 Female Black 145 0--4 Female Other 53 0--4 Female White 47 0--4 Male Black 286 0--4 Male Other 130 0--4 Male White 94 15--18 Female Black 30 15--18 Female Other 3 15--18 Female White 9 15--18 Male Black 21 15--18 Male Other 2 15--18 Male White 3 5--14 Female Black 138 5--14 Female Other 31 5--14 Female White 23 5--14 Male Black 199 5--14 Male Other 65 5--14 Male White 29 Thanks, Peng [[alternative HTML version deleted]]