简体   繁体   English

在ggplot中绘制平滑正态分布的最佳方法

[英]Best way to plot smooth normal distribution in ggplot

I would like to plot a nice, 'approaching the limit'-looking normal pdf in ggplot.我想在 ggplot 中绘制一个漂亮的、“接近极限”的正常 pdf。

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.然而,这很慢,特别是如果我希望在某个时候与 Shiny 合作。

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 uses the dnorm function (to get the density of a normal variable) parses in the mean & median values and plots the normal distribution. stat_function使用dnorm函数(以获取正态变量的密度)解析均值和中值并绘制正态分布。

Reference : How dnorm works?参考: dnorm 是如何工作的?

For ggplot stat_function Documentation follow this link Sample : https://github.com/tidyverse/ggplot2/blob/master/R/stat-function.r对于 ggplot stat_function文档,请按照此链接示例: https : //github.com/tidyverse/ggplot2/blob/master/R/stat-function.r

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM