[R] multhist,labels and percentages

Federico Calboli f.calboli at imperial.ac.uk
Fri May 14 19:50:10 CEST 2010


On 14 May 2010, at 18:25, Thomas Stewart wrote:

> First, why doesn't the following code work?  What exactly is the error you are getting? 
> 
> par(mfrow=c(1,2))
> pop1<-rnorm(100)
> hist(pop1,freq=F,ylim=c(0,1))
> pop2<-rgamma(100,1,1)
> hist(pop2,freq=F,ylim=c(0,1))

Let's define 'work'. The code above works, it just utterly fails to do what I need to do, i.e. plot two distributions side by side on one single plot -- hence, I need to use multhist(), as the subject of my email might suggest...

> Second, help.search("multihist") returned the multi.hist function.  Is multi.hist [instead of multihist] the function you are working with?

I do not think so, I'm using multhist() is from library plotrix. I'll look at multi.hist, it might be more flexible.

> Third, it sounds like you want to change the scale of the y axis.  The scale change you want is linear, so why not change the scale manually? Something like this will manually change the scale to percentages:
> 
> pop1<-rnorm(156)
> hist.pop1<-hist(pop1,yaxt="n",ylab="%",main="")
> t<-axTicks(2)
> axis(2,at=t,labels=round(t/length(pop1)*100,1))

Excellent! that's what I need. Unfortunately, because the two pops are different sized I will have to cook up a second y-axis on the right of the plot for the second population
> 
> Then, again, changing the scale creates more of a bar chart than a histogram plot.  Maybe you want to look at the barchart functions directly.  Or maybe directly calculate the bin percentages and plot them directly.  

I agree, but it is quite a bit more work, which, given the issue at hand (unability to read a chart for some users), does not seem worth it. 

> I think you have a lot of options to plot what you want.

Your suggestion for the axis is excellent, thank you very much.

F


> 
> -tgs 
> 
> 
> On Fri, May 14, 2010 at 11:51 AM, Federico Calboli <f.calboli at imperial.ac.uk> wrote:
> On 14 May 2010, at 16:09, Thomas Stewart wrote:
> 
> > Please be more specific with your question.  Perhaps a simple subset of the data you are trying to plot?  Here is some non-specific advice:
> >
> > Plotting histograms as percentages instead of frequency counts is already an option of the hist function.  For example,
> >
> > pop1<-rnorm(100)
> > hist(pop1,freq=F)
> 
> What you get is a desity, not a percentage, so you could have two bars with a value greater 0.5 on the y-axis. The fact that the area sums up to 1 does not mean that the sum of the heights adds up to 1 --the thing that my ignoramus want to see to understand. On the other hand, freq =T gives the counts, and the sum of the counts is the population total -- therefore (bar counts)/(pop total) *100  is the precentage. If I could slap that on the label of the y-axis I'd be sorted.
> 
> >
> > If you are plotting two histograms side-by-side (on the percentage scale), the y-axis of both plots can be set with the ylim option.  For example,
> >
> > par(mfrow=c(1,2))
> > pop1<-rnorm(100)
> > hist(pop1,freq=F,ylim=c(0,1))
> > pop2<-rgamma(100,1,1)
> > hist(pop2,freq=F,ylim=c(0,1))
> 
> I'm using multhist(). The above would not work for me.
> 
> F
> 
> 
> 
> 
> >
> > If your question were clearer, I might be able to help in more specific ways.
> >
> > -tgs
> >
> > On Fri, May 14, 2010 at 10:19 AM, Federico Calboli <f.calboli at imperial.ac.uk> wrote:
> > Hi All,
> >
> > I am in the annoying position of having to present some data to someone who seems to be somewhat less than numerate. I need to label the y-axes of a multhist with the y-axis labeled not as counts but as percentage of a population. Plotting the standard histogram is in a way fine, all I need is to:
> >
> > -- have a left-handside y-axis labels for pop 1 and a right-handside y-axis labels for pop2
> > -- replace the counts in each axis with population percentages (easy to calculate, but how to stick them there?)
> >
> > Any suggestion would be gratefully received.
> >
> > F
> >
> >
> > --
> > Federico C. F. Calboli
> > Department of Epidemiology and Biostatistics
> > Imperial College, St. Mary's Campus
> > Norfolk Place, London W2 1PG
> >
> > Tel +44 (0)20 75941602   Fax +44 (0)20 75943193
> >
> > f.calboli [.a.t] imperial.ac.uk
> > f.calboli [.a.t] gmail.com
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
> 
> --
> Federico C. F. Calboli
> Department of Epidemiology and Biostatistics
> Imperial College, St. Mary's Campus
> Norfolk Place, London W2 1PG
> 
> Tel +44 (0)20 75941602   Fax +44 (0)20 75943193
> 
> f.calboli [.a.t] imperial.ac.uk
> f.calboli [.a.t] gmail.com
> 
> 
> 
> 
> 
> 
> 

--
Federico C. F. Calboli
Department of Epidemiology and Biostatistics
Imperial College, St. Mary's Campus
Norfolk Place, London W2 1PG

Tel +44 (0)20 75941602   Fax +44 (0)20 75943193

f.calboli [.a.t] imperial.ac.uk
f.calboli [.a.t] gmail.com



More information about the R-help mailing list