[R] ggplot/ barplot

arun smartpink111 at yahoo.com
Fri Feb 28 07:37:25 CET 2014


Hi Farnoosh,

YOu can try:
 DataA$percent <- with(DataA,round((Var2/sum(Var2))*100,2))

library(ggplot2)
 ggplot(DataA,aes(x=Var1,y=percent))+geom_bar(stat="identity",aes(fill=Var1))+geom_text(label=paste0(DataA$percent,"%"),vjust=-0.2,size=4)


A.K.



On Thursday, February 27, 2014 5:02 PM, farnoosh sheikhi <farnoosh_81 at yahoo.com> wrote:

Hi Arun,

I hope all is well.
I have a data set like below. I want to get a barplot by ggplot function in R that can change the color of the bins and also shows the percentage of each category on the bin too.
Var1=Group
Var2=Frequencies 
Percentage can be calculated as follow: (DataA$Var2/sum(DataA$Var2))*100

DataA <- read.table(text="ID,Var1,Var2
1,A,100
1,B,58
                    2,C,200
                    2,D,125
                    2,E,250",sep=",",header=TRUE,stringsAsFactors=FALSE)


Thank you so much for your help and time.



Regards, Farnoosh Sheikhi



More information about the R-help mailing list