2个等级组与ggplot2 [英] 2 level groups with ggplot2

查看:133
本文介绍了2个等级组与ggplot2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在互联网上搜索过,找不到像我这样的例子。我有如下数据:

 FormaciónEn整合Consolidado 

Ene-Abr 2009 Meta 40 30 30

Realizado 35 45 20

May-Ago 2009 Meta 35 35 30

Realizado 34 45 20

9月-Dic 2009 Meta 30 30 40

Realizado 20 40 20

和I需要像下面这样的堆积条形图:



请注意,该图有两个级别组。

解决方案

首先,包含日期的列需要填满,没有空行,并且日期还应包含年份。我不知道你是如何得到你的数据的,所以在计算上可能需要一些修补,但它不应该那么难。我在这种情况下手动完成:

 > df 
Periodo Grupo Formacion En.consolidacion Consolidado
1 Ene-Abr.2009 Meta 40 30 30
2 Ene-Abr.2009 Realizado 35 45 20
5月-Ago.2009 Meta 35 35 30 $ b $ 4 5月-Ago.2009 Realizado 34 45 20
5 Sep-Dic.2009 Meta 30 30 40
6 Sep-Dic.2009 Realizado 20 40 20

(而不是空格,我在变量名称中使用了小圆点)。之后,使用 plyr 包和 facet_wrap

$ b $的熔化() b

  library(ggplot2)
library(plyr)

m = melt(df)
ggplot(m, aes(x = factor(Grupo),y = value,fill = factor(variable)))+
geom_bar(position =fill,stat =identity)+
scale_y_continuous(labels = percent ,
breaks = c(0.2,0.4,0.6,0.8,1))+#你可以设置任何你想要的休息
facet_wrap(〜Periodo)

这是什么你想要吗?



p>

以下是您的(编辑过的)数据:

  df = structure(list (Periodo = structure(c(1L,1L,2L,2L,3L,3L),.Label = c(Ene-Abr.2009,
May-Ago.2009,Sep-Dic。 Grupo = structure(c(1L,
2L,1L,2L,1L,2L),.Label = c(Meta,Realizado),class = 因素),
Formacion = c(40L,35L,35L,34L,30L,20L),En.consolidacion = c(30L,
45L,35L,45L,30L,40L),Consolidado = c(30L,20L,30L,
20L,40L,20L)),.Names = c(Periodo,Grupo,Formacion,
En.consolidacion,Consolidado ),class =data.frame,row.names = c(NA,
-6L))


I have searched all over Internet and can't find an example like mine. I have data like the following:

                        Formación   En consolidación   Consolidado

Ene-Abr 2009    Meta       40          30                 30

                Realizado  35          45                 20

May-Ago 2009    Meta       35          35                 30

                Realizado   34          45                 20

Sep-Dic 2009    Meta       30          30                 40

                Realizado  20          40                 20

and I need a stacked bar chart like the following:

Note that the graph has two level groups.

解决方案

First, the column with dates needs to be filled up, no empty lines, and the date should include the year as well. I don't know how you got your data, so doing that computationally might need some tinkering, but it shouldn't be that hard. I did it manually in this case:

> df
       Periodo     Grupo Formacion En.consolidacion Consolidado
1 Ene-Abr.2009      Meta        40               30          30
2 Ene-Abr.2009 Realizado        35               45          20
3 May-Ago.2009      Meta        35               35          30
4 May-Ago.2009 Realizado        34               45          20
5 Sep-Dic.2009      Meta        30               30          40
6 Sep-Dic.2009 Realizado        20               40          20

(Instead of spaces, I used dots in variable's names.) After that, it's easy using melt() from theplyr package and facet_wrap:

library(ggplot2)
library(plyr)

m=melt(df)
ggplot(m,aes(x=factor(Grupo),y=value,fill=factor(variable))) + 
  geom_bar(position="fill", stat="identity") +
  scale_y_continuous(labels  = percent, 
                     breaks=c(0.2,0.4,0.6,0.8,1)) + # you can set the breaks to whatever you want
  facet_wrap(~ Periodo)

Is this what you want?

Here is your (edited) data:

df = structure(list(Periodo = structure(c(1L, 1L, 2L, 2L, 3L, 3L), .Label = c("Ene-Abr.2009", 
"May-Ago.2009", "Sep-Dic.2009"), class = "factor"), Grupo = structure(c(1L, 
2L, 1L, 2L, 1L, 2L), .Label = c("Meta", "Realizado"), class = "factor"), 
    Formacion = c(40L, 35L, 35L, 34L, 30L, 20L), En.consolidacion = c(30L, 
    45L, 35L, 45L, 30L, 40L), Consolidado = c(30L, 20L, 30L, 
    20L, 40L, 20L)), .Names = c("Periodo", "Grupo", "Formacion", 
"En.consolidacion", "Consolidado"), class = "data.frame", row.names = c(NA, 
-6L))

这篇关于2个等级组与ggplot2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆