rtmex
rtmex

Reputation:

2 level groups with ggplot2

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:

http://imageshack.us/photo/my-images/90/efk6.png/

Note that the graph has two level groups.

Upvotes: 1

Views: 2012

Answers (2)

jakub
jakub

Reputation: 5104

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?

enter image description here

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

Upvotes: 3

maj
maj

Reputation: 2529

I could not find an example exactly like yours, but I can give you a few ideas that might help you on your way there:

To group related bars, the ggplot2 package offers two possibilities:

  1. Position two related bars next to each other, using the geom_bar(position="dodge") command, like here: http://www.cookbook-r.com/Graphs/Bar_and_line_graphs_(ggplot2)/#bar-graphs

  2. Group related bars under a common headline, using facet_grid(...) (in cookbook-r, you can find more info about that under Index >> Graphs >> Facets(ggplot2)).

If you should decide to use ggplot2, you may want to try example(geom_bar), which will also give you some examples as to how stacked plots can be created (e.g. using position="fill", which will give you a stacked bar chart like the one above).

Unfortunately, I don't know how to handle data as complex as yours in combination with plotting. But the reshape package has helped me a lot whenever I needed to transform my data, e.g. using the melt() function, as it is used here: http://www.cookbook-r.com/Manipulating_data/Converting_data_between_wide_and_long_format/

Hope I could help a little. Feel free to give me some feedback to my answer.

Markus

Upvotes: 0

Related Questions