[R] how to show percentage of individuals for two groups on histogram?

Jim Lemon drj|m|emon @end|ng |rom gm@||@com
Sat May 23 02:03:55 CEST 2020


Hi Ana,
I think this is what you want in the panel style of plot. Let me know
if not, or if I have calculated the wrong percentages. The overlaid
histograms definitely use a different calculation.

amsdf<-read.table("pheno_m1_plot",header=TRUE,stringsAsFactors=FALSE)
dim(amsdf)
# find the right breaks for your "cut"
casen<-table(cut(amsdf$HBA1C[amsdf$pheno==2],breaks=3:14))
controln<-table(cut(amsdf$HBA1C[amsdf$pheno==1],breaks=3:14))
# save yourself some typing
HBA1C2<-amsdf$HBA1C[amsdf$pheno==2]
HBA1C1<-amsdf$HBA1C[amsdf$pheno==1]
ncases<-length(HBA1C2)
ncontrols<-length(HBA1C1)
split.screen(matrix(c(0,1,0.6,1,0,1,0,0.6),nrow=2,byrow=TRUE))
par(mar=c(0,4,1,2))
barpos=barplot(100*casen/ncases,names.arg=NA,col="orange",
 space=0,ylab="Percentage",xaxt="n",ylim=c(0,27))
case_text<-sprintf(
 "Cases: n=%d, nulls=%d, median=%.1f, mean=%.1f, sd=%.1f",
 length(HBA1C2),sum(is.na(HBA1C2)),round(median(HBA1C2,na.rm=TRUE),1),
 round(mean(HBA1C2,na.rm=TRUE),1),round(sd(HBA1C2,na.rm=TRUE),1))
text(mean(barpos),25,case_text)
box()
screen(2)
par(mar=c(4,4,0,2))
barplot(100*controln/ncontrols,names.arg=NA,
 space=0,ylab="Percentage",col="orange",ylim=c(0,34))
control_text<-sprintf(
 "Cases: n=%d, nulls=%d, median=%.1f, mean=%.1f, sd=%.1f",
 length(HBA1C1),sum(is.na(HBA1C1)),round(median(HBA1C1,na.rm=TRUE),1),
 round(mean(HBA1C1,na.rm=TRUE),1),round(sd(HBA1C1,na.rm=TRUE),1))
text(mean(barpos),32,control_text)
box()
library(plotrix)
staxlab(1,at=barpos,labels=names(casen))

Jim

On Sat, May 23, 2020 at 9:01 AM Ana Marija <sokovic.anamarija using gmail.com> wrote:
>
> Hi Jim,
>
> My data is attached. It is most kind of you for looking into this!
>
> Cheers,
> Ana
>
> On Fri, May 22, 2020 at 5:49 PM Jim Lemon <drjimlemon using gmail.com> wrote:
> >
> > Hi Ana,
> > As I had very little idea what your data looked like, what I made up
> > obviously didn't fit in the plot that well. If you can send the data I
> > can make a better attempt. The other thing is whether you want a plot
> > with two adjacent panels (what I sent) or overlaid histograms (what
> > Eric sent). Let me know.
> >
> > Jim
> >
> > On Sat, May 23, 2020 at 12:45 AM Ana Marija <sokovic.anamarija using gmail.com> wrote:
> > >
> > > HI Jim,
> > >
> > > Thank you so much for getting back to me I tried your codes and I got
> > > this in attach,
> > > I think the issue is in calculating percentage per groups (cases or controls)
> > > ...
> > > I can send you the whole dataset if you would like to try with it
> > > On Thu, May 21, 2020 at 11:14 PM Jim Lemon <drjimlemon using gmail.com> wrote:

-------------- next part --------------
A non-text attachment was scrubbed...
Name: ams1.png
Type: image/png
Size: 18251 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20200523/ee75ea08/attachment.png>


More information about the R-help mailing list