Stack and Dodge using geom_bar

36 views
Skip to first unread message

David Guy

unread,
Nov 1, 2013, 9:28:49 AM11/1/13
to ggp...@googlegroups.com
Good Morning,

I have the following issue: I am trying to place two stacked bars next to each other over the same tick mark. In other words, I have a stack and dodge objective. What I get is that the second stacked bar is placed on top of the first one. 

I tried various approaches to using position="dodge" but no cigar so far. 

The following program is self contained and I attach the resulting plot.

Thank You

David

df <- data.frame(Variable=c(paste(rep(LETTERS[1],4),"a",sep=""),
                            paste(rep(LETTERS[1],3),"b",sep=""),
paste(rep(LETTERS[2],5),"a",sep=""),
paste(rep(LETTERS[2],2),"b",sep=""),
paste(rep(LETTERS[1],4),"a",sep=""),
                            paste(rep(LETTERS[1],3),"b",sep=""),
paste(rep(LETTERS[2],5),"a",sep=""),
paste(rep(LETTERS[2],2),"b",sep="")),
                 Date=c(rep(c("1990","1991"),14)))
 
df$Date<-as.Date(df$Date,"%Y")

    library (ggplot2)

    ggplot () +
    geom_bar (
        data = df[grep ('^A', df$Variable),],
        aes (
            x    = factor (format (Date, '%Y')),
            fill = factor (Variable)
        ),
        width  =  0.5
    ) +
    geom_bar (
        data = df[grep ('^B', df$Variable),],
        aes (
   x=factor (format (Date, '%Y')),
            fill = factor (Variable)
        ),
        width  =  0.2
    )

StacknDodge.png
Reply all
Reply to author
Forward
0 new messages