当前位置:   article > 正文

R 数据可视化 —— ggplot 二维直方图和密度图_r语言 ggplot 直方图 概率密度曲线

r语言 ggplot 直方图 概率密度曲线

二维直方图

二维直方图用于二维数据的统计分析X-Y 轴变量均为数值型。首先将坐标平面分割为许多大小相等的区间,并计算落在每个区间中的观察值数目,然后将观察值映射为矩形的填充色。

ggplot2 中,geom_bin2d 函数的区间形状是矩形,而 geom_hex 函数可以绘制六边形区间。

示例

1. geom_bin2d
d <- ggplot(diamonds, aes(x, y)) + xlim(4, 10) + ylim(4, 10)
d + geom_bin2d()
  • 1
  • 2

设置分箱数目

d + geom_bin2d(bins = 10)
  • 1

设置分箱的宽度(水平和竖直)

d + geom_bin2d(binwidth = c(0.1, 0.1))
  • 1

更改颜色

d + geom_bin2d(aes(fill = after_stat(count))) +
  scale_fill_gradientn(colours = rainbow(10))
  • 1
  • 2

设置边缘线条大小与颜色

d + geom_bin2d(size = 1, colour = "green")
  • 1

2. geom_hex

geom_hexgeom_bin2d 的参数一样

d <- ggplot(diamonds, aes(x, y)) + 
  xlim(4, 10) + ylim(4, 10)
  
d + geom_hex()
  • 1
  • 2
  • 3
  • 4

d + geom_hex(size = 1, colour = "#FF9900FF")
  • 1

二维密度图

前面我们提到过使用 geom_density() 函数来绘制一维密度图。

对于二维核密度估计,我们使用 geom_density_2d() 函数,以线条的方式展示,使用 geom_density_2d_filled() 以填充色的方式展示

示例

绘制分布线条

m <- ggplot(faithful, aes(x = eruptions, y = waiting)) +
 geom_point() +
 xlim(0.5, 6) +
 ylim(40, 110)
 
m + geom_density_2d()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

填充色

m + geom_density_2d_filled(alpha = 0.5)
  • 1

结合线条与填充色

m + geom_density_2d_filled(alpha = 0.5) +
  geom_density_2d(size = 0.25, colour = "black")
  • 1
  • 2

添加分组变量

d <- sample_n(diamonds, 1000) %>%
  ggplot(aes(x, y))
  
d + geom_density_2d(aes(colour = cut))
  • 1
  • 2
  • 3
  • 4

进行分面

d + geom_density_2d_filled() + 
  facet_wrap(vars(cut))
  • 1
  • 2

将观测值的数量映射到颜色强度

d + geom_density_2d_filled(contour_var = "count") + 
  facet_wrap(vars(cut))
  • 1
  • 2

我们也可以使用前一节提到的色块图函数,如 raster 对象

d + stat_density_2d(
  geom = "raster",
  aes(fill = after_stat(density)),
  contour = FALSE
) + scale_fill_viridis_c()
  • 1
  • 2
  • 3
  • 4
  • 5

或者其他对象,如散点图

d + stat_density_2d(geom = "point", aes(size = after_stat(density)), 
                    n = 20, contour = FALSE)
  • 1
  • 2

polygon 对象

d + stat_density_2d(
  geom = "polygon",
  aes(fill = after_stat(level)),
  bins = 30
) + scale_fill_viridis_c()
  • 1
  • 2
  • 3
  • 4
  • 5

组合分布图

我们可以将二维直方图和密度图,与一维统计分布图结合起来,更加详细的展示数据的分布情况

首先,构造正态分布数据

library(cowplot)

N <- 500

df <- tibble(
  x1 = rnorm(n = N, mean = 2),
  x2 = rnorm(n = N, mean = 2),
  
  y1 = rnorm(n = N, mean = 2),
  y2 = rnorm(n = N, mean = 2)
)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

绘制边缘即组合直方图

top_hist <- ggplot(df, aes(x1)) +
  geom_histogram(bins = 35, fill = "#1f78b4", colour = "black") +
  theme_void()

right_hist <- ggplot(df, aes(x2)) +
  geom_histogram(bins = 35, fill = "#1f78b4", colour = "black") +
  coord_flip() +
  theme_void()

center <- ggplot(df, aes(x1, x2)) +
  geom_hex(colour = "black") +
  scale_fill_gradientn(colours = rainbow(10)) +
  theme(
    panel.background=element_rect(fill="white",colour="black",size=0.25),
    axis.line=element_line(colour="black",size=0.25),
    axis.title=element_text(size=13,face="plain",color="black"),
    axis.text = element_text(size=12,face="plain",color="black"),
    legend.position=c(0.10,0.80),
    legend.background=element_blank()
  )

p1 <- plot_grid(top_hist, center, align = "v", 
          nrow = 2, rel_heights = c(1, 4))

p2 <- plot_grid(NULL, right_hist, align = "v",
          nrow = 2, rel_heights = c(1, 4))

plot_grid(p1, p2, ncol = 2, 
          rel_widths = c(4, 1))

glist <- list(top_hist, center, right_hist)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

绘制二维密度图的代码类似

top_hist <- ggplot(df, aes(y1)) +
  # geom_histogram(bins = 35, fill = "#1f78b4", colour = "black") +
  geom_density(fill = "#1f78b4", colour = "black") +
  theme_void()

right_hist <- ggplot(df, aes(y2)) +
  # geom_histogram(bins = 35, fill = "#1f78b4", colour = "black") +
  geom_density(fill = "#1f78b4", colour = "black") +
  coord_flip() +
  theme_void()

center <- ggplot(df, aes(y1, y2)) +
  geom_density2d(colour = "black") +
  geom_density2d_filled() +
  scale_fill_brewer(palette = "Set2") +
  theme(
    panel.background=element_rect(fill="white",colour="black",size=0.25),
    axis.line=element_line(colour="black",size=0.25),
    axis.title=element_text(size=13,face="plain",color="black"),
    axis.text = element_text(size=12,face="plain",color="black"),
    legend.position = "none"
  )

p1 <- plot_grid(top_hist, center, align = "v", 
          nrow = 2, rel_heights = c(1, 4))

p2 <- plot_grid(NULL, right_hist, align = "v",
          nrow = 2, rel_heights = c(1, 4))

plot_grid(p1, p2, ncol = 2, 
          rel_widths = c(4, 1))

glist <- list(top_hist, center, right_hist)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

代码:https://github.com/dxsbiocc/learn/blob/main/R/plot/hist_density_2d.R

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/131959
推荐阅读
相关标签
  

闽ICP备14008679号