我想用離散資料在 R 中繪制密度直方圖。鑒于我的資料集非常大,我需要在繪圖之前計算密度。
但是,我發現與instats::density相比,使用該函式提供了不同的結果。這是為什么?ggplot2 的 ..density.. 結果也符合預期,而 stats::density 則不然。..density..ggplot2
請參閱下面的可重現示例。
非常感謝
library(tidyverse)
library(patchwork)
df <- data.frame(A = round(rnorm(1000)),
B = round(rnorm(1000)),
C = round(rnorm(1000))) %>%
pivot_longer(cols = everything(), names_to = "group")
dens_df <- df %>%
group_by(group) %>%
summarise(dens = list(density(value, from = -3, to = 6, n = length(-3:6)))) %>% #compute density and nest into list
mutate(density.x = map(dens, ~.x[["x"]]), #extract x values
density.y = map(dens, ~.x[["y"]])) %>% #extract y values
select(-dens) %>%
unnest(cols = c(density.x, density.y))
plot_dens <- dens_df %>%
ggplot()
aes(x = density.x, y = density.y) %>%
geom_col()
scale_x_continuous(breaks = seq(-3,10,1))
stat_function(fun = dnorm, n = 10, args = list(mean = 0, sd = 1), geom = "point", col = "red")
stat_function(fun = dnorm, n = 10, args = list(mean = 0, sd = 1), geom = "point", size = 2, col = "red")
stat_function(fun = dnorm, n = 10, args = list(mean = 0, sd = 1), geom = "line", col = "red")
facet_wrap(~group)
labs(title = "using stats::density")
plot_geom <- df %>%
ggplot()
aes(x = value, y = ..density..) %>%
geom_histogram(
binwidth = 1,
col = "white")
scale_x_continuous(breaks = seq(-3,10,1))
stat_function(fun = dnorm, n = 10, args = list(mean = 0, sd = 1), geom = "point", col = "red")
stat_function(fun = dnorm, n = 10, args = list(mean = 0, sd = 1), geom = "point", size = 2, col = "red")
stat_function(fun = dnorm, n = 10, args = list(mean = 0, sd = 1), geom = "line", col = "red")
facet_wrap(~group)
labs(title = "using ggplot2 ..density..")
plot_dens plot_geom
uj5u.com熱心網友回復:
簡單的答案是,這stats::density不是用于離散資料的正確函式。stats::density使用平滑內核并提供表示 -3 到 6 之間的連續密度的曲線,即使僅在 10 個點采樣。這與離散資料的密度不同,后者只是每個 bin 的頻率除以觀測值的數量。這就是geom_histogram繪制的內容。
獲得離散密度的計算有效方法是使用hist甚至只是table(x)/length(x).
這是一個使用預先計算的密度的示例hist
dens_df <- df %>%
group_by(group) %>%
summarise(dens = list(hist(value, breaks = seq(-3.5, 6.5), plot = FALSE))) %>%
mutate(density.x = map(dens, ~.x[["mids"]]), #extract x values
density.y = map(dens, ~.x[["density"]])) %>% #extract y values
select(-dens) %>%
unnest(cols = c(density.x, density.y))
我們可以看到這導致與 相同的圖geom_histogram,使用您自己的繪圖代碼,但使用更改后的dens_df
plot_dens <- dens_df %>%
ggplot()
aes(x = density.x, y = density.y) %>%
geom_col()
scale_x_continuous(breaks = seq(-3,10,1))
stat_function(fun = dnorm, n = 10, args = list(mean = 0, sd = 1),
geom = "point", col = "red")
stat_function(fun = dnorm, n = 10, args = list(mean = 0, sd = 1),
geom = "point", size = 2, col = "red")
stat_function(fun = dnorm, n = 10, args = list(mean = 0, sd = 1),
geom = "line", col = "red")
facet_wrap(~group)
labs(title = "using graphics::hist")
plot_geom <- df %>%
ggplot()
aes(x = value, y = ..density..) %>%
geom_histogram(
binwidth = 1,
col = "white")
scale_x_continuous(breaks = seq(-3,10,1))
stat_function(fun = dnorm, n = 10, args = list(mean = 0, sd = 1),
geom = "point", col = "red")
stat_function(fun = dnorm, n = 10, args = list(mean = 0, sd = 1),
geom = "point", size = 2, col = "red")
stat_function(fun = dnorm, n = 10, args = list(mean = 0, sd = 1),
geom = "line", col = "red")
facet_wrap(~group)
labs(title = "using ggplot2 ..density..")
plot_dens plot_geom

轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/493092.html
下一篇:條形圖的不同列顏色
