Hi, I'm trying to create a stacked bar plot using ggplot2. Rather than plotting the count of each of the 13 "Bar" factors on the Y axis, I would like to represent the sum of the Values associated with each of the 13 "Bar" factors. Is there a way to do that? Given the following data, that would obviously mean that there would be some negative sums represented. Here's a bit of example data along with the command I've been using.>library(ggplot2) > xValue Bar Segment 1 1.10020075 1 1 2 -1.37734577 2 1 3 2.50702876 3 1 4 0.58737028 3 2 5 0.21106851 3 3 6 -2.50119261 4 1 7 1.34984831 5 1 8 -0.27556149 6 1 9 -1.54401647 6 2 10 -2.75975562 6 3 11 -0.09527123 6 4 12 1.36331646 7 1 13 -0.36051429 8 1 14 1.36790999 9 1 15 0.15064633 9 2 16 0.34022421 9 3 17 -0.64512970 10 1 18 0.83268199 11 1 19 -1.50117728 12 1 20 1.09004959 13 1> qplot(factor(Bar), data = x, geom = "bar", fill = factor(Segment))Thanks for any suggestions you might have. James
Richard M. Heiberger
2012-Jan-16 03:15 UTC
[R] ggplot2 stacked bar - sum of values rather than count
This is based on lattice, not ggplot, and I hope I understand what you are trying to do. require(lattice) x <- read.table(text=" Value Bar Segment 1.10020075 1 1 -1.37734577 2 1 2.50702876 3 1 0.58737028 3 2 0.21106851 3 3 -2.50119261 4 1 1.34984831 5 1 -0.27556149 6 1 -1.54401647 6 2 -2.75975562 6 3 -0.09527123 6 4 1.36331646 7 1 -0.36051429 8 1 1.36790999 9 1 0.15064633 9 2 0.34022421 9 3 -0.64512970 10 1 0.83268199 11 1 -1.50117728 12 1 1.09004959 13 1 ", header=TRUE) qplot(factor(Bar), data = x, geom = "bar", fill = factor(Segment)) xx <- matrix(0, 13, 4, dimnames=list(1:13, 1:4)) for (i in 1:nrow(x)) xx[x$Bar[i], x$Segment[i]] <- x$Value[i] xx barchart(xx, horizontal=FALSE) Please see the plot.likert function in the HH package ## install.packages("HH") ## if necessary library(HH) ?likert for a development of graphs for data similar to what you have. Rich On Sun, Jan 15, 2012 at 9:08 PM, J Toll <jctoll@gmail.com> wrote:> Hi, > > I'm trying to create a stacked bar plot using ggplot2. Rather than > plotting the count of each of the 13 "Bar" factors on the Y axis, I > would like to represent the sum of the Values associated with each of > the 13 "Bar" factors. Is there a way to do that? Given the following > data, that would obviously mean that there would be some negative sums > represented. Here's a bit of example data along with the command I've > been using. > > >library(ggplot2) > > x > Value Bar Segment > 1 1.10020075 1 1 > 2 -1.37734577 <2%20%20-1.37734577> 2 1 > 3 2.50702876 3 <2.50702876%20%20%203> 1 > 4 0.58737028 <4%20%20%200.58737028> 3 2 > 5 0.21106851 3 3 > 6 -2.50119261 4 1 > 7 1.34984831 <7%20%20%201.34984831> 5 1 > 8 -0.27556149 <8%20%20-0.27556149> 6 1 > 9 -1.54401647 <9%20%20-1.54401647> 6 2 > 10 -2.75975562 6 3 > 11 -0.09527123 6 4 > 12 1.36331646 <12%20%201.36331646> 7 1 > 13 -0.36051429 8 1 > 14 1.36790999 <14%20%201.36790999> 9 1 > 15 0.15064633 <15%20%200.15064633> 9 2 > 16 0.34022421 <16%20%200.34022421> 9 3 > 17 -0.64512970 10 1 > 18 0.83268199 <18%20%200.83268199> 11 1 > 19 -1.50117728 12 1 > 20 1.09004959 13 1 > > qplot(factor(Bar), data = x, geom = "bar", fill = factor(Segment)) > > Thanks for any suggestions you might have. > > James > > ______________________________________________ > 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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
On 16/01/12 02:08, J Toll wrote:> Hi, > > I'm trying to create a stacked bar plot using ggplot2. Rather than > plotting the count of each of the 13 "Bar" factors on the Y axis, I > would like to represent the sum of the Values associated with each of > the 13 "Bar" factors. Is there a way to do that? Given the following > data, that would obviously mean that there would be some negative sums > represented. Here's a bit of example data along with the command I've > been using. > >> library(ggplot2) >> x > Value Bar Segment > 1 1.10020075 1 1 > 2 -1.37734577 2 1 > 3 2.50702876 3 1 > 4 0.58737028 3 2 > 5 0.21106851 3 3 > 6 -2.50119261 4 1 > 7 1.34984831 5 1 > 8 -0.27556149 6 1 > 9 -1.54401647 6 2 > 10 -2.75975562 6 3 > 11 -0.09527123 6 4 > 12 1.36331646 7 1 > 13 -0.36051429 8 1 > 14 1.36790999 9 1 > 15 0.15064633 9 2 > 16 0.34022421 9 3 > 17 -0.64512970 10 1 > 18 0.83268199 11 1 > 19 -1.50117728 12 1 > 20 1.09004959 13 1 >> qplot(factor(Bar), data = x, geom = "bar", fill = factor(Segment)) > Thanks for any suggestions you might have. > > James > > ______________________________________________ > 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.I'm not at my usual computer, but in ggplot2 the geom_bar geom is designed for stats, so by default does report count. You can change the stat method (ie to identity to just use the values in the column) but the easiest (IIRC) is to give a weight; #Gives count || qplot <http://had.co.nz/ggplot2/qplot.html>(color, data=diamonds, geom="bar") #Gives sum of carat variable qplot <http://had.co.nz/ggplot2/qplot.html>(color, data=diamonds, geom="bar", weight=carat, ylab="carat") #just gives raw values from meanprice column || qplot <http://had.co.nz/ggplot2/qplot.html>(cut, meanprice, geom="bar", stat="identity") Check out the ggplot2 help page (http://had.co.nz/ggplot2/geom_bar.html) for more info. Regards, Paul. [[alternative HTML version deleted]]