R ggplot";“急躁”;分布图

R ggplot";“急躁”;分布图,r,plot,ggplot2,R,Plot,Ggplot2,我使用ggplot2和以下代码绘制具有两种密度的直方图: ggplot(khstat_wide, aes(x=khstat_vorher)) + geom_histogram(aes(y=..density..),binwidth=200) + ylim(0,0.004) + xlim(0,10000) + scale_colour_brewer(palette="Dark2") + stat_function(fun = dtweedie, aes(colour = 'Tweedie'), a

我使用ggplot2和以下代码绘制具有两种密度的直方图:

ggplot(khstat_wide, aes(x=khstat_vorher)) + geom_histogram(aes(y=..density..),binwidth=200) +
ylim(0,0.004) + xlim(0,10000) + scale_colour_brewer(palette="Dark2") +
stat_function(fun = dtweedie, aes(colour = 'Tweedie'), args = list(xi=round(out3$p.max,4), mu=mean(khstat_wide$khstat_vorher),phi=out3$phi.max)) +
theme(title=element_text(size=14),axis.text=element_text(size=12), legend.text=element_text(size=12),
    legend.position=c(.85,.85), legend.title=element_blank()) +
stat_function(fun = dgamma, aes(colour = 'Gamma' ), args = list(shape=1.199157e-01, scale=1.138242e+04 )) 

请注意,我设置了
ylim(0,0.004)+xlim(010000)
,但这样做会更改直方图的比例,因为忽略了超出此范围的值

这是与坐标笛卡尔(ylim=c(0,0.004),xlim=c(05000))相同的图,但现在密度线看起来真的“尖锐”:

我怎样才能使它们再次光滑

更新:

很难建立一个可复制的示例,下面是一个尝试:

a <- rgamma(500, shape=0.5)
b <- rep(0,300)
df <- data.frame(V1=c(a,b), V2=0)
不起作用:

ggplot(df, aes(x=V1)) +  geom_histogram(aes(y=..density..),binwidth=200) +
scale_colour_brewer(palette="Dark2") +
stat_function(fun = dgamma, aes(colour = 'Gamma' ), args =  list(shape=1.199157e-01, scale=1.138242e+04 )) +
coord_cartesian(ylim=c(0,0.01),xlim=c(0,1000))

这为我解决了问题

首先,创建原始绘图的对象

p <- ggplot(df, aes(x=V1)) + geom_histogram(aes(y=..density..),binwidth=200) +
  scale_colour_brewer(palette="Dark2") +
  stat_function(fun = dgamma, aes(colour = 'Gamma' ), args =   list(shape=1.199157e-01, scale=1.138242e+04 )) +
  ylim(0,0.01) + xlim(0,1000)
p + coord_cartesian(ylim=c(0,0.01),xlim=c(0,500))

这为我解决了这个问题

首先,创建原始绘图的对象

p <- ggplot(df, aes(x=V1)) + geom_histogram(aes(y=..density..),binwidth=200) +
  scale_colour_brewer(palette="Dark2") +
  stat_function(fun = dgamma, aes(colour = 'Gamma' ), args =   list(shape=1.199157e-01, scale=1.138242e+04 )) +
  ylim(0,0.01) + xlim(0,1000)
p + coord_cartesian(ylim=c(0,0.01),xlim=c(0,500))

如果您提供了一些数据,这会有所帮助。@Heroka抱歉,这些数据是保密的,那么您可以提供示例数据。如果有一些(类似的)数据可供使用,则更容易提供帮助。如果您提供了一些数据,这里有一些可能会有所帮助。@Heroka抱歉,这些数据是机密的,那么您可以提供示例数据。如果有一些(类似的)数据可供使用,则更容易提供帮助。这里有一些