在ggplot2中的一个地块上的条形图和堆积条形图

时间:2015-02-11 21:45:37

标签: r ggplot2

晚上好,任何人都可以帮忙策划。我有一些这样的数据:

    DF <- data.frame(
        country_dest=c("Russia", "Germany", "Ukraine" ,"Kazakhstan", 
        "United States", "Italy", "Israel", "Belarus"),
        SumTotal=c(7076562,2509617,1032325,680137,540630,359030,229186,217623)
    )

用单独的8个条形图来绘制它并不是什么大不了的事,但我想知道是否有可能用3个条形图,其中第一个条形图将与俄罗斯的数据(例如)第二个将是德国,乌克兰,哈萨克斯坦,美国和意大利的堆积酒吧也许有一些传说可以了解谁是白俄罗斯和以色列的第三堆酒吧。

在互联网上,我找到了一个用0值创建新DF的解决方案,但不太明白。

比你提前!

1 个答案:

答案 0 :(得分:3)

那么,您需要为数据添加分组信息。然后它变得简单明了。这是一个策略

#define groups
grp <-c("Russia"=1, "Germany"=2, "Ukraine"=2,"Kazakhstan"=2, 
        "United States"=2, "Italy"=2, "Israel"=3, "Belarus"=3)
DF$grp<-grp[as.character(DF$country_dest)]

#ensure plotting order
DF$country_dest <- factor(DF$country_dest, levels=DF$country_dest)

#draw plot
ggplot(DF, aes(x=grp, y=SumTotal, fill=country_dest)) + 
    geom_bar(stat="identity")

这会给你

enter image description here

您可能希望为您的论坛提供更具描述性的标签。

相关问题