I have two related problems.
Problem 1: I’m currently using the code below to generate a histogram overlayed with a density plot:
hist(x,prob=T,col="gray")
axis(side=1, at=seq(0,100, 20), labels=seq(0,100,20))
lines(density(x))
I’ve pasted the data (i.e. x above) here.
I have two issues with the code as it stands:
- the last tick and label (100) of the x-axis does not appear on the histogram/plot. How can I put these on?
- I’d like the y-axis to be of count or frequency rather than density, but I’d like to retain the density plot as an overlay on the histogram. How can I do this?
Problem 2: using a similar solution to problem 1, I now want to overlay three density plots (not histograms), again with frequency on the y-axis instead of density. The three data sets are at:
Here’s your first 2 questions:
The histogram has a bin width of 5, which is also equal to
1/sum(myhist$density), whereas thedensity(x)$xare in small jumps, around .2 in your case (512 even steps).sum(density(x)$y)is some strange number definitely not 1, but that is because it goes in small steps, when divided by the x interval it is approximately 1:sum(density(x)$y)/(1/diff(density(x)$x)[1]). You don’t need to do this later because it’s already matched up with its own odd x values. Scale 1) for the bin width ofhist()and 2) for the frequency of xlength(x), as DWin says. The last axis tick became visible after setting thexlimargument.To do your problem 2, set up a plot with the correct dimensions (
xlimandylim), withtype = "n", then draw 3 lines for the densities, scaled using something similar to the density line above. Think however about whether you want those semi continuous lines to reflect the heights of imaginary bars with bin width 5… You see how that might make the density lines exaggerate the counts at any particular point?