back-to-back bar charts

902 views
Skip to first unread message

Patrick Wustmann

unread,
Sep 24, 2009, 7:13:56 AM9/24/09
to ggp...@googlegroups.com
Hi folk,

does anyone know how to create a back-to-back bar chart with ggplot2?

For anyone who don't know what I am talking about, have a look on a
recent paper from the EU (sorry for posting a link, but i think its
best to see an example):

http://ec.europa.eu/agriculture/publi/map/02_09.pdf

I'd like to create plots like the graphs 5,6,18 in the paper.

Moreover due to space constraints I need to draw a second information
in one plot (see also the graphs in the paper). I know 2 y-axis plots
is not good but sometimes it is better to have eye catching plots with
lots of information in it rather than multiple plots :). Is there a
way to do such things with ggplot2?

Thank you very much.

Patrick

baptiste auguie

unread,
Sep 24, 2009, 7:31:33 AM9/24/09
to Patrick Wustmann, ggp...@googlegroups.com
Hi,

Would that be similar to these examples?

http://learnr.wordpress.com/2009/06/01/ggplot2-positioning-of-barplot-category-labels/

baptiste
--
_____________________________

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

http://newton.ex.ac.uk/research/emag
______________________________

Patrick Wustmann

unread,
Sep 24, 2009, 8:16:26 AM9/24/09
to baptiste auguie, ggp...@googlegroups.com
Hi,

thanks for the reply, No, not really unfortunately. These bars are not
back to back. I need to have two bars on one line 'pointing' in
opposite directions to depict a negative and a positive value on the
same time.

Patrick

2009/9/24 baptiste auguie <bapt...@googlemail.com>:

Harlan Harris

unread,
Sep 24, 2009, 8:25:13 AM9/24/09
to ggplot2
I'd think you'd want to use geom_rect (http://had.co.nz/ggplot2/
geom_rect.html) with a df like:

id year com.imp com.exp int.imp int.exp fin.imp fin.exp conf.imp
conf.exp bal

and position_stack(). Plus the geom_line() as a separate layer. Oh,
wait, you might have to manually add the data together -- I'm not sure
if position_stack() works with geom_rect() in the right way...

And yeah, I'd find some other way to do the plot than using a second Y
axis. Probably just a small (short but wide) graph of the second data
set on a separate graph, above or below the main graph. I'm not sure
why they're plotted together on Fig 18 anyway -- the two lines are not
very correlated once you get rid of the linear trend...

-Harlan


On Sep 24, 8:16 am, Patrick Wustmann <wustm...@tcd.ie> wrote:
> Hi,
>
> thanks for the reply, No, not really unfortunately. These bars are not
> back to back. I need to have two bars on one line 'pointing' in
> opposite directions to depict a negative and a positive value on the
> same time.
>
> Patrick
>
> 2009/9/24 baptiste auguie <bapt4...@googlemail.com>:
>
> > Hi,
>
> > Would that be similar to these examples?
>
> >http://learnr.wordpress.com/2009/06/01/ggplot2-positioning-of-barplot...
>
> > baptiste

Andreas Christoffersen

unread,
Sep 24, 2009, 8:48:21 AM9/24/09
to Patrick Wustmann, ggp...@googlegroups.com
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)

Harlan Harris

unread,
Sep 24, 2009, 11:45:05 AM9/24/09
to ggplot2
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)
>

Learning R Blog

unread,
Sep 24, 2009, 2:55:17 PM9/24/09
to ggplot2
I came up with pretty much the same solution - and made a quick
blogpost about it as well, also showing the faceting option.

http://learnr.wordpress.com/2009/09/24/ggplot2-back-to-back-bar-charts/

Patrick Wustmann

unread,
Oct 9, 2009, 1:06:34 PM10/9/09
to Learning R Blog, ggplot2
Thank you very much for that! Saved me a lot of time.

Patrick

2009/9/24 Learning R Blog <rle...@gmail.com>:

James Howison

unread,
Oct 11, 2009, 4:12:21 PM10/11/09
to ggplot2
btw, that is one awesome blog post. Thanks!
Reply all
Reply to author
Forward
0 new messages