Reputation: 90960
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).
Upvotes: 5
Views: 3268
Reputation: 173517
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:
Your example data only had two platforms, so maybe that was just a typo since you referenced three.
Upvotes: 7