How to label percentage values inside stacked bar plot using R-base -
How to label percentage values inside stacked bar plot using R-base -
this question has reply here:
how label percentage values within stacked bar plot using r-base 1 replyi new r. others explain me how add together absolute values within individual stacked bars in consistent way using basic r plotting function (r base). tried plot stacked bar graph using r base of operations values appear in inconsistent/illogical way in such way supposed 100% each small town don't sum 100%. here info 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 5 villages , info showing head of household interviewed sex.
i have used next command towards plotting 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 allocate right values in each bar thanks
this started comment seemed unfair not turn answer. reply question (even on stack overflow) need know how "mydata" structured. assumed @ first info frame 5 rows , 2 or 3 columns in case code makes no sense. however, if were how structured here 1 way think want:
mydata <- data.frame( row.names =c(100, 200, 300, 400, 500), male =c(68.33333, 53.33333, 70, 70, 61.66667), female =c(31.66667, 46.66667, 30, 30, 38.33333)) x <- barplot(t(as.matrix(mydata)), col=c("yellow", "green"), legend=true, border=na, xlim=c(0,8), args.legend= list(bty="n", border=na), ylab="cumulative percentage", xlab="village number") text(x, mydata$male-10, labels=round(mydata$male), col="black") text(x, mydata$male+10, labels=100-round(mydata$male)) which produces following:
an alternative set y value 40 male text labels, , 80 females - have advantage of less confusing jitter of labels, , disadvantage text vertical position no longer notionally attached data.
personally, don't much barplot @ all, although there many far worse crimes against info visualisation straightforward bar plot. numbers on plots cluttering , detract visual impact of actual mapping of info colours, shapes , sizes. i'd rather simple dot plot like:
library(ggplot2) ggplot(mydata, aes(x=row.names(mydata), y=male)) + geom_point(size=4) + coord_flip() + labs(x="village number\n", y="percentage male") + ylim(0,100) + geom_hline(yintercept=50, linetype=2) which gives
there less redundant clutter in plot, higher info ink ratio, etc. in end need produce plot mean audience.
r
Comments
Post a Comment