Aloha -
I am creating a series of histograms and would like to add a vertical line indicating the mean value of the distribution as well as a text label indicating the sample size. My code is as follows:
BIN_WIDTH <- 1 #desired bin width
print(histogram(~ Length..cm. | Method, #create and print the histogram and save to variable "graph"
data = hist.data[hist.data$Scientific_name == "Pristipomoides filamentosus",],
nint = (max(hist.data$Length..cm.) - min(hist.data$Length..cm.)+1)/BIN_WIDTH,
layout = c(1,2),
type = "density",
main = "Length-Frequency of Pristipomoides filamentosus by Gear",
xlab = "Length (cm)",
panel = function(x, ...){
panel.histogram(x,...)
panel.mathdensity(dmath = dnorm, col = "red",
args = list(mean = mean(x), sd= sd(x)), ...)
}
))
I assumed this would be done using the panel.abline
and panel.text
functions inserted just after panel.histogram
, but this does not seem to be working. What am I doing wrong? If someone can give me the code for a dummy vertical line (e.g. x=10) and dummy text, I can easily insert the equation for the mean and sample size.