在ggplot中绘制平滑正态分布的最佳方法
我想在ggplot中绘制一个漂亮的,接近极限"的普通pdf文件.
I would like to plot a nice, 'approaching the limit'-looking normal pdf in ggplot.
我发现要获得一个非常对称且外观整洁的图,我必须将样本数量提高到相当大的数量.一百万创造了一个很好的可视化效果.但是,这非常慢,特别是如果我希望在某些时候与Shiny合作的话.
I found that to get a very symmetric and clean looking plot, I had to crank up the number of samples to a rather large number; one million creates a great visualization. However, this is pretty slow, especially if I hope to work with Shiny at some point.
df <- data.frame(c(rnorm(1000000)))
ggplot(df, aes(df[1])) + geom_density()
肯定有更好的方法来显示接近理想正态分布的东西吗?
Surely there is a better way to display something close to the ideal normal distribution?
基本上,您的代码应类似于:
Basically, your code should look like:
ggplot(data=dataset, aes(dataset$value)) +
stat_function(fun = dnorm, args = c(mean = mean(dataset$value), sd = sd(dataset$value)))
stat_function
使用 dnorm
函数(以获取正态变量的密度)来分析均值&中值并绘制正态分布.
stat_function
uses the dnorm
function (to get the density of a normal variable) parses in the mean & median values and plots the normal distribution.
参考资料: dnorm如何工作?
对于ggplot stat_function
文档,请遵循以下 https://github.com/tidyverse/ggplot2/blob/master/R/stat-function.r
For ggplot stat_function
Documentation follow this link
Sample : https://github.com/tidyverse/ggplot2/blob/master/R/stat-function.r