Skip to content

multiple graphs on one plot

3 messages · Andrew Halford, Jim Lemon

#
Hi Listers

I've been trying to make a single graphic that has frequency histograms for
male and female mud crabs displayed side by side (such as when using the
beside=TRUE command for barplots). I then want to display a normal
distribution on top of the male and female histograms.

I have been using the multhist command in Plotrix to generate the
histograms without too much problem, but I cannot get the normal
distributions to plot up on the same graph.

Histograms plot

mf <-
list(lf_crabs$cw[lf_crabs$sex=='female'],lf_crabs$cw[lf_crabs$sex=='male'])
multhist(mf, xlab="CW", ylab="Frequency", ylim=c(0,100),main="All Measured
Crabs", col=c("dark gray", "light gray"),
         breaks=seq(90,210, by=10),beside=TRUE,space=c(0,0.5))
legend("topright", c("Females", "Males"), fill=c("dark gray", "light gray"))

Then I try to add a normal distribution curve just to the female data but I
cant get the output to plot

points(seq(min(mf[[1]]), max(mf[[1]]), length.out=300),
       dnorm(seq(min(mf[[1]]), max(mf[[1]]), length.out=300),
             mean(mf[[1]]), sd(mf[[1]])),type="l", col="dark gray")

Even trying to add an abline to the plot doesn't work.

What am I missing?

cheers

Andy
#
Hi Andrew,
First, a little mind reading. My crystal ball says that "cw" can be
interpreted as "carapace width". It didn't tell me the parameters of
the distribution, so:

set.seed(1234)
mf<-list(rnorm(400,145,15),rnorm(400,160,15))
library(plotrix)
multhist(mf, xlab="CW", ylab="Frequency", ylim=c(0,100),main="All Measured
Crabs", col=c("dark gray", "light gray"),
         breaks=seq(90,210, by=10),beside=TRUE,space=c(0,0.5))
legend("topright", c("Females", "Males"), fill=c("dark gray", "light gray"))
lines(seq(0,32,length.out=121),rescale(dnorm(90:210,145,15),c(0,100)))

This produces what I think you are after. Note that it may be
misleading as the distribution of carapace width in real mud crabs
doesn't look normal to me.

Jim
On Mon, May 13, 2019 at 3:00 PM Andrew Halford <andrew.halford at gmail.com> wrote:
1 day later
#
Hi Jim,

Many thanks for your help and yes CW is carapace width. Here is the final
coding I used...I set the peak of the curves at max frequency bin for each
sex. I also added the means and SD's from my data. According to my visual
diagnostics (qqplots, density plots) the frequency distributions do appear
normally distributed in this case. To be honest Ive struggled to find
whether CW is normally distributed in mud crab populations or not.


f<- lf_crabs$cw[lf_crabs$sex=='female']
m<- lf_crabs$cw[lf_crabs$sex=='male']

# find mean and sd to determine normal curve dimensions
m_m <-mean(m)
sd_m <-sqrt(var(m))
m_f <-mean(f)
sd_f <-sqrt(varf(f))

mf <- list(f,m)
multhist(mf, xlab="CW", ylab="Frequency", ylim=c(0,100),main="All Measured
Crabs", col=c("dark gray", "light gray"),
         breaks=seq(90,210, by=10),beside=TRUE,space=c(0,0.5))
legend("topright", c("Females", "Males"), fill=c("dark gray", "light gray"))
lines(seq(0,32,length.out=121),rescale(dnorm(90:210,145.4867,20.99906),c(0,50)),col="dark
gray",lwd=2)
lines(seq(0,32,length.out=121),rescale(dnorm(90:210,151.0783,21.88299),c(0,80)),col="light
gray",lwd=2)
abline(v=145.4867,lwd=2,col="red")
On Mon, 13 May 2019 at 17:30, Jim Lemon <drjimlemon at gmail.com> wrote: