weis26
weis26

Reputation: 401

How do you create a "nested" bar graph?

The last image in this blog post.

I have tried searching "nested bar graph" and "hierarchical bar graph", but they may not be the word for it.

Upvotes: 6

Views: 2770

Answers (3)

rpalloni
rpalloni

Reputation: 131

Use this:

ggplot() +
    geom_bar(data=stupidTotal, aes(x=group, y=value, fill="grey50"), stat="identity") +
    geom_bar(data=mstupid, aes(x=group, y=value, fill=variable), 
             stat="identity", position="dodge") +
    theme_bw()

Upvotes: 0

Yorgos
Yorgos

Reputation: 30465

Look for 'barNest' in package plotrix

Upvotes: 1

Andrie
Andrie

Reputation: 179448

Use ggplot and create separate layers:

library(ggplot2)

set.seed(1)
stupid <- data.frame(
    group= LETTERS[1:5],
    men = sample(1:10, 5),
    women = sample(1:10, 5) 
)

# Melt the data and calculate totals
mstupid <- melt(stupid, id.vars="group")
stupidTotal <- ddply(mstupid, .(group), summarize, value=sum(value))

ggplot() +
    geom_bar(data=stupidTotal, aes(x=group, y=value), fill="grey50") +
    geom_bar(data=mstupid, aes(x=group, y=value, fill=variable), 
             stat="identity", position="dodge") +
    theme_bw()

enter image description here

Upvotes: 9

Related Questions