如果我想繪制一個經驗密度,我會去:
library(ggplot2) ggplot() geom_density(aes(x = rbeta(100,3,1)))
要么
library(ggplot2)
ggplot()
geom_histogram(aes(x = rbeta(100,3,1)))
其中rbeta(100,3,1)可以是任何向量。
如果我想繪制一個理論密度,我可以去:
library(ggplot2)
ggplot(data = data.frame(x = c(0, 1)), mapping = aes(x = x))
stat_function(fun = dbeta, args = c(3,1), n = 100)
但是當我嘗試在第二條曲線上繪制第一條曲線時:
library(ggplot2)
ggplot(data = data.frame(x = c(0, 1)), mapping = aes(x = x))
stat_function(fun = dbeta, args = c(3,1), n = 100)
geom_histogram(aes(x = rbeta(100,3,1)))
我會得到一個錯誤。
如何在理論上繪制經驗確定的密度?
uj5u.com熱心網友回復:
這似乎有效。
先繪制直方圖,然后繪制密度。并且隨著更多的資料點,直方圖更適合理論密度。
library(ggplot2)
library(gridExtra)
set.seed(2022)
p1 <- ggplot()
geom_histogram(aes(x = rbeta(100,3,1), y = ..density..), bins = 30)
stat_function(fun = dbeta, args = c(3,1), n = 100)
p2 <- ggplot()
geom_histogram(aes(x = rbeta(10000,3,1), y = ..density..), bins = 30)
stat_function(fun = dbeta, args = c(3,1), n = 100)
grid.arrange(p1, p2, ncol=2)

由reprex 包于 2022-02-20 創建(v2.0.1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/429479.html
下一篇:在ggplot中繪制多列點
