Re: Stacked & Facet Wrap Barplots in R Using ggplot2

1,059 views
Skip to first unread message

Roman Luštrik

unread,
Feb 11, 2013, 4:39:04 PM2/11/13
to skhd...@gmail.com, ggp...@googlegroups.com
What have you tried?


Cheers,
Roman



On Mon, Feb 11, 2013 at 9:31 PM, <skhd...@gmail.com> wrote:
Hi there,

I'm having a difficult time setting up stacked barplots using ggplot2.  I would like to do two things:

1. Create a single stacked barplot in which the horizontal axis is "year" and the vertical axis is "amount" stacked by the "donor" category.

2. Create a facet wrap barplot using the "donor" category as the facet type

Here is my data:

year <- c(rep(c(1929:1978), each=9))
donor <- c(rep(c("USSR","US","Germany","China","Japan","UN","WB","ADB","UK"), each=50))
amount <- c(  
  82000000, 47000000, 59000000, 77000000, 48000000, 59000000, 19000000, 73000000, 81000000,
  68000000, 6000000, 42000000, 84000000, 35000000, 44000000, 1000000, 1000000, 68000000,
  19000000, 1000000, 36000000, 91000000, 16000000, 63000000, 77000000, 65000000, 11000000,
  88000000, 35000000, 86000000, 60000000, 66000000, 52000000, 95000000, 50000000, 1000000,
  42000000, 3000000, 95000000, 2000000, 5000000, 3000000, 63000000, 37000000, 89000000,
  30000000, 31000000, 71000000, 80000000, 54000000, 32000000, 21000000, 67000000, 86000000,
  75000000, 25000000, 70000000, 54000000, 59000000, 90000000, 80000000, 4000000, 98000000,
  93000000, 34000000, 46000000, 26000000, 20000000, 62000000, 90000000, 87000000, 51000000,
  84000000, 58000000, 8000000, 72000000, 52000000, 44000000, 50000000, 17000000, 23000000,
  73000000, 83000000, 51000000, 7000000, 74000000, 14000000, 39000000, 67000000, 10000000,
  50000000, 94000000, 48000000, 2000000, 54000000, 11000000, 15000000, 89000000, 20000000,
  5000000, 8000000, 79000000, 7000000, 39000000, 51000000, 24000000, 63000000, 96000000,
  2000000, 59000000, 49000000, 4000000, 21000000, 20000000, 87000000, 63000000, 13000000,
  15000000, 16000000, 9000000, 26000000, 70000000, 82000000, 4000000, 73000000, 4000000,
  100000000, 65000000, 24000000, 92000000, 90000000, 33000000, 46000000, 70000000, 48000000,
  23000000, 93000000, 79000000, 100000000, 74000000, 26000000, 22000000, 30000000, 96000000,
  20000000, 61000000, 88000000, 94000000, 18000000, 29000000, 35000000, 49000000, 58000000,
  98000000, 13000000, 100000000, 75000000, 52000000, 93000000, 3000000, 96000000, 8000000,
  21000000, 27000000, 6000000, 89000000, 28000000, 99000000, 5000000, 63000000, 1000000,
  94000000, 58000000, 37000000, 92000000, 61000000, 85000000, 39000000, 90000000, 51000000,
  10000000, 61000000, 13000000, 68000000, 63000000, 68000000, 2000000, 57000000, 69000000,
  71000000, 89000000, 2000000, 47000000, 62000000, 24000000, 77000000, 32000000, 89000000,
  12000000, 16000000, 82000000, 50000000, 67000000, 75000000, 60000000, 24000000, 95000000,
  32000000, 97000000, 38000000, 33000000, 9000000, 95000000, 20000000, 3000000, 49000000,
  75000000, 83000000, 87000000, 38000000, 37000000, 87000000, 5000000, 15000000, 74000000,
  64000000, 1000000, 16000000, 87000000, 69000000, 54000000, 33000000, 26000000, 79000000,
  3000000, 15000000, 62000000, 21000000, 43000000, 81000000, 16000000, 46000000, 46000000,
  26000000, 79000000, 47000000, 42000000, 1000000, 65000000, 23000000, 7000000, 96000000,
  48000000, 13000000, 18000000, 18000000, 54000000, 23000000, 81000000, 65000000, 97000000,
  29000000, 53000000, 9000000, 80000000, 55000000, 15000000, 34000000, 39000000, 92000000,
  21000000, 94000000, 21000000, 30000000, 78000000, 37000000, 68000000, 92000000, 47000000,
  10000000, 65000000, 8000000, 31000000, 33000000, 39000000, 58000000, 89000000, 85000000,
  32000000, 84000000, 90000000, 99000000, 9000000, 5000000, 87000000, 85000000, 41000000,
  93000000, 14000000, 74000000, 50000000, 18000000, 4000000, 78000000, 16000000, 31000000,
  63000000, 59000000, 77000000, 36000000, 65000000, 25000000, 34000000, 18000000, 20000000,
  14000000, 94000000, 59000000, 63000000, 38000000, 13000000, 99000000, 50000000, 6000000,
  14000000, 41000000, 55000000, 26000000, 17000000, 5000000, 88000000, 63000000, 19000000,
  99000000, 42000000, 30000000, 2000000, 90000000, 76000000, 77000000, 61000000, 28000000,
  10000000, 63000000, 98000000, 36000000, 38000000, 87000000, 38000000, 65000000, 15000000,
  36000000, 37000000, 54000000, 10000000, 88000000, 24000000, 76000000, 20000000, 81000000,
  8000000, 98000000, 85000000, 27000000, 85000000, 14000000, 38000000, 54000000, 79000000,
  1000000, 66000000, 8000000, 92000000, 89000000, 60000000, 61000000, 98000000, 80000000,
  96000000, 18000000, 9000000, 57000000, 75000000, 36000000, 59000000, 87000000, 12000000,
  1000000, 8000000, 64000000, 24000000, 91000000, 57000000, 18000000, 12000000, 40000000,
  33000000, 34000000, 92000000, 1000000, 86000000, 52000000, 71000000, 49000000, 20000000,
  76000000, 91000000, 91000000, 16000000, 21000000, 43000000, 81000000, 56000000, 41000000,
  36000000, 21000000, 60000000, 56000000, 29000000, 36000000, 43000000, 52000000, 45000000,
  81000000, 43000000, 90000000, 100000000, 89000000, 41000000, 43000000, 46000000, 100000000,
  65000000, 94000000, 86000000, 13000000, 36000000, 52000000, 20000000, 57000000, 8000000,
  73000000, 48000000, 26000000, 61000000, 18000000, 43000000, 99000000, 39000000, 25000000)

df <- data.frame(year, donor, amount)  

I have tried the following sets of code in trying to create a stacked barplot or facet wrap barlot and received the following errors:

ggplot(amount, aes(year, amount, fill=factor(donor)),legend=FALSE) + geom_bar(binwidth=1.0)
Error: ggplot2 doesn't know how to deal with data of class numeric

ggplot(df, aes(year, amount, fill=factor(donor)),legend=FALSE) + geom_bar(binwidth=1.0)
Error in pmin(y, 0) : object 'y' not found

ggplot(df,aes(year),fill=donor) + geom_bar(aes(colour = donor, fill= donor), stat="identity",position = 'stack',binwidth=1)
Error in pmin(y, 0) : object 'y' not found

ggplot(amount, aes(x=year, fill=donor),legend=FALSE) + geom_bar(binwidth=1.0)
Error: ggplot2 doesn't know how to deal with data of class integer

ggplot(df, aes(x=year, y=amount, fill=donor),legend=FALSE) + geom_bar(binwidth=1.0)
Error in pmin(y, 0) : object 'y' not found

ggplot(df, aes(x=year, y=amount,fill=factor(donor))) + geom_bar(binwidth=1.0) + facet_wrap(~donor)
Error in pmin(y, 0) : object 'y' not found
 

Any help would be very much appreciated.


--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2
 
---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
In God we trust, all others bring data.

adam.l...@pnc.com

unread,
Feb 11, 2013, 5:51:40 PM2/11/13
to skhd...@gmail.com, ggp...@googlegroups.com
Hey, here's two ways to do it:
ggplot(df,aes(year,amount,colour=donor,fill=donor))+geom_bar(position="stack",stat="identity")
ggplot(df,aes(year,amount,colour=donor,fill=donor))+stat_summary(fun.y="sum",geom="bar",position="stack")


And the corresponding facet wraps for each one:
ggplot(df,aes(year,amount,colour=donor,fill=donor))+geom_bar(position="stack",stat="identity")+facet_wrap(~donor)
ggplot(df,aes(year,amount,colour=donor,fill=donor))+stat_summary(fun.y="sum",geom="bar",position="stack")+facet_wrap(~donor)



Adam Loveland



From: skhd...@gmail.com
To: ggp...@googlegroups.com
Date: 02/11/2013 04:38 PM
Subject: Stacked & Facet Wrap Barplots in R Using ggplot2
Sent by: ggp...@googlegroups.com



The contents of this email are the property of PNC. If it was not addressed to you, you have no legal right to read it. If you think you received it in error, please notify the sender. Do not forward or copy without permission of the sender. This message may contain an advertisement of a product or service and thus may constitute a commercial electronic mail message under US Law. The postal address for PNC is 249 Fifth Avenue, Pittsburgh, PA 15222. If you do not wish to receive any additional advertising or promotional messages from PNC at this e-mail address, click here to unsubscribe. https://pnc.p.delivery.net/m/u/pnc/uni/p.asp
By unsubscribing to this message, you will be unsubscribed from all advertising or promotional messages from PNC. Removing your e-mail address from this mailing list will not affect your subscription to alerts, e-newsletters or account servicing e-mails.


Sk Hd

unread,
Feb 11, 2013, 6:20:13 PM2/11/13
to adam.l...@pnc.com, ggp...@googlegroups.com
Thanks very much for the response.  It looks like your code doesn't actually stack the bars for the years that correspond with the amounts.  I'm looking for something similar to this (as unattractive as this is!), and for a facet wrap barplot with the amounts corresponding to their associated years.


Sk Hd

unread,
Feb 11, 2013, 6:22:14 PM2/11/13
to adam.l...@pnc.com, ggp...@googlegroups.com
The chart analogue in Excel might not have pasted correctly.  See attached.
Chart.png

Dennis Murphy

unread,
Feb 11, 2013, 6:59:54 PM2/11/13
to Sk Hd, ggp...@googlegroups.com
Hi:

On Mon, Feb 11, 2013 at 3:22 PM, Sk Hd <skhd...@gmail.com> wrote:
The chart analogue in Excel might not have pasted correctly.  See attached.


On Mon, Feb 11, 2013 at 6:20 PM, Sk Hd <skhd...@gmail.com> wrote:
Thanks very much for the response.  It looks like your code doesn't actually stack the bars for the years that correspond with the amounts.  I'm looking for something similar to this (as unattractive as this is!), and for a facet wrap barplot with the amounts corresponding to their associated years.

That's because of the way you defined the year vector - it should just have been 1929:1978. With that fixed and the result stored in a data frame DF (so as not to clash with the function df(), the pdf of the F distribution), sorted by year and country name within year:

library(ggplot2)
library(plyr)
library(grid)

DF <- arrange(DF, year, donor)  # plyr::arrange

# Stacked bar plot
ggplot(DF, aes(x = year, y = amount/1e8, fill = donor)) +
    geom_bar(stat = "identity", position = "stack") +
    ylab("Amount ($100 million)") +
    scale_fill_brewer(palette = "Set1")

# faceted by donor using line as the geometry
ggplot(DF, aes(x = year, y = amount/1e6)) +
    geom_line(size = 1, color = "firebrick") +
    ylab("Amount ($1 million)") +
    facet_wrap(~ donor, ncol = 3) +
    theme(panel.margin = unit(0.5, "cm"))

# ditto using bar as the geometry
ggplot(DF, aes(x = year, y = amount/1e6)) +
    geom_bar(stat = "identity", fill = "firebrick") +
    ylab("Amount ($1 million)") +
    facet_wrap(~ donor, ncol = 3) +
    theme(panel.margin = unit(0.5, "cm"))

Dennis

John Edwards (je_syd)

unread,
Feb 11, 2013, 7:01:28 PM2/11/13
to ggp...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages