How to create "back-to-back" bar chart

296 views
Skip to first unread message

Greg Blevins

unread,
Jul 4, 2013, 11:02:41 PM7/4/13
to ggp...@googlegroups.com
    df <- structure(list(q9_1recodet = c(1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 
    0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1), q9_2recodet = c(1, 
    0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 
    0, 0, 0, 0, 1), q9_3recodet = c(1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 
    0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1)), .Names = c("q9_1recodet", 
    "q9_2recodet", "q9_3recodet"), row.names = c(1L, 2L, 3L, 4L, 
    5L, 6L, 8L, 9L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 
    20L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L), class = "data.frame", na.action = structure(c(7L, 10L, 21L), .Names = c("7", "10", "21"), class = "omit"))

I would like to create a graph similar to the image shown here -- this type of graph is frequently used by the Pew Research center. 



I manipulate the data as follows to get summary measures:

    df <- melt(df)
    a <- with(df, ftable(xtabs(~ value + variable)))
    a <- round(prop.table(a,2) * 100, 0)
    a <- as.data.frame(a)

My summary data looks like this:

      value    variable Freq
    1     0 q9_1recodet   78
    2     1 q9_1recodet   22
    3     0 q9_2recodet   74
    4     1 q9_2recodet   26
    5     0 q9_3recodet   67
    6     1 q9_3recodet   33

From here I can generate a standard bar plot as follows:

    g2 <- ggplot(a, aes( x = variable, y = Freq, fill = value, width=0.9)) +
    geom_bar(stat ="identity", position ="dodge") +
    geom_text(aes(label = Freq), vjust = -0.4, colour ="black", position = position_dodge(.9), size = 4) 
    g2 + coord_cartesian(ylim = c(1,100)) 

But, at this point I am stumped on how to proceed to generate a "back-to-back" bar plot like the Pew example shown above. Hence, I would like the light blue bars in the bar chart above to display to the right and the red bars to display to the left.  Any help would be much appreciated.

Thanks,

Greg

Stuart Luppescu

unread,
Jul 5, 2013, 12:42:43 AM7/5/13
to ggp...@googlegroups.com
On 鐃緒申, 2013-07-04 at 20:02 -0700, Greg Blevins wrote:
> I am stumped on how to proceed to generate a
> "back-to-back" bar plot like the Pew example shown above. Hence, I
> would like the light blue bars in the bar chart above to display to
> the right and the red bars to display to the left. Any help would be
> much appreciated.

I had a similar problem a week or so ago. I wanted to center bars at a
certain point. The solution is to separate the data set into two and
multiply one subset by -1. Then use two geom_bar() statements. Here is
some of the code I used (I'm sure you can modify it to get it to do what
you want):

ggplot(testratings[testratings$rating>=3,], aes(x=fake.schlid,
y=percent, fill=rating.c)) +
geom_bar(stat="identity") +
scale_y_continuous(name="Percentage", limits=c(-100, 100)) +
geom_bar(data=testratings[testratings$rating<3,], aes(x=fake.schlid,
y=(-1)*percent, fill=rating.c), stat="identity") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
scale_fill_manual(name="Rating", values=rev(mypal)) +
labs(title="Percentages of Ratings by School", x="School ID")
--
Stuart Luppescu <s...@ccsr.uchicago.edu>

Reply all
Reply to author
Forward
0 new messages