acacia21 wrote on 01/09/2012 07:01:28 PM: > Hi all, > i'm fairly new to R and its graphing, but having unsuccessfully 'googled' > and checked this forum to find answer to my problem, i'm posting my question > here. > > I'm trying to plot stacked barplot. I have simple data that looks like this: > bg ag > 0.41 2.81 > 0.37 2.91 > 0.31 2.06 > 0.32 2.39 > > every row indicates a factor (1,2,3,4, see below in names.arg). Now when i > plot this using following function for stacked barplots: > plot<-barplot(t(data), main=txt, ylim=c(0,10), col=c("white", "grey90"), > ylab="Total biomass (g)", space=0.1, names.arg=c("1", "2", "3", "4")) > > i get a lovely stacked graph and it color-codes with white (bg) and grey90 > (ag) in each individual stacked bar. I have a total of 4 stacked bars as i > have 4 factors. Now here is the question: i would like to add density lines > across entire stacked bar or any other graphic feature to distinguish > between bars 1 and 2 as they indicate same factor and 3 and 4 that indicate > different factor. Is there any way to do that? Surely it's possible, but not > so obvious for the beginner =) > > thank you very much
One workaround is to create a second matrix of data, with zeroes for the bars where you don't want to add emphasis. Then, overlay a second bar plot with density lines for emphasis. df <- data.frame(bg=c(0.41, 0.37, 0.31, 0.32), ag=c(2.81, 2.91, 2.06, 2.39)) # matrix for full barplot m <- t(df) # matrix for subset of barplot to add angled lines to mlines <- m mlines[, 3:4] <- 0 barplot(m, space=0.1, col=c("white", "grey90"), ylim=c(0, 10), ylab="Total biomass (g)", names.arg=c("1", "2", "3", "4")) barplot(mlines, space=0.1, col="black", density=10, angle=35, add=TRUE) Jean P.S. You should avoid creating objects that have the same names as currently existing functions, e.g., "data" and "plot". [[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.