Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting different types of bar graph ggplot

Tags:

r

ggplot2

I'm trying to plot a bar graph of this data

The R script I have written so far is as follows:

library(ggplot2)
f<-read.table("Coverage_test", sep="\t", header=TRUE)
f$Coverage <- factor(f$Coverage, levels=unique(as.character(f$Coverage)))
g = ggplot(data=f, aes(x=Coverage, y=Variable_counts, group=Form, fill=Type)) 
+ geom_bar(position="dodge", stat="identity", colour="black") 
+ facet_grid( ~ Sample_name, scales="free") + opts(title = "Coverage", axis.text.x = theme_text(angle = 90, hjust = 1, size = 8, colour = "grey50")) 
+ ylab("Number of variables") + scale_fill_hue() + scale_y_continuous(formatter="comma")
ggsave("Figure_test_coverage.pdf")

The output of this code is as follows:

enter image description here

My question is: Is there a way to show differences in the behavior of graph based two variables. Each x-axis variable has four bars. I've already chosen to fill the color by 'Type', this shows how different 'Type' (one variable) behaves in my data. But I also want to show how the variable 'Form' behaves in my data. I have grouped them in my code 'group=Form' but can't differentiate it in the actual graph (visually). This can be done in line plots by showing different colors for one variable and different linetypes(solid and dashed) for the other variable. Something like below: enter image description here.

I want to know if the 'Form' variable can be shown by different color or atleast can be named below their respective bars or anything that is possible? Any help is greatly appreciated.

Thank you.

like image 873
smandape Avatar asked Dec 03 '25 02:12

smandape


1 Answers

I think you want something like this :

ggplot(data=dat, aes(x=Coverage, 
                        y=Variable_counts, 
                         group=interaction(Form,Type), 
                         fill=interaction(Form,Type))) +
 geom_bar(position="dodge", stat="identity", colour="black")

enter image description here

EDIT

Here I would lattice because of barchart and samrt formula notation. For fun I use a ggplot like theme using latticeExtra.

library(latticeExtra)
barchart(Variable_counts~Coverage|Sample_name,
         groups=interaction(Type,Form),
         data=dat,stack=F,auto.key=list(columns = 4),
         par.settings = ggplot2like(),
         axis = axis.grid,
         between=list(x=2))

enter image description here

like image 198
agstudy Avatar answered Dec 04 '25 22:12

agstudy