## 12.9 密度图

ggplot(mpg, aes(cty)) +
geom_density(aes(fill = factor(cyl)), alpha = 0.8) +
labs(
title = "Density plot",
subtitle = "City Mileage Grouped by Number of cylinders",
caption = "Source: mpg",
x = "City Mileage",
fill = "# Cylinders"
)

ggplot(diamonds, aes(x = price, fill = cut)) + geom_density()
ggplot(diamonds, aes(x = price, fill = cut)) + geom_density(alpha = 0.5)

ggplot(diamonds, aes(x = price, fill = cut)) +
geom_density(position = "stack")

# You can use position="fill" to produce a conditional density estimate
ggplot(diamonds, aes(carat, stat(count), fill = cut)) +
geom_density(position = "fill")

ggplot(diamonds) +
ggridges::geom_density_ridges(aes(x = price, y = color, fill = color))

ggplot(diamonds, aes(x = carat, y = price)) +
geom_density_2d(aes(color = cut)) +
facet_grid(~cut)

stat 函数，特别是 nlevel 参数，在密度曲线之间填充我们又可以得到热力图

ggplot(diamonds, aes(x = carat, y = price)) +
stat_density_2d(aes(fill = stat(nlevel)), geom = "polygon") +
facet_grid(. ~ cut)

gemo_hex 也是二维密度图的一种变体，特别适合数据量比较大的情形

ggplot(diamonds, aes(x = carat, y = price)) + geom_hex() +
scale_fill_viridis_c()

heatmaps in ggplot2 二维密度图

ggplot(faithful, aes(x = eruptions, y = waiting)) +
stat_density_2d(aes(fill = ..level..), geom = "polygon") +
xlim(1, 6) +
ylim(40, 100)

ggplot(faithful, aes(x = eruptions, y = waiting)) +
stat_density2d(aes(fill = stat(level)), geom = "polygon") +
scale_fill_viridis_c(option = "viridis") +
xlim(1, 6) +
ylim(40, 100)

MASS::kde2d() 实现二维核密度估计，ggplot2 包提供了两种等价的绘图方式

1. stat_density_2d()..
2. stat_density2d()stat()
plotly::plot_ly(
data = faithful, x = ~eruptions,
y = ~waiting, type = "histogram2dcontour"
) %>%
plotly::config(displayModeBar = FALSE)

# plot_ly(faithful, x = ~waiting, y = ~eruptions) %>%
#   add_histogram2dcontour()
library(KernSmooth)
p1 <- plotly::plot_ly(x = den$x1, y = den$x2, z = den$fhat) %>% plotly::config(displayModeBar = FALSE) %>% plotly::add_heatmap() # 等高线图 p2 <- plotly::plot_ly(x = den$x1, y = den$x2, z = den$fhat) %>%
htmltools::tagList(p1, p2)