我目前正在使用ggplot包来绘制一个带有N(0,1)密度曲线的正态变量直方图。我对这个包非常陌生,我正在使用的代码是:
x = rnorm(1000)
qplot(x, geom = 'blank') +
geom_histogram(aes(y = ..density.., colour = 'Histogram'), legend = FALSE,
binwidth = 0.5, fill = "blue") +
stat_function(fun = dnorm, aes(colour = 'Density'))+
scale_x_continuous('x', limits = c(-4, 4))+
opts(title = "Histogram with Overlay")+
scale_colour_manual(name = 'Legend', values = c('darkblue', 'red')) +
scale_y_continuous('Frequency')+
opts(legend.key=theme_rect(fill="white",colour="white"))+
opts(legend.background = theme_rect())
这段代码生成了下面的图表。我如何更改图例,以便用填充的蓝色框(代表直方图的条形)替换表示直方图的线条?谢谢!
geom_density()
。在我看来比stat_function(...)
更容易。 - Chase