Stacked barplot crossing the x-axis [duplicate]
Asked Answered
O

2

4

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

O answered 6/3, 2014 at 21:40 Comment(0)
V
2

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)

Voluptuous answered 6/3, 2014 at 22:31 Comment(2)
+ 1 for the subset bit. I have a faint recollection of seeing this documented someplace, but can't figure out where. Also, feel a bit silly +1 after yours, and would've done so on the other post save for the overplotting, so this is as good a place to thank you for teaching me something new.Seritaserjeant
@Nordenskiold, if your question is answered please consider marking one of the answers as the answer. Thanks.Seritaserjeant
S
2

You can try:

df <- transform(df, Value=ifelse(as.character(Type) %in% c("R", "S"), -Value, Value))
df.split <- split(df, df$Value < 0)

ggplot() + 
  geom_bar(data=df.split[[1]], aes(x=Location, y=Value, fill=Type), stat="identity") +
  geom_bar(data=df.split[[2]], aes(x=Location, y=Value, fill=Type), stat="identity") +
  geom_hline(yintercept=0) +
  scale_y_continuous(labels=abs)

enter image description here

Here we need to split the data frame into positive and negative values, and then we use the label argument to scale_y_continous to make all the values positive on the y axis.

Seritaserjeant answered 6/3, 2014 at 22:0 Comment(0)
V
2

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)

Voluptuous answered 6/3, 2014 at 22:31 Comment(2)
+ 1 for the subset bit. I have a faint recollection of seeing this documented someplace, but can't figure out where. Also, feel a bit silly +1 after yours, and would've done so on the other post save for the overplotting, so this is as good a place to thank you for teaching me something new.Seritaserjeant
@Nordenskiold, if your question is answered please consider marking one of the answers as the answer. Thanks.Seritaserjeant

© 2022 - 2024 — McMap. All rights reserved.