5

I'm struggling a bit with ggplot2 and hoping to learn by example a bit more.

I have a lot of data that looks like like the thing this generates:

data.frame(version=c('v1', 'v1', 'v1', 'v1', 'v2', 'v2', 'v2', 'v2'),
           platform=c('linux', 'linux', 'mac', 'mac',
                      'linux', 'linux', 'mac', 'mac'),
           type=c('a', 'b', 'a', 'b', 'a', 'b', 'a', 'b'),
           count=floor(runif(8, 0, 10000)))

I got regular barplot to draw me a stacked bar chart of type for a given OS (by slicing it out with cast, but I've not quite got what I want with ggplot2 yet.

I can manage to plot a single platform by doing something like this (assuming the above is saved as sample):

qplot(version, a, data=cast(sample[sample$platform=='linux',],
                            version ~ type, value="count"),
      geom='bar')

Ideally, I'd like that stacked by type (explicitly a in this example -- there are only two types) and then have one per platform appear side-by-side on the same chart grouped by version.

That is, for every version, I'd like three bars (one for each platform) with two stacks each (by type).

1 Answer 1

7

Here's one option:

dat <- data.frame(version=c('v1', 'v1', 'v1', 'v1', 'v2', 'v2', 'v2', 'v2'),
           platform=c('linux', 'linux', 'mac', 'mac',
                      'linux', 'linux', 'mac', 'mac'),
           type=c('a', 'b', 'a', 'b', 'a', 'b', 'a', 'b'),
           count=floor(runif(8, 0, 10000)))


ggplot(data = dat, aes(x = platform, y = count)) + 
    facet_wrap(~version) + 
    geom_bar(aes(fill = type))

which produces something like this:

enter image description here

Your example data only had two platforms, so maybe that was just a typo since you referenced three.

Sign up to request clarification or add additional context in comments.

2 Comments

Oh sorry about that. I do have three in my real data, but my sample only had two. Got tiring to type all of the combinations. :) This looks pretty good. I think it may be easier to understand than exactly what I asked for.
@Dustin No problem. This blog has lots of good ggplot2 examples, as does Hadley's site but his book is by far the best reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.