[R] Help with customizing a histogram figure
Jim Lemon
jim at bitwrit.com.au
Fri Sep 17 11:21:20 CEST 2010
On 09/16/2010 11:40 PM, Josh B wrote:
> Hi all,
>
> Please consider the following code:
>
> require(plotrix)
>
> l<- list(rnorm(50),rnorm(50,sd=2),rnorm(50,mean=3))
> multhist(l)
>
> I have two things I need help with:
>
> (1) In the output, there are empty spaces on the x-axis. How would I eliminate
> these spaces? I want a nice, smooth, empty-spaceless x-axis.
>
> (2) How could I add tracing lines to each histogram? I am undoubtedly using the
> wrong terminology. What I want to do is something like this:
> http://addictedtor.free.fr/graphiques/RGraphGallery.php?graph=55
> Note that each histogram has a tracing line that summarizes the histogram.
>
> In summary, I'd like to modify the code above to (1) get rid of the dead spaces
> on the x-axis, and (2) add lines that traces to each of the histograms therein
> (all on the same graph).
>
> I am very bad at using R graphics at this point, so I need some pretty serious
> hand-holding. Exact code that gets the job done would be most useful for me.
> Thanks very much in advance!
>
Hi Josh,
You can get rid of the spaces like this:
multhist(l,space=c(0,0))
By "tracing lines" I think you mean smoothed densities of your three
samples. The addictedtor site seems to be down at the moment, so I can't
check the page suggested by Thomas and Jorge. This is a bit more
complicated, as the functions underlying multhist don't usually place
the bars at values corresponding to the intervals you need to calculate.
Perhaps if you use multhist to calculate the histograms:
lhist<-multhist(l,plot.it=FALSE)
Then plot the histogram with barp instead (barp places bars on integer
values by default and this happens to match your breakpoints):
barp(lhist$out,names.arg=seq(-5.5,4.5,by=1),
col=c("gray20","gray50","gray80")))
calculate the density:
l1dens<-density(l[[1]])
and then display the lines:
# notice the x offset and the rescaling of the density values
lines(l1dens$x+6.5,rescale(l1dens$y,c(0,20)))
repeating for the other two samples, you can get the plot you want.
Jim
More information about the R-help
mailing list