Reputation: 11
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.sstatic.net/WHT8d.png(https://i.sstatic.net/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!
Upvotes: 1
Views: 480
Reputation: 2616
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")
(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
Upvotes: 1