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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With