[R] How to label percentage values inside stacked bar plot using R-base

Jim Lemon jim at bitwrit.com.au
Mon Feb 18 11:51:24 CET 2013


On 02/18/2013 06:05 PM, nay-nancy Laiser wrote:
> Hello, I am new to R. I would like others to explain to me how to add
> absolute values inside the individual stacked bars in a consistent way
> using the basic R plotting function (R base). I tried to plot a stacked bar
> graph using R base but the values appear in an inconsistent/illogical way
> in such a way that its supposed to be 100% for each village but they don't
> sum up to 100%.
> Here is the data that am working on:
>
> Village      100        200         300    400        500
>
> Male    68.33333    53.33333    70    70       61.66667
>
> Female  31.66667   46.66667    30   30        38.33333
>
> In summary, there are five villages and the data showing the head of
> household interviewed by sex.
>
> I have used the following command towards plotting the graph:
> barplot(mydata,col=c("yellow","green")
> x<-barplot(mydata,col=c("yellow","green")
> text(x,mydata,labels=mydata,pos=3,offset=.5)
>
> Please help to allocate the correct values in each bar
> Thanks
> Nay
>
Hi Nay,
You can label the plot you have done above like this:

mydata<-matrix(c(100,200,300,400,500,
  68.33333,53.33333,70,70,61.66667,
  31.66667,46.66667,30,30,38.33333),ncol=3)
colnames(mydata)<-c("Village","Male","Female")
xpos<-barplot(mydata,col=c("yellow","green"))
library(plotrix)
barlabels(rep(xpos,each=5),
  apply(mydata,2,cumsum)-mydata/2,
  labels=mydata,prop=1,cex=0.7)

However, the plot is pretty redundant and you might want to consider 
other ways of presenting the data.

Jim



More information about the R-help mailing list