Wednesday, August 3, 2016

Making grouped barplot in ggplot2

Lets say you want to make a simple grouped barplot in R
using the data

cat for.R.plot
0.12 TF1-a
0.10 TF1-b
0.78 TF2-a
0.67 TF2-b
0.39 TF3-a

0.34 TF3-b

load ggplot2 package

library(ggplot2)


Read data

x<-read.delim("for.R.plot", header=F,sep=" ")

Add column with group names

x$V3<- c("TF1","TF1","TF2","TF2","TF3","TF3")


Plot in ggplot

pdf("output.pdf")
> ggplot(data=x, aes(x=V2,y=V1,fill=V3)) + scale_fill_manual(values=c("purple", "blue", "darkgreen")) +geom_bar(stat = "identity") +labs(title="Title of graph") + xlab("xlab title") + ylab("ylab title")+ theme(axis.text.x = element_text(colour="grey20",size=15,angle=45,hjust=.5,vjust=.5,face="plain"),axis.text.y = element_text(colour="grey20",size=14),axis.title.x = element_text(colour="grey20",size=14),axis.title.y = element_text(colour="grey20",size=14),plot.title = element_text(size=15))+theme(legend.title=element_blank())
> dev.off()


Output file:




No comments:

Post a Comment