Ggplot2 : Plotting Multiple Bar Against Same Y Axis. Visualization Of Samples!
2
0
Entering edit mode
11.6 years ago
k.nirmalraman ★ 1.1k

Dear All,

I would like to plot a multiple bar plot as shown below against the same y axis. So, I can observe the trend across different samples.

a<-c(rep("C1",3),rep("C2",5),rep("C4",4))
des<-c("ab","cd","de","ab","cd","de","ef","fg","ab","bc","cd","de")
count<-c(23:34)
x<-data.frame(a,des,count)
ggplot(x,aes(x=des,y=count,fill=a))+geom_bar(position="fill")+coord_flip()

I get the following image with some warnings.

Output

Mapping a variable to y and also using stat="bin".
With stat="bin", it will attempt to set the y value to the count of cases in each group.
This can result in unexpected behavior and will not be allowed in a future version of ggplot2.
If you want y to represent counts of cases, use stat="bin" and don't map a variable to y.
If you want y to represent values in the data, use stat="identity".
See ?geom_bar for examples. (Deprecated; last used in version 0.9.2)

What I would like is to have multiple plots from different dataframes like x in above example and be able to plot them one next to each other such that the y axis is same... so comparison of such a trend across samples would be possible.

I am wondering,

  1. how to achieve the ordering in y axis and
  2. if a particular y axis element is not present in another dataframe, would it be possible to have a blank space in the corresponding bar space of the graph?

Any help will be very much appreciated. Thanks a lot in advance!

visualization • 13k views
ADD COMMENT
0
Entering edit mode

For aligning your plots, you will need to look at the gridExtra package and you can use +scale_x_discrete() to manually order your samples across the plots (what you are called the "y axis" is the x-axis because you used coord_flip() in your call to ggplot). I didn't test anything, but that should be a start. As far as the warnings, did you try +geom_bar(stat="identity")? The warnings give clear advice on what to do and where to get more information.

ADD REPLY
3
Entering edit mode
11.6 years ago
Ido Tamir 5.2k
x2 <- data.frame(x, batch="A")
x3 <- data.frame(x, batch="B")
x4 <- data.frame(x, batch="C")
comb <- rbind(x2,x3,x4)
comb2 <- comb[!(comb$des == "cd" & comb$batch == "B"), ]
ggplot(comb2,aes(x=des,y=count,fill=a)) + geom_bar(stat="identity", position="fill") + facet_grid( . ~ batch)
ggplot(comb2,aes(x=des,y=count,fill=a)) + geom_bar(stat="identity") + facet_grid( . ~ batch)

for reordering see: http://stackoverflow.com/questions/5208679/order-bars-in-ggplot2-bar-graph

ADD COMMENT
0
Entering edit mode

This is awesome!! Cant thank you enough!!

ADD REPLY
0
Entering edit mode

I couldnt succeed much with oredering from all my attempts.. My Bad!! would you be able to help me to achieve the following..

I would like the order of x2 based on des to be retained and then followed by those in x3 but not in x2 at the end and those in x4 but not in x2 & x3 .

At the least it would be great to retain the order of x2 and the rest followed by it! Now the order is based on alphabetical order.... (apparently the mock data is ordered alpahbetically)

Thanks a lot!!

ADD REPLY
0
Entering edit mode
11.6 years ago
k.nirmalraman ★ 1.1k

Just to document it here...

the ordering can be achieved with passing the argument for aes ggplot(comb2, aes(x=factor(Des,levels=unique(Des)),y=....

ADD COMMENT

Login before adding your answer.

Traffic: 2827 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6