Hm, poking at this a little... I actually like the two-sided bar
charts as a visualization with the surplus/deficit in the middle.
Here's what I came up with as a first stab:
df <- expand.grid(year=seq(1999,2008),
type=c("Commodities", "Intermediate", "Final Products",
df$Value = runif(nrow(df), 5, 30) * ifelse(df$dir=="Imports", -1, 1)
p <- ggplot(df, aes(x=year, y=Value, fill=type)) +
geom_bar(stat="identity", data=subset(df, dir=="Imports")) +
geom_bar(stat="identity", data=subset(df, dir=="Exports"))
df.m <- melt(df, id=c("type", "year", "dir"))
df.trade <- cast(df.m, year ~ ., sum)
names(df.trade) <- c("year", "Value")
p <- p + geom_line(data=df.trade, aes(fill=NULL), color="black",
The aes(fill=NULL) is required to prevent a decidedly unhelpful eval
error. For reasons I don't yet understand, the line geom crashes if
you don't override the fill=type aesthetic. And there's no legend for
the Trade line. This may be a case where, if you're generating graphs
for publication, some post-hoc editing may be valuable...
Also, the code gives the following warning, despite working fine:
Stacking not well defined when ymin != 0
Anyone know why it would complain in this case?
On Sep 24, 8:48 am, Andreas Christoffersen <achristoffer...@gmail.com>