Re: [R] Barplot w/ single stacked bar

From: Muenchen, Robert A (Bob) <muenchen_at_utk.edu>
Date: Thu, 24 Jan 2008 11:59:18 -0500

Marc & Eric,

Thanks so much for the help. That is exactly what I was looking for.

I should have mentioned that I don't really like this plot, but I'm writing an explanation of the Grammar of Graphics concept. A very nice example of that is that a single stacked bar chart converts to a pie chart when you change from Cartesian to polar coordinates. And yes, that may well be going from bad to worse!

Thanks!
Bob



Bob Muenchen (pronounced Min'-chen), Manager Statistical Consulting Center
U of TN Office of Information Technology 200 Stokely Management Center, Knoxville, TN 37996-0520 Voice: (865) 974-5230
FAX: (865) 974-4810
Email: muenchen_at_utk.edu
Web: http://oit.utk.edu/scc,
news: http://listserv.utk.edu/archives/statnews.html

> -----Original Message-----
> From: Marc Schwartz [mailto:marc_schwartz_at_comcast.net]
> Sent: Thursday, January 24, 2008 11:32 AM
> To: Muenchen, Robert A (Bob)
> Cc: r-help_at_stat.math.ethz.ch
> Subject: Re: [R] Barplot w/ single stacked bar
>
> Muenchen, Robert A (Bob) wrote:
> > Hi All,
> >
> > I can get the barplot function to do many types of plots, stacked or
> > otherwise. However, I cannot get it to do a *single* stacked bar.
> I've
> > searched several books & listserv archives to no avail. I suspect
I'm
> > missing the obvious from the help file!
> >
> > I can reach my goal in ggplot2, although the relative heights of the
> > bar's pieces don't seem quite right (it does generate a warning):
> >
> > library(ggplot2)
> > x<-factor(1)
> > y<-factor( c("Male","Male","Female") )
> > mydata <- data.frame(x,y)
> > rm(x,y)
> > mydata
> >
> > #These are close to my goal:
> > qplot( x, y, fill=y, geom="bar", data=mydata)
> >
> > # or
> > ggplot(mydata, aes(x=x, y=y, fill=y)) + geom_bar()
> >
> > # But this places the bars beside each other rather than stack them.
> > barplot( table(mydata$y), beside=FALSE)
> >
> > Thanks!
> > Bob
>
> Bob,
>
> Try this:
>
> barplot(as.matrix(table(mydata$y)), beside = FALSE)
>
> Conceptually, for a stacked bar, each bar is a column in a matrix. The
> components in a stacked bar are the row values in the column.
>
> Thus, you need to create a single column matrix from your table.
>
> One might question the value of such a plot however, if the intent is
> to
> provide a visual representation of the difference in
counts/proportions
> between two groups. A side-by-side barplot or a dotchart would seem to
> be better here.
>
> HTH,
>
> Marc Schwartz



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. Received on Thu 24 Jan 2008 - 17:01:13 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 24 Jan 2008 - 17:30:08 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive