Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stacked barplot crossing the x-axis [duplicate]

Friends, How do I create a stacked barplot on both sides of the x-axis (preferably in ggplot2) ?

Example: http://s23.postimg.org/3lbgicb3f/Example.png

I've searched around, but haven't been able to find any good examples. The data consists of two locations (1 and 2), with values (weight) for 5 different categories (A, B, C, R and S). A, B and C should on top of the x-axis, while R and S should be plotted below. Note the positive values on both sides of the x-axis. Never mind the error-bars.

Example data:

Type=c("A","B","C","R","S","A","B","C","R","S")
Location=c(1,1,1,1,1,2,2,2,2,2)
Value=c(2,6,5,3,2.5,6,3,2,4,1.5)
df=data.frame(Type, Location, Value)
df$Location <- as.factor(df$Location)

Any pointers would be much appreciated, Nordenskiold

like image 889
Nordenskiold Avatar asked Oct 19 '25 08:10

Nordenskiold


1 Answers

Here's another approach very similar to @BrodieG which does not require the creation of any new dataframes.

library(plyr)
library(ggplot2)
ggplot(df, aes(x=Location, fill=Type))+
  geom_bar(subset=.(Type %in% c("A","B","C")), aes(y=Value))+
  geom_bar(subset=.(Type %in% c("R","S")), aes(y=-Value))+
  geom_hline(yintercept=0, linetype=2)+
  scale_y_continuous(labels=abs)

like image 78
jlhoward Avatar answered Oct 20 '25 23:10

jlhoward