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",
"Confidential"),
dir=c("Imports", "Exports"))
df$Value = runif(nrow(df), 5, 30) * ifelse(df$dir=="Imports", -1, 1)
library("ggplot2")
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",
size=2)
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:
Warning message:
Stacking not well defined when ymin != 0
Anyone know why it would complain in this case?
-Harlan
On Sep 24, 8:48 am, Andreas Christoffersen <
achristoffer...@gmail.com>
wrote:
> Hi Patrick,
>
> Is it not simple a stacked barchart with imports being plottet as negative
> values? I dont have time to try it out in ggplot right now - but maybe
> later. Guess you have to use stat_identity someway.
>
> It's not easy to read though - that kind of chart. Maybe try with
> facet_grid(.~import_export)
>