如何将高斯曲线添加到使用 qplot 创建的直方图?

发布于 2024-12-01 09:51:32 字数 434 浏览 1 评论 0原文

我的问题可能类似于 将密度曲线拟合到直方图R。使用 qplot,我使用此命令创建了 7 个直方图:

 (qplot(V1, data=data, binwidth=10, facets=V2~.)   

对于每个切片,我想添加一条拟合高斯曲线。当我尝试使用 lines() 方法时,出现错误:

Error in plot.xy(xy.coords(x, y), type = type, ...) : 
plot.new has not been called yet

正确执行此操作的命令是什么?

I have question probably similar to Fitting a density curve to a histogram in R. Using qplot I have created 7 histograms with this command:

 (qplot(V1, data=data, binwidth=10, facets=V2~.)   

For each slice, I would like to add a fitting gaussian curve. When I try to use lines() method, I get error:

Error in plot.xy(xy.coords(x, y), type = type, ...) : 
plot.new has not been called yet

What is the command to do it correctly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

骷髅 2024-12-08 09:51:32

您尝试过stat_function吗?

+ stat_function(fun = dnorm)

您可能需要使用 aes(y = ..密度..) 绘制直方图,以便绘制密度值而不是计数。

许多有用的信息可以在这个中找到 问题,包括在不同方面绘制不同正态曲线的一些建议。

以下是一些示例:

dat <- data.frame(x = c(rnorm(100),rnorm(100,2,0.5)), 
                  a = rep(letters[1:2],each = 100))

在每个面上覆盖单个法线密度:

ggplot(data = dat,aes(x = x)) + 
  facet_wrap(~a) + 
    geom_histogram(aes(y = ..density..)) + 
    stat_function(fun = dnorm, colour = "red")

在此处输入图像描述

从我链接到的问题中,创建具有不同正态曲线的单独数据框:

grid <- with(dat, seq(min(x), max(x), length = 100))
normaldens <- ddply(dat, "a", function(df) {
  data.frame( 
    predicted = grid,
    density = dnorm(grid, mean(df$x), sd(df$x))
  )
})

并使用 geom_line 分别绘制它们:

ggplot(data = dat,aes(x = x)) + 
    facet_wrap(~a) + 
    geom_histogram(aes(y = ..density..)) + 
    geom_line(data = normaldens, aes(x = predicted, y = density), colour = "red")

Have you tried stat_function?

+ stat_function(fun = dnorm)

You'll probably want to plot the histograms using aes(y = ..density..) in order to plot the density values rather than the counts.

A lot of useful information can be found in this question, including some advice on plotting different normal curves on different facets.

Here are some examples:

dat <- data.frame(x = c(rnorm(100),rnorm(100,2,0.5)), 
                  a = rep(letters[1:2],each = 100))

Overlay a single normal density on each facet:

ggplot(data = dat,aes(x = x)) + 
  facet_wrap(~a) + 
    geom_histogram(aes(y = ..density..)) + 
    stat_function(fun = dnorm, colour = "red")

enter image description here

From the question I linked to, create a separate data frame with the different normal curves:

grid <- with(dat, seq(min(x), max(x), length = 100))
normaldens <- ddply(dat, "a", function(df) {
  data.frame( 
    predicted = grid,
    density = dnorm(grid, mean(df$x), sd(df$x))
  )
})

And plot them separately using geom_line:

ggplot(data = dat,aes(x = x)) + 
    facet_wrap(~a) + 
    geom_histogram(aes(y = ..density..)) + 
    geom_line(data = normaldens, aes(x = predicted, y = density), colour = "red")

enter image description here

无名指的心愿 2024-12-08 09:51:32

ggplot2 使用与基础图形不同的图形范例。 (虽然您可以使用 grid 图形,但最好的方法是向绘图添加新的 stat_function 图层。ggplot2 代码是 请注意

,我无法使用 qplot 来实现此功能,但到 ggplot 的转换相当简单,最重要的区别是您的数据必须采用 data.frame 格式。

另请注意 y 美学 aes=aes(y=..密度..)) 的显式映射 - 这有点不寻常,但会获取 stat_function 结果并将其映射到数据:

library(ggplot2)
data <- data.frame(V1 <- rnorm(700), V2=sample(LETTERS[1:7], 700, replace=TRUE))
ggplot(data, aes(x=V1)) + 
  stat_bin(aes(y=..density..)) + 
  stat_function(fun=dnorm) + 
  facet_grid(V2~.)

在此处输入图像描述

ggplot2 uses a different graphics paradigm than base graphics. (Although you can use grid graphics with it, the best way is to add a new stat_function layer to the plot. The ggplot2 code is the following.

Note that I couldn't get this to work using qplot, but the transition to ggplot is reasonably straighforward, the most important difference is that your data must be in data.frame format.

Also note the explicit mapping of the y aesthetic aes=aes(y=..density..)) - this is slighly unusual but takes the stat_function results and maps it to the data:

library(ggplot2)
data <- data.frame(V1 <- rnorm(700), V2=sample(LETTERS[1:7], 700, replace=TRUE))
ggplot(data, aes(x=V1)) + 
  stat_bin(aes(y=..density..)) + 
  stat_function(fun=dnorm) + 
  facet_grid(V2~.)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文