1

It's a bit cluttered, I know, but I'm trying to further divide the data that makes up my stacked bar chart. Here's what it looks like so far:

A = ggplot(data=yield,aes(N,Mean.Yield,fill=Cutting))
B=A+facet_grid(Location~Mngmt)+geom_bar(stat="identity")
B+labs(x="Nitrogen Level")+labs(y="Yield (lb/acre)")

Yielding this graph: (I would post the graph but apparently my reputation isn't up to snuff as a new member!)

How can I further divide the bars by the factor "species"? I'm assuming it involves adding another geom, but I'm new to all this. Thanks!

Edited to add: Attempting to use mtcars for dummy data, though not the best as mpg is not additive like yield over two cutting is in my data.

mtcars$cyl=as.factor(mtcars$cyl)
mtcars$vs=as.factor(mtcars$vs)
mtcars$am=as.factor(mtcars$am)
mtcars$gear=as.factor(mtcars$gear)
mtcars$carb=as.factor(mtcars$carb)
A = ggplot(data=mtcars,aes(cyl,mpg,fill=gear))
B=A+facet_grid(am~vs)+geom_bar(stat="identity")

This yields this ugly graph: https://i.stack.imgur.com/WHT8d.png(https://i.stack.imgur.com/WHT8d.png) I'm hoping to split each of those bars (e.g., cylinders) into two side by side bars (in this example, 6 side by side bars denoting the mpg of engines with varying levels of carb for each cylinder factor). I hope this makes sense. Thanks again!

Slim
  • 11
  • 2
  • Upload your graph to any free image site, post URL here, we'll edit it in for you. – smci Jan 06 '15 at 21:32
  • What do you mean *'divide the bars by the factor "species"'*, a second faceting, or stacked barplot, or side-by-side barplot? – smci Jan 06 '15 at 21:33
  • 3
    @Slim Do you have a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)? If you are unable or unwilling to share your own data, either create dummy data or use one of R's built in [datasets](http://stat.ethz.ch/R-manual/R-patched/library/datasets/html/00Index.html). As a guess to your question, you can use `+` in the `facet()`: `facet_grid(Location~Mngmt + species)` or change the color of the bars: `geom_bar(stat="identity", aes(color = species))`. – Richard Erickson Jan 06 '15 at 21:36
  • graph I have so far: http://i.imgur.com/eZEaWfL.png I guess I'm not sure how to word what I'm trying to. I want to split each one of those bars into two side by side bars that are separate colors – Slim Jan 06 '15 at 22:47
  • @RichardErickson using the `+ Species` in the `facet()` was definitely close to what I'm looking for, but I guess I'm trying to have those bars side by side. I think it's called interleaved? – Slim Jan 06 '15 at 22:54
  • @smci I've further edited with an attempt at providing dummy data. I'm hoping to create stacked side-by-side barplots. that's a mouthful. – Slim Jan 07 '15 at 00:16
  • possible duplicate of [Issue with ggplot2, geom\_bar, and position="dodge": stacked has correct y values, dodged does not](http://stackoverflow.com/questions/11604070/issue-with-ggplot2-geom-bar-and-position-dodge-stacked-has-correct-y-values) – Richard Erickson Jan 07 '15 at 02:45

1 Answers1

1

Okay, based upon your comments, I think you want to change the position within the geom_bar(). Using the diamonds dataset from ggplot2, Does this look like what you want?

library(ggplot2)
## note the diamonds dataset comes with ggplot2

ggplot(diamonds, aes(clarity, fill=cut)) + 
    geom_bar(position="dodge") 

example image from ggplot2
(source: ggplot2.org)

Then you would just add in your facet and other details. With the diamonds example, this would be

ggplot(diamonds, aes(clarity, fill=cut)) + 
    geom_bar(position="dodge") + 
    facet_grid(color ~ clarity)

I figured out how to do this browsing the ggplot2 help files

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Richard Erickson
  • 2,568
  • 8
  • 26
  • 39
  • I've tried that, but when I set the position to dodge, it negates the additive stacking that I'm looking for. I attempted to provide dummy code using the mtcars dataset in my edit to my original dataset – Slim Jan 06 '15 at 23:58
  • @Slim I searched through the Stacks and found somebody else with a [similar problem](http://stackoverflow.com/questions/11604070/issue-with-ggplot2-geom-bar-and-position-dodge-stacked-has-correct-y-values). Take a look. They end having a similar problem. One of the comments is that "I think what you want is to both dodge and stack, but you can't do both." Sadly, I don't think `ggplot` can do what you want it to do. You might try posting your question to the ggplot2 [Google group](https://groups.google.com/forum/#!forum/ggplot2). – Richard Erickson Jan 07 '15 at 02:41