Reputation: 663
I'm currently trying to plot a histogram with an overlay (given by my_fun) using the following code.
dfr = data.frame(x)
ggplot(dfr,aes(x)) + geom_histogram(colour="darkblue", binwidth = 0.1,
aes(y =..density..), size=1, fill="blue", freq = TRUE)+
stat_function(fun = my_fun, colour = "red")
The x-axis in ggplot is from 1 to 2 (which is the range of my data). However, I would like my plot to have an x-axis from 0 to 3, so that the overlay can be drawn over the range (0, 3).
I've tried adding coord_cartesian(xlim=c(0, 3))
but this does not work. Could you please provide me with some suggestions on changing the range? Thank You.
Upvotes: 2
Views: 10346
Reputation: 173737
Just guessing here since you provided only a little useful information in your question, but this works for me:
dat <- data.frame(x=rnorm(100))
ggplot(dat,aes(x=x)) +
geom_histogram(aes(y=..density..),freq=TRUE) +
stat_function(fun = dnorm, colour="red") +
xlim(c(-4,4))
using xlim
rather than coord_cartesian
. But since you haven't provided any details on your data or function, I can't assure you that this will work in your case.
Upvotes: 4