如何创建“嵌套”条形图?

时间:2011-11-08 07:24:49

标签: r graph

this blog post中的最后一张图片。

我尝试过搜索“嵌套条形图”和“分层条形图”,但它们可能不适合它。

3 个答案:

答案 0 :(得分:9)

使用ggplot并创建单独的图层:

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

答案 1 :(得分:1)

在包plotrix

中查找'barNest'

答案 2 :(得分:0)

使用此:

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()