在R中使用ggplot扩展x极限

在R中使用ggplot扩展x极限,r,ggplot2,R,Ggplot2,我目前正试图用下面的代码绘制一个带有覆盖的直方图(由my_fun提供) 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") gg

我目前正试图用下面的代码绘制一个带有覆盖的直方图(由my_fun提供)

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")
ggplot中的x轴从1到2(这是我的数据范围)。但是,我希望我的绘图具有从0到3的x轴,以便可以在范围(0,3)上绘制覆盖


我尝试添加
coord\u cartesian(xlim=c(0,3))
,但这不起作用。你能给我提供一些改变范围的建议吗?谢谢。

您在问题中只提供了一点有用的信息,所以请在这里猜一下,但这对我很有用:

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))

dat什么是
my_fun
,您能提供
dput(dfr)
的结果吗?谢谢。这很有效。myfun函数实际上是一个密度函数,与Joran的答案不同,有时设置一个相等的框架是有意义的:
+xlim(c(-max(abs(dat$x)),max(abs(dat$x)))