Interesting geom_area problem ...

27 views
Skip to first unread message

Geoff Russell

unread,
Nov 16, 2015, 8:19:21 PM11/16/15
to ggplot2
Consider the following ...

data.csv .....
Year,Category,Diff
1990,Elec,-1
1991,Elec,-2
1992,Elec,-3
1990,Land,-1
1991,Land,-2
1992,Land,-3
1990,Water,-1
1991,Water,-2
1992,Water,-3
1990,Sheep,0
1991,Sheep,4
1992,Sheep,9

library(ggplot2)
df=read.csv("data.csv",header=T)
ggplot(aes(x=Year,y=Diff),data=df)+geom_area(aes(fill=Category));

The (dummy) data represent changes in greenhouse emissions in a category.  ie. in 1992 there were 3 less tonnes of GHG generated by electricity and so on, 3 less from
Land Clearing and so on. Extra sheep added 9 extra tonnes by 1992.

I want the increases (positive numbers) to be stacked and shown above zero and the decreases to be stacked and shown above zero. So you 
can see every category and what's happened in that category. But the current geom combines them...

Any ideas?   I could split the data into two panels ... but that seems a bit clumsy.

Geoff Russell

unread,
Nov 16, 2015, 9:23:08 PM11/16/15
to ggplot2
Found a good answer ... split the data into positive and negative Categories and then
create two layers, one for the positive Categories and one for the Negative.
Reply all
Reply to author
Forward
0 new messages