Hi, I would like to draw a stacked bar chart with four bars (say "a", "b", "c", "d") . Two bars belong to group A and the two others to group B. Therefore, I would like to have, on the x-axis, a label for each bar and an additional label for each group, positioned underneath. To give an idea, the x-axis labels should look like this: |a|b|c|d| | A | B | Do you know how I can generate such two-levels labels in R? Thank you for your help! Sebastien [[alternative HTML version deleted]]
Hi S?bastien, Not sure about an elegant, general way but here is something quick and dirty: p <- barplot(matrix(1:8, 2)) axis(1, at = p, labels = letters[1:4]) axis(1, at = c(mean(p[1:2]), mean(p[3:4])), labels = paste("\n", LETTERS[1:2]), padj = 1) Cheers, Josh On Mon, Aug 22, 2011 at 10:14 AM, S?bastien Vigneau <sebastien.vigneau at gmail.com> wrote:> Hi, > > I would like to draw a stacked bar chart with four bars (say "a", "b", "c", > "d") . Two bars belong to group A and the two others to group B. Therefore, > I would like to have, on the x-axis, a label for each bar and an additional > label for each group, positioned underneath. To give an idea, the x-axis > labels should look like this: > |a|b|c|d| > | A | B | > > Do you know how I can generate such two-levels labels in R? > > Thank you for your help! > > Sebastien > > ? ? ? ?[[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. >-- Joshua Wiley Ph.D. Student, Health Psychology Programmer Analyst II, ATS Statistical Consulting Group University of California, Los Angeles https://joshuawiley.com/
On Aug 22, 2011, at 12:14 PM, S?bastien Vigneau wrote:> Hi, > > I would like to draw a stacked bar chart with four bars (say "a", "b", "c", > "d") . Two bars belong to group A and the two others to group B. Therefore, > I would like to have, on the x-axis, a label for each bar and an additional > label for each group, positioned underneath. To give an idea, the x-axis > labels should look like this: > |a|b|c|d| > | A | B | > > Do you know how I can generate such two-levels labels in R? > > Thank you for your help! > > SebastienSee ?barplot and note in the Value section: If beside is true, use colMeans(mp) for the midpoints of each group of bars, see example. Here is another example: mat <- matrix(1:4, ncol = 2) mp <- barplot(mat, beside = TRUE) Groups <- c("A", "B") Sub.Groups <- c("a", "b", "c", "d") mtext(side = 1, line = 1, at = mp, text = Sub.Groups) mtext(side = 1, line = 3, at = colMeans(mp), text = Groups) HTH, Marc Schwartz