You can simply add the hist()
plot to already existing plot using add = TRUE
. Don't forget to use freq = FALSE
to say you want the probability scale, not the count scale.
set.seed(123)
x<-rnorm(100)
plot(x, exp(x)/(1+exp(x)), col = "green") # some plot
hist(x, freq = FALSE, add = TRUE)
EDIT: If you need to perform any manipulation on the computed histogram, you can replace the hist
call by splitting it into computation and the plotting itself, which is done using rect()
(see the source code of plot.histogram
):
h <- hist(x, plot = FALSE) # computation
rect(h$breaks[-length(h$breaks)], 0, h$breaks[-1], h$intensities) # plotting