-1

I'm new using R and ggplot2, I have looked some posts like this plot stacked bar plot in R but they are not what I want.

I have the next data

                           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 teh next stacked bar chart like the one in the next link http://imageshack.us/photo/my-images/90/efk6.png/

Community
  • 1
  • 1
rtmex
  • 1
  • 3
  • 2
    Welcome. The R section of Stack Overflow is an extremely valuable resource if used properly; there are many knowledgeable people here who are willing to help. HOWEVER you will get a more timely response if (a) you show what you have already tried i.e. if you show the code you have already written to prove that you have made an effort to solve the problem and failed (b) you make your question reproducible (DO read [this post](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for details). You've done neither, so I suggest you first improve your question. – SlowLearner Aug 07 '13 at 10:56
  • I can barely see the graph. Please make it larger. – Metrics Aug 07 '13 at 11:55
  • I have already changed the graphic for one with better quality, and I also added example code – rtmex Aug 07 '13 at 22:31
  • I cannot see any code and the graphic appears to be the same size. – SlowLearner Aug 08 '13 at 07:11
  • When I added the new graph and the sample code, the site showed me a message telling me that the change will be visible for others after someone reviewed, I think a moderator. But I don't know how long will it take, I guess I will have to wait – rtmex Aug 12 '13 at 15:03
  • 1
    Here's the better quallity graph again [1:] http://imageshack.us/photo/my-images/90/efk6.png – rtmex Aug 12 '13 at 21:09

1 Answers1

0

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))
jakub
  • 4,774
  • 4
  • 29
  • 46
  • I tryed to reproduce your example, but I get "Error: Labels and breaks must be same length" – rtmex Aug 26 '13 at 21:54
  • I have changed labels=percent by formatter = "percent" and that fixed – rtmex Aug 26 '13 at 22:40
  • ok, sorry for that :) I think it may have something to do with different version of ggplot2 (judging from the fact that I can run this code without an error). – jakub Aug 27 '13 at 18:36
  • Yes, I think so too, by the way, I had to add the parametter ncol = 3 to the facet_wrap(~ Periodo) to get the 3 periods on a single row, so the code is facet_wrap(~ Periodo, ncol=3). Thanks :) – rtmex Aug 29 '13 at 00:13