本文介绍了在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 linkSample : https://github.com/tidyverse/ggplot2/blob/master/R/stat-function.r

这篇关于在ggplot中绘制平滑正态分布的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 04:39