user3390169
user3390169

Reputation: 1045

R ggplot2: Columns not stacking

I thought stacking columns was the default action under ggplot2 but that does not seem to be happening for my plot. I am trying to take two vectors (may or may not have the same length) and graph them in the same plot as stacked bars. Here is a simple example:

z1<-c(500, 300, 200, 100)
z2<-c(800, 100, 50)

names(z1)<-c("a", "b", "c", "d")
names(z2)<-c("a", "c", "e")

z1<-as.data.frame(z1)
z2<-as.data.frame(z2)

colnames(z1)<-"total"
colnames(z2)<-"total"

ggplot()+ 
    labs(x="", y="") + 
    theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(), 
    panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
    scale_y_continuous(labels=format_si()) +
    ggtitle("Test") +
    geom_bar(data=z1, aes(x=rownames(z1), y=total),position="identity",stat="identity",
    fill=rgb(red=200, green=0, blue=50, maxColorValue = 255)) +
    geom_bar(data=z2, aes(x=rownames(z2), y=total),position="identity",stat="identity",
    fill=rgb(red=0, green=200, blue=50, maxColorValue = 255))

Gives me:

enter image description here

As you can see, the a and c elements are in front of each other instead of stacked.

Upvotes: 1

Views: 551

Answers (2)

Sandipan Dey
Sandipan Dey

Reputation: 23109

Just try this:

df <- rbind(cbind(z1, type=rownames(z1), data='z1'), cbind(z2, type=rownames(z2), data='z2'))
ggplot(df, aes(type, total, fill=data)) + 
  geom_bar(stat="identity") +
  scale_fill_manual(values=c(rgb(red=200, green=0, blue=50, maxColorValue = 255), rgb(red=0, green=200, blue=50, maxColorValue = 255)))

enter image description here

Upvotes: 1

boshek
boshek

Reputation: 4416

This type of data organization would work better:

z1<-c(500, 300, 200, 100)
z2<-c(800, 100, 50)

names(z1)<-c("a", "b", "c", "d")
names(z2)<-c("a", "c", "e")

z1<-as.data.frame(z1)
z2<-as.data.frame(z2)

colnames(z1)<-"total"
colnames(z2)<-"total"

Add group (z1, z2) to the data

z1$Group <- "z1"
z2$Group <- "z2"

Add the rownames as a variable column

z1$rnm <- rownames(z1)
z2$rnm <- rownames(z2)

Bind these together

zt <- rbind(z1, z2)

A much simplified plot

ggplot(zt, aes(x=rnm, y=total, fill=Group)) +
  geom_bar(stat="identity") 

At its core here, you need to understand aesthetics and what type of data is most efficient with ggplot2. Having separate calls for each group/data ignores the power of a factor variable with several levels. For example, experiment with swapping Group for rnm in the example.

Upvotes: 1

Related Questions